smarie/python-autoclass

KeyError: '@autodict generated dict view - args is a constructor parameter but is not a field (was the constructor called ?)'

smarie opened this issue · 1 comments

This happens when I create a simple class and use repr on the object:

from autoclass import autoclass

@autoclass
class City:
    name: Optional[str]
    buildings: List[str] = []
>>> City("mine")

yields

  File "C:\_dev\python_ws\_Libs_OpenSource\python-autoclass\autoclass\autodict_.py", line 456, in __getitem__
    'field (was the constructor called ?)'.format(key))
KeyError: '@autodict generated dict view - args is a constructor parameter but is not a field (was the constructor called ?)'

The answer is simple: since none of the fields is actually an explicit field, autoclass has no way to understand that it should use autofields to create them.

You should use autofields=True

@autoclass(autofields=True)
class City:
    name: Optional[str]
    buildings: List[Building] = []

Note that we may wish to change this overly complex behaviour for the sake of usability.