Skip to content

Support rename=True in collections.namedtuple - #17247

Merged
JelleZijlstra merged 5 commits into
python:masterfrom
JelleZijlstra:ntrename
May 18, 2024
Merged

JelleZijlstra merged 5 commits into
python:masterfrom
JelleZijlstra:ntrename

Conversation

@JelleZijlstra

Copy link
Copy Markdown
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.

[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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

@github-actions

This comment has been minimized.

@github-actions

Copy link
Copy Markdown
Contributor

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

@hamdanal hamdanal left a comment

Copy link
Copy Markdown
Collaborator

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
@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