Karthikeyan Singaravelan <tir.kar...@gmail.com> added the comment:

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

----------
nosy: +gvanrossum, levkivskyi, xtreak
versions: +Python 3.8

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue36363>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to