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

ParamSpec readability simplification causes weird behavior #14799

Closed
davidhalter opened this issue Feb 27, 2023 · 1 comment · Fixed by #16240
Closed

ParamSpec readability simplification causes weird behavior #14799

davidhalter opened this issue Feb 27, 2023 · 1 comment · Fixed by #16240
Labels
bug mypy got something wrong topic-paramspec PEP 612, ParamSpec, Concatenate

Comments

@davidhalter
Copy link

Bug Report

ParamSpecs are allowed to be defined in class generics like this: Foo[[int, str]]. This is allowed to be simplified for readability reasons like this: Foo[int, str]. It should however only work if a ParamSpec is the only generic type variable. In the example below however we get a weird case where this simplification is applied even though it shouldn't.

To Reproduce

from typing import Generic, TypeVar, ParamSpec

P = ParamSpec("P")
T = TypeVar("T")

class D(Generic[P, T]): ...

# Fails:
D[int, bytes, str]
# Works
D[int, bytes]

See also: https://mypy-play.net/?mypy=latest&python=3.12&gist=1c1e42d6e323cb2dcc54a7c06b440cac

Expected Behavior

In case multiple type variables or param specs are defining a class, the param spec should not be simplified and the type application above D[int, bytes] should not be allowed.

@davidhalter davidhalter added the bug mypy got something wrong label Feb 27, 2023
@AlexWaygood AlexWaygood added the topic-paramspec PEP 612, ParamSpec, Concatenate label Feb 27, 2023
@ilevkivskyi
Copy link
Member

It is interesting that if it appears in annotations both examples correctly fail:

x: D[int, bytes, str]
y: D[int, bytes]

So the problem is specific when type application happens in runtime context.

ilevkivskyi added a commit that referenced this issue Oct 15, 2023
This adds some missing pieces to runtime type application handling for
both `TypeVarTuple` and `ParamSpec`. Everything is straightforward
(maybe a bit hacky, but we already import `typeanal` in `checkexpr` for
similar purposes, e.g. type aliases in runtime context).

Fixes #14799
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-paramspec PEP 612, ParamSpec, Concatenate
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants