typing_extensions icon indicating copy to clipboard operation
typing_extensions copied to clipboard

`Concatenate` implementation generates runtime exception if parameterized by `...`

Open erictraut opened this issue 3 years ago • 3 comments

The following code should work without a runtime exception.

from typing import Any, Callable
from typing_extensions import ParamSpec, Concatenate


P = ParamSpec("P")
MyAlias = Callable[Concatenate[int, P], Any]

x: MyAlias[...]

It runs fine on Python 3.10 if using typing.Concatenate but fails on Python 3.9 if using typing_extensions.Concatenate. The runtime exception is:

TypeError: Parameters to generic types must be types. Got Ellipsis.

erictraut avatar Jun 09 '22 19:06 erictraut

This seems like a bug in Python's typing module itself.

# In 3.9
typing.Callable[T, int][...]

TypeError: Parameters to generic types must be types. Got Ellipsis.

The problem is that Callable uses the default _GenericAlias's substitution behavior pre 3.10. Which means its checks are too restrictive. We had to intentionally loosen the checks in 3.10 to get around this.

I don't think we can fix this from typing_extensions unless we monkey patch typing._GenericAlias.__getitem__ (or typing._type_check). I don't feel too good doing that though.

Fidget-Spinner avatar Jul 07 '22 14:07 Fidget-Spinner

Also, we're sadly past the stage for bugfixes in 3.9. I suggest that we add this to the known limitations in the README and (unfortunately) close as can't fix.

Fidget-Spinner avatar Jul 07 '22 14:07 Fidget-Spinner

This is now documented as a limitation at https://typing-extensions.readthedocs.io/en/latest/#Concatenate. If there is a reasonable way to fix it, I'd accept a PR.

JelleZijlstra avatar May 22 '23 00:05 JelleZijlstra

I am currently looking at this again and the statement that "It runs fine on Python 3.10 if using typing.Concatenate" confused be a bit. In hindsight this should only hold for 3.10.0-3.10.2 afterwards it also does not run on the other 3.10 typing versions either.

~~Edit: Monkey patching copy_with of _ConcatenateGenericAlias to the 3.11 variant would be sufficient for the 3.10 versions.~~ Edit2: Also doable without monkey patch in 3.10.

Daraan avatar Oct 03 '24 10:10 Daraan