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 |
## 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.
see the mr description, it's implemented in pycharm and basedpyright |
94b1653 to
7a4c793
Compare
b001edf to
5f531ef
Compare
7c7dc6a to
34c9c0d
Compare
1d4ba59 to
424faf1
Compare
jorenham
left a comment
There was a problem hiding this comment.
The lack of co/contra variance for TypeVarTuples has caused problems for me on several occasions in the past. So thanks for patching this hole in the spec!
The changes to the variance algorithm also look like the way to go. It's too bad we don't have a general notion of the "top signature" like we do for the general top type (object) and the top product type (tuple[object, ...]). But I expect that the way you informally describe it here will be understood just fine by anyone that needs to know.
424faf1 to
37c324a
Compare
|
@carljm good to review now |
carljm
left a comment
There was a problem hiding this comment.
The direction here remains correct IMO, but I think there are some updates needed.
| def f(self, *args: InP.args, **kwargs: InP.kwargs): ... | ||
|
|
||
| in_obj: ContravariantParamSpec[object] = ContravariantParamSpec[int]() # E | ||
| in_int: ContravariantParamSpec[int] = ContravariantParamSpec[object]() # OK |
There was a problem hiding this comment.
All of the ParamSpec specializations here have one required positional parameter. Could we add cases involving keyword-only or positional-only parameters, parameter names, defaults, and differing callable arities, perhaps also Concatenate? Otherwise an implementation that compares only a tuple of parameter types could pass without implementing actual signature assignability.
There was a problem hiding this comment.
without the ability to denote any of these forms (apart from arity), i'm having trouble find a way to test them
There was a problem hiding this comment.
okay, i think i managed to get some of these cases using an invariant box
| constructor call. No further inference is needed. | ||
|
|
||
| 3. Create two specialized versions of the class. We'll refer to these as | ||
| 2. Create two specialized versions of the class. We'll refer to these as |
There was a problem hiding this comment.
What should the "dummy type instance" be when one of the other parameters is a TypeVarTuple or ParamSpec? An ordinary dummy class isn't a valid specialization for either kind. Could we spell out kind-compatible substitutions and cover a mixed example such as:
class Mixed[T, *Ts, **P]:
def f(self, x: T, /, *args: P.args, **kwargs: P.kwargs) -> tuple[*Ts]: ...I would expect T and P to be contravariant and Ts to be covariant, independently.
74c5067 to
5e5e560
Compare
…should have variance
5e5e560 to
39c64a1
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