-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-85795: Add support for super()
in NamedTuple
subclasses
#129352
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
Conversation
This needs a backport to 3.12 and 3.13. |
Changed my mind, let's not backport it :)
|
CPython disallows class Foo:
__classcell__ = None but this implementation allows from typing import NamedTuple
class Foo(NamedTuple):
__classcell__ = None should I pop from the origin class namespace with a sentinel object fallback instead of Errors should never pass silently, unless explicitly silenced. |
Ideally I feel like this fix would not involve adding a new parameter to
It seems like an unlikely situation, but I suppose this would be strictly-speaking more correct! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly LGTM, but I have a few thoughts.
I like @ZeroIntensity's idea of moving |
20bfaeb
to
074df4c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice. This LGTM now.
oh, except some tests are failing |
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
0d6dcbb
to
ee93519
Compare
Hah! How ignorant of me. I added another frame by wrapping the function and didn't fixup the existing frame refs ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, with one tiny suggestion.
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Thanks @AlexWaygood and @ZeroIntensity for working through this and finding a better approach! One last thought is whether I should test cpython/Lib/test/test_super.py Lines 262 to 273 in c931d75
I'm leaving this decision to @rhettinger since he self-assigned this. I personally believe a possible regression allowing nonsense overwrites of __classcell__ in typing.NamedTuple subclasses is not harmful by any means and niche enough that and I wouldn't care about that case.
@rhettinger Please review. :) |
I don't think this is worth it and instead prefer Alex's suggestion "to document that super() doesn't work inside methods on typing.NamedTuples created using the class syntax". Named tuples are thin classes, there isn't much in the way of reusable code. Likely, that is why overriding rather than extending is the norm. Also dataclasses seem to be preferred when people are trying to do anything more interesting. I do admire the analysis and effort that was put into this PR. The code looks correct. However, I think almost no one needs this. The edit is a really interesting (and brilliant) but slightly icky magic trick that isn't that easy to explain. Neither FWIW, I'm only |
@rhettinger Thank you. I must say I agree with you for the most part, I also don't feel too strong about the general necessity for this to work, especially since How about I make a PR to detect classcells and raise a proper, easy to comprehend error message like "using |
+1 That would tremendously improve the user experience. |
I'm therefore closing this PR and will create another one with the improved error message. Thank y'all for reviews, this bug was very fun to work on, so no regrets though it didn't make a cut :) |
Fixes gh-85795.A private parameter
_classcell
was added tocollections.namedtuple()
and its wrappers intyping
in order to deliver it to the targetedtype.__new__
.It's the easiest approach here.
__classcell__
is popped from the original class namespace, as it would otherwise leak to the end class (metaclasses should clean up the__classcell__
attribute).It is expected not to be added to typeshed, as the parameter is intended for internal use only.
After this PR, please consider a follow-up issue gh-129343 and the PR associated with it.
📚 Documentation preview 📚: https://cpython-previews--129352.org.readthedocs.build/