Skip to content

Conversation

@johnslavik
Copy link
Contributor

@johnslavik johnslavik commented Jan 27, 2025

Clos​es gh-85795
Clos​es gh-129343

I'm open to criticism, especially the documentation part.
I'm elaborating on the solution in further comments.

I'll add the news entry quickly.


📚 Documentation preview 📚: https://cpython-previews--129344.org.readthedocs.build/

@johnslavik johnslavik changed the title gh-85795: Fix __classcell__, support overriding __repr__ easily gh-85795: Fix typing.NamedTuple __classcell__ bug, support overriding __repr__ easily Jan 27, 2025
@johnslavik johnslavik force-pushed the fix-classcell-bug-and-overriding-repr branch from 96af8ac to dcdab5f Compare January 27, 2025 11:49
@johnslavik
Copy link
Contributor Author

johnslavik commented Jan 27, 2025

(Sorry for the force push, but this one was a very tricky and subtle git mistake of mine and I prefer not confusing y'all)

@johnslavik
Copy link
Contributor Author

johnslavik commented Jan 27, 2025

A couple notes on the solution:

I added a private parameter _classcell to collections.namedtuple() and its wrappers in typing in order to deliver it to the targeted type.__new__.
It's the easiest approach here.

I also pop __classcell__ from the original class namespace, as it would otherwise leak to the end class (metaclasses should clean up the __classcell__ attribute).

I expect it is not added to typeshed, as it is only intended for internal use.

I expect _repr added to typeshed though and offer a PR for it upon acceptance of this solution.

@johnslavik
Copy link
Contributor Author

If you disagree with the addition of _repr to named tuples, we can split this PR. Just let me know.

@AlexWaygood
Copy link
Member

AlexWaygood commented Jan 27, 2025

If you disagree with the addition of _repr to named tuples, we can split this PR. Just let me know.

It would make sense to me to split this PR into two. I'm not necessarily saying I disagree with the _repr addition, but it seems less clearly a bugfix than the __classcell__ thing. Small, incremental PRs are also easier for us to review :-)

@johnslavik
Copy link
Contributor Author

@AlexWaygood Thanks, you're right. I should have split that in the first place. I'll work through all the possible CI problems and then proceed to split it.

@johnslavik
Copy link
Contributor Author

This looks super-bad!
image

@johnslavik
Copy link
Contributor Author

Closing this one and opening two new PRs, as concluded in #129344 (comment).

@johnslavik johnslavik closed this Jan 27, 2025
@johnslavik johnslavik deleted the fix-classcell-bug-and-overriding-repr branch December 2, 2025 00:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants