parameter specification/type variable tuple variance#2215
Conversation
5f8e0cc to
d9d82c9
Compare
81ba400 to
54ad5e1
Compare
JelleZijlstra
left a comment
There was a problem hiding this comment.
I like adding this if we can get it specified nicely, but this PR is not ready; the proposed test is incorrect.
Also, https://typing.python.org/en/latest/spec/generics.html#paramspec-variables still says variance on ParamSpec is unsupported; this should be updated.
I'd also like to see an implementation in at least one type checker, even if only as a draft PR, so we can be confident this is something that can be feasibly implemented.
|
I personally would also like to see tests added for param spec variance inference, since that would probably need to be supported as well. |
|
The test cases do have variance inference, though as I noted some of the cases are wrong. But it would probably be useful to have a few more cases, and I'd recommend putting the tests for paramspec variance in their own file so we can track type checker support more precisely. |
I have wip support in PyCharm, but I could also add it to basedpyright it was very straightforward to implement |
Oh right, sorry I didn't see them, because I thought they would be in a different file. I'm also very much +1 on putting those tests into a different file (for example |
8f30004 to
117964e
Compare
7a21592 to
5788125
Compare
|
support has landed in pycharm and cpython |
|
https://typing.python.org/en/latest/spec/generics.html#paramspec-variables stills says that we don't support variance in ParamSpec, that should be fixed in this PR. |
|
Also can you open an issue on python/typing-council asking for a formal pronouncement? |
5788125 to
f23d8ae
Compare
|
Noticed some more issues:
|
not |
414c697 to
02c4628
Compare
|
I don't think that's right. The form we want here should be a supertype of every other possible value, and something like a one-parameter callable is not a subtype of |
|
In ty we have a type that we spell as |
ec71bfe to
7cfeee0
Compare
7cfeee0 to
1d3e04d
Compare
## Summary This PR adds `covariant`, `contravariant`, and `infer_variance` support to `ParamSpec`. See: python/typing#2215. See: #24479 (review).
|
i've pushed changes, it's good to review again |
JelleZijlstra
left a comment
There was a problem hiding this comment.
Agree with this idea, we might have to tweak the exact PR a bit more.
## Summary This PR adds `covariant`, `contravariant`, and `infer_variance` support to `ParamSpec`. See: python/typing#2215. See: astral-sh#24479 (review).
davidhalter
left a comment
There was a problem hiding this comment.
I like the general approach. I think it would also be useful to have at least one type checker with a proper implementation of this new feature.
|
|
||
| an ``object`` instance for a type variable. | ||
| a ``*tuple[object, ...]`` value for a type variable tuple. | ||
| a for a parameter specification, a 'top signature' value, i.e. a type |
There was a problem hiding this comment.
I assume a for a is not wanted? The whole paragraph feels a bit weird and the formatting might be a bit off.
There was a problem hiding this comment.
addressed points 1 and 3. do you have a suggestion on how to improve the wording?
There was a problem hiding this comment.
I would probably go with a list here:
| a for a parameter specification, a 'top signature' value, i.e. a type | |
| - an ``object`` instance for a type variable | |
| - a ``*tuple[object, ...]`` value for a type variable tuple | |
| - a "top signature" value for a parameter specification, i.e. a type that represents the super type of every possible signature. |
see the mr description, it's implemented in pycharm and basedpyright |
fdb31bb to
94b1653
Compare
fixup! fixup! specify that parameter specification should have variance
94b1653 to
7a4c793
Compare
| out_obj: CovariantTypeVarTuple[object] = CovariantTypeVarTuple[int]() # OK | ||
| out_multiple: CovariantTypeVarTuple[float, float] = CovariantTypeVarTuple[ | ||
| int, # OK | ||
| object, # E |
There was a problem hiding this comment.
I don't think we should require that the error be reported on this exact line. Generally, the conformance tests allow an error to be reported on any reasonable line. Furthermore, I tested this snippet:
from typing import Generic, TypeVar
T1 = TypeVar("T1", covariant=True)
T2 = TypeVar("T2", covariant=True)
class C(Generic[T1, T2]): pass
x: C[float, float] = C[
int,
object,
]()
in mypy, pyright, pyrefly, and ty, and they all reported the error either on the first line of x's definition or on the entire definition, which presumably the current conformance tests accept, so it would be odd to have a different requirement for TypeVarTuple than for TypeVar.
There was a problem hiding this comment.
okay, addressed with a matrix of four assignments
b001edf to
5f531ef
Compare
997d9a6 to
7c7dc6a
Compare
7c7dc6a to
34c9c0d
Compare
discussion: https://discuss.python.org/t/parameter-specifications-should-have-variance/106452
typing.TypeVarTuplecpython#148212typing.TypeVarTuple: add bound/variance properties from 3.15 typeshed#15670 / Add Python 3.15 typing updates typeshed#15725