[issue33094] dataclasses: ClassVar attributes are not working properly
New submission from Adrian Stachlewski : Class variables should behave in the same way whether with or without ClassVar annotation. Unfortunately there are not. class A: __slots__ = () x: ClassVar = set() A() # it's ok @dataclass class B: __slots__ = () x = set() B() # ok too @dataclass class C: __slots__ = () # cannot use set() because of error x: ClassVar = field(default_factory=set) C() # AttributeError: 'C' object has no attribute 'x' Exception is raised from __init__ method, with flag init=False nothing changes. Python version: 3.7.0b2 -- components: Library (Lib) messages: 314017 nosy: stachel priority: normal severity: normal status: open title: dataclasses: ClassVar attributes are not working properly type: behavior versions: Python 3.7 ___ Python tracker <https://bugs.python.org/issue33094> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue33094] dataclasses: ClassVar attributes are not working properly
Adrian Stachlewski added the comment: Thanks for explaining. I was trying to do something like @dataclass class A: x: ClassVar = set() and thanks to you I know it should be @dataclass class A: x: ClassVar[Set] = set() If you are looking for improved error message, it's probably should be somehow connected to not proper usage of annotation. I've ended with default_factory because x: ClassVar = set() wasn't working and there was no error with default_factory and without slots. -- ___ Python tracker <https://bugs.python.org/issue33094> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue33100] dataclasses and __slots__ - non-default argument (member_descriptor)
New submission from Adrian Stachlewski : I've tried to declare two classes @dataclass class Base: __slots__ = ('x',) x: Any @dataclass class Derived(Base): x: int y: int As long as I correctly understood PEP 557 (inheritance part), changing type of variable is possible. This code produce error: TypeError: non-default argument 'y' follows default argument 'x' variable in Derived class has changed default from MISSING to member_descriptor and that's the reason of the exception. -- components: Library (Lib) messages: 314077 nosy: stachel priority: normal severity: normal status: open title: dataclasses and __slots__ - non-default argument (member_descriptor) type: behavior versions: Python 3.7, Python 3.8 ___ Python tracker <https://bugs.python.org/issue33100> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue33094] dataclasses: ClassVar attributes are not working properly
Adrian Stachlewski added the comment: Once more same mistake. 'x' should be declared as: - x: ClassVar[set] = set() - x: ClassVar[Set[Any]] = set() -- ___ Python tracker <https://bugs.python.org/issue33094> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue33100] dataclasses and __slots__ - non-default argument (member_descriptor)
Adrian Stachlewski added the comment: I don't really get your point. @dataclass class Base: __slots__ = ('x',) x: Any This case is described in PEP 557 as correct, so I don't understand why you want to generate error. Also inheritance without defining slots is correct as stated in data model. In my opinion, member_descriptor should be treated same as MISSING and everything should work correctly. -- ___ Python tracker <https://bugs.python.org/issue33100> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue33100] dataclasses and __slots__ - non-default argument (member_descriptor)
Adrian Stachlewski added the comment: There's also another major problem. Because Base.x has value, __init__ is not prepared correctly (member_descriptor is passed as default). @dataclass class Base: __slots__ = ('x',) x: Any Base() # No TypeError exception Fixing this should be quite easy, if you want I can prepare PR. -- ___ Python tracker <https://bugs.python.org/issue33100> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue33094] dataclasses: ClassVar attributes are not working properly
Adrian Stachlewski added the comment: There's nothing to do, thanks for help one more again. -- status: pending -> open ___ Python tracker <https://bugs.python.org/issue33094> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com