Skip to content
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

Generic with ParamSpec and Concatenate substitutions behave incorrectly #492

Closed
Daraan opened this issue Oct 23, 2024 · 3 comments · Fixed by #489
Closed

Generic with ParamSpec and Concatenate substitutions behave incorrectly #492

Daraan opened this issue Oct 23, 2024 · 3 comments · Fixed by #489

Comments

@Daraan
Copy link
Contributor

Daraan commented Oct 23, 2024

The following code displays some incorrect behavior, but different parts do not work in certain ranges of 3.8-3.11.3

  • 3.8 & 3.9 substitution of Concatenate
  • 3.10 does not correctly collect type parameters
  • < 3.11.3 wrong substitution behavior; type parameter not removed
from typing import Generic, TypeVar
from typing_extensions import ParamSpec, Concatenate
P = ParamSpec('P')
T = TypeVar("T")

class CLS(Generic[P]): ...

Y = CLS[T] # or with a ParamSpec
# 3.10
Y[str]  # raises TypeError is not a generic class

# 3.8 & 3.9
G9 = CLS[Concatenate[T, P]]
H9 = G9[int, [T]] # should be ok; but TypeError
H9[str]  # should also work

# 3.11.0
G8 = CLS[Concatenate[T, ...]]
H1 = G8[int]
H1[str]  # for python 3.11.0-3 this still has a parameter and does not raise a TypeError

This is a variant of #126 but with generics which require different fixes. There are multiple flavors of this issue depending on the version parameters are not recognized or not substituted correctly.

For a more comprehensive list check the skipped tests of #491 here


It's possible that some substitutions with Concatenate worked prior to #479 and caused a regression here, but I am not yet certain.


Fixes come with

@jeertmans
Copy link

jeertmans commented Nov 29, 2024

Not sure if this is directly related, but Python 3.11 added support for ellipsis ... as last parameter in Concatenate. In Python 3.10, this was not possible (only ParamSpec was allowed).

It seems that typing_extensions provides a 3.10-compatible version of Concatenate when Python < 3.11, which means I cannot use Concatenate[Foo, ...] unless my library's minimal supported Python version is 3.11.

In other words, I was planning to do that:

if sys.version_info >= (3, 11):
    from typing import Concatenate
else:
    from typing_extension import Concatenate

Bar = Concatenate[Foo, ...]

but this doesn't work

@Daraan
Copy link
Contributor Author

Daraan commented Nov 29, 2024

Not sure if this is directly related, but Python 3.11 added support for ellipsis ... as last parameter in Concatenate. In Python 3.10, this was not possible (only ParamSpec was allowed).

The issue in question for this problem is #110 to address this problem for 3.10. A few weeks ago we added #481 to main which solves this problem, but note that there is no release yet.

@jeertmans
Copy link

Oh, thanks for pointing to the correct issue :-)

Not sure if this is directly related, but Python 3.11 added support for ellipsis ... as last parameter in Concatenate. In Python 3.10, this was not possible (only ParamSpec was allowed).

The issue in question for this problem is #110 to address this problem for 3.10. A few weeks ago we added #481 to main which solves this problem, but note that there is no release yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants