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

Possible regression in mypy 0.981: Generics don't work when TypeVar are previously used in a TypedDict #13755

Closed
jzazo opened this issue Sep 28, 2022 · 5 comments
Labels

Comments

@jzazo
Copy link

jzazo commented Sep 28, 2022

Bug Report

When pulling the example of Generics from the documentation and specifying a generic TypedDict following the blog post, I get an error on simple Generic example.

To Reproduce

from typing import Generic, TypeVar
from typing_extensions import TypedDict  # I am using python 3.10.4

T = TypeVar("T")
MyDict = TypedDict("MyDict", {"foo": T})

class Stack(Generic[T]):  # mypy error: Free type variable expected in Generic[...]  [misc]
    def __init__(self) -> None:
        pass

Commenting line 5 removes the error. Moving line 5 after the declaration of Stack also works.

Expected Behavior
That class Stack is a correct Generic and mypy should not error.

Your Environment

  • Mypy version used: 0.981
  • Python version used: 3.10.4
@AlexWaygood
Copy link
Member

AlexWaygood commented Sep 28, 2022

@jzazo
Copy link
Author

jzazo commented Sep 28, 2022

Another example that may originate from the same issue. Commenting MyDict1 declaration removes the error.

from typing import Sequence, TypeVar
from typing_extensions import TypedDict

T = TypeVar("T")

MyDict1 = TypedDict("MyDict1", {"foo": Sequence[T]})
MyDict2 = TypedDict("MyDict2", {"foo": Sequence[T]})

MyDict1[int]  # ok
MyDict2[int]  # mypy(error): The type "Type[MyDict2]" is not generic and not indexable  [misc]

@ilevkivskyi
Copy link
Member

This is not really a regression, as previously you would still get an error (in a different place). It is however indeed a bug, you should be able to reuse the type variable. Unless I am missing something, should be a simple fix.

@ilevkivskyi
Copy link
Member

Oh, actually both examples work correctly on master. It looks like @Michael0x2a already added the missing TypeVar scope frame in #13678 (and also fixed a docstring for me, thank you!)

@ilevkivskyi
Copy link
Member

(btw it was one of rare cases where git blame was used as git gratitude)

Michael0x2a added a commit to Michael0x2a/mypy that referenced this issue Sep 29, 2022
This is a small follow-up to python#13678 and python#13755.

In the former diff, I added a TypeVar scope frame around
TypedDict creation. However, I had really no good reason for
doing this at the time: I wasn't able to find a bug due to
the missing frame and added it purely speculatively, out of a
desire for symmetry.

It turns out this missing frame does legitimately cause
some issues, which were reported in the latter.

So, this diff encodes one of the reported bugs as a test
case to make sure we don't regress.
hauntsaninja pushed a commit that referenced this issue Sep 30, 2022
This is a small follow-up to #13678 and #13755.

In the former diff, I added a TypeVar scope frame around TypedDict
creation. However, I had really no good reason for doing this at the
time: I wasn't able to find a bug due to the missing frame and added it
purely speculatively, out of a desire for symmetry.

It turns out this missing frame does legitimately cause some issues,
which were reported in the latter.

So, this diff encodes one of the reported bugs as a test case to make
sure we don't regress.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants