Do we need to add invariant tuple matching to the spec? #2238
Replies: 2 comments 2 replies
-
|
I might be misunderstanding, but I think the behaviour here follows from the specification in https://typing.python.org/en/latest/spec/tuples.html#type-compatibility-rules and that mypy, ty, pyrefly get this right and pyright gets it wrong |
Beta Was this translation helpful? Give feedback.
-
|
I think this is already clearly specified at https://typing.python.org/en/latest/spec/tuples.html#tuple-type-form This just looks like a pyright bug to me, I don't think any new specification is needed. def bare(x: tuple[int, int], y: tuple[int, ...]):
x = y # everyone agrees this is an error
y = x # everyone agrees this is not an error
def in_list(x: list[tuple[int, int]], y: list[tuple[int, ...]]):
x = y # everyone agrees this is an error
y = x # everyone but pyright agrees this is an error("everyone" above means "mypy/pyright/ty/pyrefly/zuban") The fact that either of lines 2 or 3 is an error (meaning the two tuple types are not equivalent, or even mutually assignable gradual types) is sufficient to say definitely that both lines 6 and 7 must be an error, due to invariance of list. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to figure out how to implement invariant
tuple[int, ...] ≡ tuple[int, int]matching. Pyright and Mypy disagree and IMO it's a prime candidate to add to the spec (I'm not 100% sure this is not specified, but it feels like it). I like Mypy better here, but would be happy if this was added to the spec either way.Am I missing something? Which direction should we take? I discovered this problem with type var tuples, where this can also cause issues when matching for example
X[Unpack[Tuple[int, ...]]] ≡ X[int, int]. I personally think it's very annoying for library authors if type checkers do this in a different way. Also note that there's a different between invariant matching oftuple[int, ...]andtuple[Any, ...]in Mypy.Mypy:
Pyright: No issues
Beta Was this translation helpful? Give feedback.
All reactions