-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Conversation
A pretty marginal feature but it's now tested in the typing conformance suite, and it was easy to add support.
for more information, see https://pre-commit.ci
@@ -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() |
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.
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.
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.
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?
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.
Good idea, done.
This comment has been minimized.
This comment has been minimized.
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
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.
Thank you, as far as I can tell this now mirrors the runtime behavior.
A pretty marginal feature but it's now tested in the typing conformance suite,
and it was easy to add support.
For reference: https://github.com/python/typing/blob/9f7f400bb7c4c79f1fb938402e0bb3198dac0054/conformance/tests/namedtuples_define_functional.py#L46, https://github.com/python/cpython/blob/7d8725ac6f3304677d71dabdb7c184e98a62d864/Lib/collections/__init__.py#L389