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

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