-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
typing: Unexpected result with value of instance of class inherited from typing.NamedTuple #77258
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
Comments
Overwriting of default values not working, and used default value of base class. Unittest file if attachment described a problem. |
Thanks Евгений Махмудов for the report! The crux is this: class A(NamedTuple):
value: bool = True
class B(A):
value: bool = False B(True).value # Expected True, but is False If we add NamedTuple to B's bases or make its metaclass NamedTupleMeta, it works as expected. Introspecting the classes a bit more suggests a cause: the class variable A.value is a <property ...>, but B.value is just False, and adding the extra base class or metaclass corrects this. Ivan, you can probably tell what's wrong from this. Maybe it's hard to fix, because NamedTuple doesn't appear in A.__mro__? (IIRC there was a question about that somewhere recently too?) |
Yes, this is because subclassing |
I wonder if it's too late to conclude that NamedTuple in this context |
I once thought of building NamedTuple functionality into dataclasses, either as a parameter to @DataClass or as a new decorator. But it didn't get very far. It would have to return a different class, like NamedTuple does, and this didn't fit in well with the original "keep @DataClass simple" mantra. It would also be especially confusing with frozen already existing. |
I would say it is too late. (Also a camel case decorator would look weird.) |
Apart from the fact that it's too late, if you had to do it over again, Anyway, let's keep this issue open but reclassify it as a docs issue. |
Yes, this could be done as a decorator which would replace the original class with a named tuple after inspecting |
Would it be worthwhile to show an example of a subclass that overrides or extends __new__? Elsewhere in Python, the usual technique for changing method defaults is for a subclass to override or extend the method in question. class A:
def somemeth(self, value: bool = True):
print(value)
class B(A):
def somemeth(self, value: bool = False):
super().somemeth(value) |
I think yes. I would actually add few examples what could (and maybe also couldn't) be done with named tuples. |
This still reproduces on 3.11. #90130 covers similar confusing NamedTuple behavior. |
I thought the conclusion was to just document this? And that other issue seems something quite unrelated. |
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: