New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dataclasses and __slots__ - non-default argument (member_descriptor) #77281
Comments
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. |
This is the same reason that this fails: class Base:
__slots__ = ('x',)
x = 3 with: In the dataclasses case, the error needs to be improved, and moved to when the base class is being defined. |
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. |
My point is that the problem is that after: @dataclass
class Base:
__slots__ = ('x',)
x: Any Base.x has a value (it's the member_descriptor for x). That's what's causing the problem that when adding a field to the derived class, it thinks you're adding a field without a default value after one that has a default value. I agree that I could detect this specific case and allow it. My comment about the error message was inaccurate. |
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. |
Thanks, but I'm already looking at this in the context of a different bug. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: