Message338367
I am not sure this is a problem with dataclasses. dataclasses acts upon information from cls.__dict__.get('__annotations__', {}) at [0] and sets the type attribute for the field. Seems like using a valid identifier (class or function) used as class attribute along with defining it as optional type of the same name sets None type in annotation. Also happens when a class attribute is used with optional type for another attribute as in Spam class This more feels like a behavior with typing module. I am adding typing module maintainers for clarification.
# bpo36363.py
import typing
class Baz:
pass
class Bar:
pass
class Foo:
baz: typing.Optional[Bar] = None
Bar: typing.Optional[Bar] = None
class Spam:
bar: typing.Optional[Bar] = None
baz: typing.Optional[bar] = None
print(Foo.__dict__.get('__annotations__', {}))
print(Spam.__dict__.get('__annotations__', {}))
$ ./python.exe ../backups/bpo36363.py
{'baz': typing.Union[__main__.Bar, NoneType], 'Bar': <class 'NoneType'>}
{'bar': typing.Union[__main__.Bar, NoneType], 'baz': <class 'NoneType'>}
[0] https://github.com/python/cpython/blob/dcf617152e1d4c4a5e7965733928858a9c0936ca/Lib/dataclasses.py#L828 |
|
Date |
User |
Action |
Args |
2019-03-19 15:41:35 | xtreak | set | recipients:
+ xtreak, gvanrossum, eric.smith, levkivskyi, bux |
2019-03-19 15:41:34 | xtreak | set | messageid: <1553010094.98.0.435053850117.issue36363@roundup.psfhosted.org> |
2019-03-19 15:41:34 | xtreak | link | issue36363 messages |
2019-03-19 15:41:34 | xtreak | create | |
|