This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: RecursionError when annotating a field with the same name as a field
Type: crash Stage:
Components: Library (Lib) Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: eric.smith Nosy List: Gobot1234, eric.smith
Priority: normal Keywords:

Created on 2021-12-01 10:19 by Gobot1234, last changed 2022-04-11 14:59 by admin.

Messages (1)
msg407438 - (view) Author: Gobot1234 (Gobot1234) * Date: 2021-12-01 10:19
Small snippet to reproduce:
```
from dataclasses import dataclass, field

@dataclass
class Foo:
    bool: bool = field()
```
Raises
```
---------------------------------------------------------------------------
RecursionError                            Traceback (most recent call last)
<ipython-input-1-dce9c97e78ae> in <module>
      2 
      3 @dataclass
----> 4 class Foo:
      5     bool: bool = field()
      6 

/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/dataclasses.py in dataclass(cls, init, repr, eq, order, unsafe_hash, frozen, match_args, kw_only, slots)
   1176 
   1177     # We're called as @dataclass without parens.
-> 1178     return wrap(cls)
   1179 
   1180 

/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/dataclasses.py in wrap(cls)
   1167 
   1168     def wrap(cls):
-> 1169         return _process_class(cls, init, repr, eq, order, unsafe_hash,
   1170                               frozen, match_args, kw_only, slots)
   1171 

/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/dataclasses.py in _process_class(cls, init, repr, eq, order, unsafe_hash, frozen, match_args, kw_only, slots)
   1085         # Create a class doc-string.
   1086         cls.__doc__ = (cls.__name__ +
-> 1087                        str(inspect.signature(cls)).replace(' -> None', ''))
   1088 
   1089     if match_args:

/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/inspect.py in __str__(self)
   3200         render_kw_only_separator = True
   3201         for param in self.parameters.values():
-> 3202             formatted = str(param)
   3203 
   3204             kind = param.kind

/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/inspect.py in __str__(self)
   2717         if self._annotation is not _empty:
   2718             formatted = '{}: {}'.format(formatted,
-> 2719                                        formatannotation(self._annotation))
   2720 
   2721         if self._default is not _empty:

/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/inspect.py in formatannotation(annotation, base_module)
   1362             return annotation.__qualname__
   1363         return annotation.__module__+'.'+annotation.__qualname__
-> 1364     return repr(annotation)
   1365 
   1366 def formatannotationrelativeto(object):

/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/dataclasses.py in __repr__(self)
    281 
    282     def __repr__(self):
--> 283         return ('Field('
    284                 f'name={self.name!r},'
    285                 f'type={self.type!r},'

... last 1 frames repeated, from the frame below ...

/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/dataclasses.py in __repr__(self)
    281 
    282     def __repr__(self):
--> 283         return ('Field('
    284                 f'name={self.name!r},'
    285                 f'type={self.type!r},'

RecursionError: maximum recursion depth exceeded while getting the repr of an object
```
This is due to the self.type being the field itself as the annotation is evaluated using the class namespace.
History
Date User Action Args
2022-04-11 14:59:53adminsetgithub: 90104
2021-12-01 14:30:55eric.smithsetassignee: eric.smith
2021-12-01 10:19:19Gobot1234create