[issue45946] RecursionError when annotating a field with the same name as a field

2021-12-01 Thread Eric V. Smith


Change by Eric V. Smith :


--
assignee:  -> eric.smith

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45946] RecursionError when annotating a field with the same name as a field

2021-12-01 Thread Gobot1234


New submission from Gobot1234 :

Small snippet to reproduce:
```
from dataclasses import dataclass, field

@dataclass
class Foo:
bool: bool = field()
```
Raises
```
---
RecursionErrorTraceback (most recent call last)
 in 
  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__ +
-> 1087str(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.

--
components: Library (Lib)
messages: 407438
nosy: Gobot1234, eric.smith
priority: normal
severity: normal
status: open
title: RecursionError when annotating a field with the same name as a field
type: crash
versions: Python 3.11

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com