-
Notifications
You must be signed in to change notification settings - Fork 290
conformance: change generics_typevartuple_args.py case #2203
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
base: main
Are you sure you want to change the base?
Changes from all commits
1ff577f
ed33b9c
dfef23c
1a9dac5
b08e02f
9dcab0e
5841fb4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -54,7 +54,7 @@ def __init__(self, shape: tuple[Shape]): # E: not unpacked | |
| self._shape: tuple[*Shape] = shape | ||
|
|
||
| def get_shape(self) -> tuple[Shape]: # E: not unpacked | ||
| return self._shape | ||
| raise NotImplementedError | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was causing mypy to emit an error because it resolves the invalid |
||
|
|
||
| def method1(*args: Shape) -> None: # E: not unpacked | ||
| ... | ||
|
|
@@ -86,7 +86,7 @@ def func2(arg1: tuple[*Ts], arg2: tuple[*Ts]) -> tuple[*Ts]: | |
| func2((0.0,), (0,)) # OK | ||
| func2((0,), (1,)) # OK | ||
|
|
||
| func2((0,), ("0",)) # E | ||
| func2((0,), ("0",)) # OK | ||
| func2((0, 0), (0,)) # E | ||
|
|
||
|
|
||
|
|
||
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.
To me it seems most consistent with how typevar solving works everywhere else in the type system to actually specify that this should not be an error. It looks like that would match the behavior of mypy and zuban, but not of pyrefly or pyright.
If there's opposition to that change, I would still probably favor leaving this in, with E? and a comment -- that seems more useful to future discussion of this case.
(As far as I can see from a quick scan, this behavior was not specified in the PEP.)
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.
This comes from this section of the spec, which was copied from the PEP. This section was added to the PEP late in the process — after I had reviewed the PEP and implemented it in pyright. I was very unhappy when I learned about the addition because, as you point out, it introduces an inconsistency with the way that TypeVar solving works in other cases.
Line 76 of the conformance test is arguably different than the special case spelled out in the spec, which says "If the same TypeVarTuple instance is used in multiple places".
Tsappears only once in the signature offunc4, but because it is defining the type of a variadic parameter,Tsappears multiple times from the perspective of a constraint solver. Constraints are supplied for each argument that maps to that variadic parameter.Consider the following example. The spec clearly dictates that the call to func3 is an error. I think the call to func4 should likewise be an error.
So, unless we amend the spec, I think the conformance test should remain as is (with the possible addition of a better comment to explain the above).
I'm in favor of modifying the spec and removing this entire section.
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.
Ah, thanks @erictraut -- I missed that section when scanning the PEP text. I agree that that section of the PEP/spec text is applicable here, and also that it's unfortunately inconsistent with the usual behavior of Python typevar solving. I'm not sure any of the PEP authors are still active in Python typing, or we could ask what motivated that late addition to the PEP text.
There are use cases for stricter typevar solving that doesn't ever synthesize wider types, but those use cases exist for regular typevars too, not just for variadic ones. So it seems better to go for consistency, and introduce a new orthogonal type system feature for stricter solving. (One possible shape for that feature is a
NoInferqualifier -- though it's not clear how that could be used in the*argscase.)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.
Thanks for the background! I have now amended this PR to propose changing the spec to remove this rule. I will post on Discuss before opening a vote for the Typing Council.