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.

Author xtreak
Recipients bux, eric.smith, gvanrossum, levkivskyi, xtreak
Date 2019-03-19.15:41:34
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1553010094.98.0.435053850117.issue36363@roundup.psfhosted.org>
In-reply-to
Content
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
History
Date User Action Args
2019-03-19 15:41:35xtreaksetrecipients: + xtreak, gvanrossum, eric.smith, levkivskyi, bux
2019-03-19 15:41:34xtreaksetmessageid: <1553010094.98.0.435053850117.issue36363@roundup.psfhosted.org>
2019-03-19 15:41:34xtreaklinkissue36363 messages
2019-03-19 15:41:34xtreakcreate