-
Notifications
You must be signed in to change notification settings - Fork 258
Clarify namedtuple member rules #1979
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
base: main
Are you sure you want to change the base?
Conversation
Yes, please start a thread in the discussion forum. I don't think these changes will be controversial, but we should let the community weigh in. |
Hi, I'm not sure if this is the right place for this discussion—if not, please let me know (and sorry!). I originally raised this issue in pyright, but I think here might be a more appropriate place to discuss it. Suppose I do have members that start with NT5 = namedtuple("NT5", ["abc", "_1"], rename=True) # OK
NT5(abc="", _1=1) # OK How can I provide type annotations for them in a class NT5(NamedTuple):
abc: str
_1: int # <- HOW ?
|
I'm with Eric on the linked issue that this doesn't make sense to support. If you use different types on both arms of
No, unless the runtime has that exemption. We should match the runtime behavior.
Type checkers could do that but I don't think it should be the spec's business to decide how type checkers should proceed once they've detected a type error. |
And to give a constructive suggestion for your use case: Maybe just don't use namedtuple? If you require names that start with an underscore, it's not the right tool for the job. |
Fair enough! Thanks for your quick response! 👍
It would be nice if
I can't control the code on the other end; I can only provide type annotations for it (like in a The workaround I've come up with for now is: class NT5(tuple[str, int]):
abc: str
_1: int
__match_args__ = ("abc", "_1")
def __new__(cls, abc: str, _1: int) -> Self: ... But this isn't as straightforward as Anyway, thanks for your help! 🙏 |
This PR clarifies the typing spec around named tuple fields and adds a few conformance test cases:
rename
)Analogous restrictions are outlined in the spec for enums, but they are not present in the spec for named tuples.
Since this is adding new rules to the spec, should I open a discussion in the forum about it?