Skip to content

parameter specification/type variable tuple variance - #2215

Merged
JelleZijlstra merged 7 commits into
python:mainfrom
KotlinIsland:paramspec-variance
Aug 13, 2026
Merged

parameter specification/type variable tuple variance#2215
JelleZijlstra merged 7 commits into
python:mainfrom
KotlinIsland:paramspec-variance

Conversation

@KotlinIsland
KotlinIsland force-pushed the paramspec-variance branch 3 times, most recently from 5f8e0cc to d9d82c9 Compare March 9, 2026 03:13
@srittau srittau added the topic: typing spec For improving the typing spec label Mar 9, 2026
@KotlinIsland
KotlinIsland force-pushed the paramspec-variance branch 2 times, most recently from 81ba400 to 54ad5e1 Compare April 1, 2026 07:59

@JelleZijlstra JelleZijlstra left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread conformance/tests/generics_paramspec_semantics.py Outdated
Comment thread conformance/tests/generics_paramspec_semantics.py Outdated
@davidhalter

Copy link
Copy Markdown
Collaborator

I personally would also like to see tests added for param spec variance inference, since that would probably need to be supported as well.

@JelleZijlstra

Copy link
Copy Markdown
Member

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.

@KotlinIsland

KotlinIsland commented Apr 3, 2026

Copy link
Copy Markdown
Contributor Author

I'd also like to see an implementation in at least one type checker

I have wip support in PyCharm, but I could also add it to basedpyright

it was very straightforward to implement

@davidhalter

davidhalter commented Apr 3, 2026

Copy link
Copy Markdown
Collaborator

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.

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 generics_paramspec_variance.py).

@KotlinIsland
KotlinIsland force-pushed the paramspec-variance branch 2 times, most recently from 8f30004 to 117964e Compare April 7, 2026 01:29
@KotlinIsland KotlinIsland changed the title parameter specification variance parameter specification/type variable tuple variance Apr 7, 2026
@KotlinIsland

Copy link
Copy Markdown
Contributor Author

support has landed in pycharm and cpython

@JelleZijlstra

Copy link
Copy Markdown
Member

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.

@JelleZijlstra

Copy link
Copy Markdown
Member

Also can you open an issue on python/typing-council asking for a formal pronouncement?

@KotlinIsland

Copy link
Copy Markdown
Contributor Author

python/typing-council#59

@JelleZijlstra

Copy link
Copy Markdown
Member

Noticed some more issues:

  • generics_typevartuple_basic.py still has a line Ts1 = TypeVarTuple("Ts1", covariant=True) # E. But of course that's legal now. I'd delete the line and leave all variance testing to the new file generics_typevartuple_variance.py.
  • The description of the variance inference algorithm says "In the upper specialized class, specialize the target type parameter with an object instance". But that doesn't make sense for ParamSpec and TypeVarTuple. I think TypeVarTuple should instead be specialized with *tuple[object, ...]. For ParamSpec perhaps Callable[..., object] is right but not 100% sure.
  • The way the tests are written produces some incidental errors, like from ty generics_paramspec_variance.py:28:23: error[empty-body] Function always implicitly returns `None`, which is not assignable to return type `(...) -> Unknown` and mypy generics_typevartuple_variance.py:18: error: Missing return statement [empty-body]. Should add something like raise NotImplementedError.

@KotlinIsland

KotlinIsland commented Apr 23, 2026

Copy link
Copy Markdown
Contributor Author

Callable[..., object] is right but not 100% sure.

not Callable, and not ..., as this is the gradual form. but (*object, **object), which is a non-denotable psuedo representation of the widest value for a parameter specification

@JelleZijlstra

Copy link
Copy Markdown
Member

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 (*: object, **: object).

@AlexWaygood

Copy link
Copy Markdown
Member

In ty we have a type that we spell as Top[Callable[..., object]] in ty's type display (a fully static type describing the infinite union of all possible Callable types). It's not representable using standard Python notation currently.

@KotlinIsland
KotlinIsland force-pushed the paramspec-variance branch 3 times, most recently from 1d4ba59 to 424faf1 Compare July 14, 2026 05:56

@jorenham jorenham left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@KotlinIsland

Copy link
Copy Markdown
Contributor Author

@carljm good to review now

@KotlinIsland
KotlinIsland requested a review from carljm August 6, 2026 22:25

@carljm carljm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The direction here remains correct IMO, but I think there are some updates needed.

Comment thread conformance/results/basilisk/generics_paramspec_variance.toml Outdated
Comment thread conformance/results/pycroscope/generics_typevartuple_basic.toml Outdated
Comment thread conformance/tests/generics_typevartuple_variance.py Outdated
Comment thread conformance/tests/generics_typevartuple_variance.py Outdated
Comment thread conformance/tests/generics_typevartuple_variance.py
Comment thread docs/spec/generics.rst
Comment thread conformance/tests/generics_paramspec_variance.py Outdated
Comment thread conformance/tests/generics_paramspec_variance.py
Comment thread docs/spec/generics.rst
Comment thread conformance/tests/generics_typevartuple_variance.py
@KotlinIsland
KotlinIsland force-pushed the paramspec-variance branch 2 times, most recently from 5e5e560 to 39c64a1 Compare August 7, 2026 05:49
@KotlinIsland
KotlinIsland requested a review from carljm August 8, 2026 02:41

@carljm carljm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Some things still need attention.

Comment thread conformance/tests/generics_paramspec_variance.py Outdated
Comment thread conformance/tests/generics_paramspec_variance.py Outdated
Comment thread conformance/tests/generics_paramspec_variance.py Outdated
Comment thread docs/spec/generics.rst
Comment thread conformance/tests/generics_paramspec_variance.py Outdated
Comment thread conformance/results/ty/generics_typevartuple_variance.toml Outdated
Comment thread conformance/tests/generics_paramspec_variance.py Outdated
Comment thread conformance/tests/generics_typevartuple_variance.py Outdated
Comment thread docs/spec/generics.rst Outdated
Comment thread docs/spec/generics.rst Outdated
@KotlinIsland
KotlinIsland force-pushed the paramspec-variance branch 2 times, most recently from e24ce3c to e0d06d5 Compare August 12, 2026 22:32
@KotlinIsland
KotlinIsland requested a review from carljm August 12, 2026 22:33

@carljm carljm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI says conformance results need updating; can you do that and then re-request review?

@carljm carljm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I pushed a few additional small changes (in separate commits so you can review each change if you like) -- this now looks good to me.

The most notable change is that I found three more places where the conformance suite used an implicitly-invariant ParamSpec in a location where it should be contravariant; thus a compliant checker should error on those uses, per the current spec / conformance suite. Fixing these involved a choice between unpalatable options:

  1. The most obvious fix is to make those ParamSpecs explicitly contravariant=True, but this errors on four type checkers (mypy, pyright, zuban, pyrefly) which don't yet allow ParamSpecs to use that argument, making those type checkers fail on conformance tests that aren't really related to ParamSpec variance.
  2. Another option is to define those ParamSpecs using PEP 695 syntax, which automatically opts into inferred variance, and allows those tests to stay focused on their core purpose. Unfortunately this seems to expose a crash in pycroscope.

I've gone with option (2) for now. @JelleZijlstra let me know if you are OK with landing this for now, would like to propose another alternative, or would like time to fix the pycroscope crash before we land this.

@JelleZijlstra

Copy link
Copy Markdown
Member

Let's land it, I think I need to do a round of fixing bugs in pycroscope :)

@JelleZijlstra
JelleZijlstra merged commit 97b98ba into python:main Aug 13, 2026
5 checks passed
@KotlinIsland
KotlinIsland deleted the paramspec-variance branch August 13, 2026 07:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic: typing spec For improving the typing spec

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants