Skip to content
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

Support rename=True in collections.namedtuple #17247

Merged
merged 5 commits into from
May 18, 2024

Conversation

JelleZijlstra
Copy link
Member

JelleZijlstra and others added 2 commits May 14, 2024 21:18
A pretty marginal feature but it's now tested in the typing conformance suite,
and it was easy to add support.
@@ -125,6 +128,8 @@ E = namedtuple('E', 'a b', 0)
[builtins fixtures/bool.pyi]

[out]
main:4: error: Boolean literal expected as the "rename" argument to namedtuple()
main:5: error: Boolean literal expected as the "rename" argument to namedtuple()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A little unfortunate that we give two errors for the same line, but the same happens with T = TypeVar("T", covariant="True"); I don't think it's worth the effort to suppress one of the two.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think its fine to keep both errors but it would be better if both errors have the same code so that they can be disabled with a single type ignore comment. Currently the errors are:

nt.py:5: error: Boolean literal expected as the "rename" argument to namedtuple()  [misc]
nt.py:5: error: Argument "rename" to "namedtuple" has incompatible type "str"; expected "bool"  [arg-type]

How about giving the first error the code arg-type as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, done.

This comment has been minimized.

Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Copy link
Collaborator

@hamdanal hamdanal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, as far as I can tell this now mirrors the runtime behavior.

@JelleZijlstra JelleZijlstra merged commit 828c0be into python:master May 18, 2024
18 checks passed
@JelleZijlstra JelleZijlstra deleted the ntrename branch May 18, 2024 21:14
@JelleZijlstra JelleZijlstra restored the ntrename branch September 10, 2024 23:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants