votes up 3

'%s' object has no attribute '%s'

Package:
ansible
github stars 49704
Exception Class:
AttributeError

Raise code

display = Display()


def _generic_g(prop_name, self):
    try:
        value = self._attributes[prop_name]
    except KeyError:
        raise AttributeError("'%s' object has no attribute '%s'" % (self.__class__.__name__, prop_name))

    if value is Sentinel:
        value = self._attr_defaults[prop_name]

    return value
😲 Agile task management is now easier than calling a taxi. #Tracklify
🙏 Scream for help to Ukraine
Today, 14th August 2022, Russia continues bombing and firing Ukraine. Don't trust Russia, they are bombing us and brazenly lying in same time they are not doing this 😠, civilians and children are dying too! We are screaming and asking exactly you to help us, we want to survive, our families, children, older ones.
Please spread the information, and ask your governemnt to stop Russia by any means. We promise to work extrahard after survival to make the world safer place for all.

Ways to fix

votes up 1 votes down

Summary:

This exception is thrown when the _generic_g function is called from ansible. The function takes in two parameters: prop_name and self. prop_name is a string and self is an instance of an object. The object passed in must have an _attributes dictionary where one of the keys is the value passed into prop_name, otherwise, the exception will be thrown.

Code to Reproduce the Error (Wrong):

from ansible.playbook.base import _generic_g

class A():
  _attributes = {"key":"val"}

a = A()
_generic_g("random", a)

Error Message:

KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/ansible/playbook/base.py in _generic_g(prop_name, self)
     32     try:
---> 33         value = self._attributes[prop_name]
     34     except KeyError:

KeyError: 'random'
During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)

1 frames
/usr/local/lib/python3.7/dist-packages/ansible/playbook/base.py in _generic_g(prop_name, self)
     33         value = self._attributes[prop_name]
     34     except KeyError:
---> 35         raise AttributeError("'%s' object has no attribute '%s'" % (self.__class__.__name__, prop_name))
     36 
     37     if value is Sentinel:

AttributeError: 'A' object has no attribute 'random'

Working Version (Right):

from ansible.playbook.base import _generic_g

class A():
  _attributes = {"key":"val"}

a = A()
_generic_g("key", a)
Jul 30, 2021 codingcomedyig answer

Add a possible fix

Please authorize to post fix