mypy icon indicating copy to clipboard operation
mypy copied to clipboard

(🐞) List literals are accepted as valid types when applying a `ParamSpec`

Open KotlinIsland opened this issue 3 years ago • 2 comments

P = ParamSpec("P")
T = TypeVar("T")
class C(Generic[P]): ...
class G(Generic[T]): ...
C[[[int]]]  # no error  # C[[[int]]]
G[[[int]]]  # error

KotlinIsland avatar Aug 01 '22 02:08 KotlinIsland

I would consider these to be a bug as well:

    reveal_type(C[int])  # Revealed type is "C[[builtins.int]]"
    reveal_type(C[int, bytes, float])  # Revealed type is "C[[builtins.int, builtins.bytes, builtins.float]]"

There should always be exactly one list literal to do type application.

davidhalter avatar Jan 06 '23 12:01 davidhalter

Nevermind, the PEP 612 actually says that C[int, bytes, float] should be fine for C[P] for readability reasons. While I feel like that's kind of ok, I feel like it's very strange that:

class D[P, T]: ...
# Fails:
D[int, bytes, str]
# Works
D[int, bytes]  # Type is D[[int, bytes]]

I feel like at least the latter is an extremely weird case and should probably be rejected by the type checker, because it feels ambiguous.

davidhalter avatar Jan 06 '23 12:01 davidhalter

@davidhalter You might have already made an issue (I haven't checked) but if not, could you make a new one?

The original case in this issue is now passing as of mypy v1: https://mypy-play.net/?mypy=latest&python=3.11&gist=a0e33483038f95c43a7b6e99fec355df

A5rocks avatar Feb 22 '23 22:02 A5rocks

See #14799.

davidhalter avatar Feb 27 '23 20:02 davidhalter