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

typing_extensions TypeVar seems to be unsupported #14312

Closed
evtn opened this issue Dec 18, 2022 · 6 comments · Fixed by #14313
Closed

typing_extensions TypeVar seems to be unsupported #14312

evtn opened this issue Dec 18, 2022 · 6 comments · Fixed by #14313
Labels
bug mypy got something wrong

Comments

@evtn
Copy link

evtn commented Dec 18, 2022

Bug Report

Mypy doesn't support typing_extensions.TypeVar at all

To Reproduce

Playground

from typing import Generic
from typing_extensions import (
    TypeVar,
)

T = TypeVar("T")

class A(Generic[T]):
    def __init__(self, value: T):
        self.value = value

a: A = A(8)
b: A[str] = A("")

Expected Behavior

This code should work fine (importing TypeVar from typing works, but my actual use case depends on default argument of TypeVar, so I can't use typing.TypeVar)

Actual Behavior

main.py:8: error: Free type variable expected in Generic[...]  [misc]
main.py:9: error: Variable "__main__.T" is not valid as a type  [valid-type]
main.py:9: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
main.py:13: error: "A" expects no type arguments, but 1 given  [type-arg]
Found 3 errors in 1 file (checked 1 source file)

Your Environment

mypy Playground, latest mypy (0.991)

@evtn evtn added the bug mypy got something wrong label Dec 18, 2022
@JelleZijlstra
Copy link
Member

This should be an easy fix if you're up for it. For inspiration, you can look at the commit adding support for typing_extensions.overload: 0df8cf5.

@evtn
Copy link
Author

evtn commented Dec 18, 2022

I would be happy to contribute, but it seems that this approach would just alias typing.TypeVar.

My actual usage (not from example) relies on default argument, which is absent from typing version (because it's from PEP 696 which is a draft).

@JelleZijlstra
Copy link
Member

Yes, that would be much harder. :) I think @Gobot1234 already has a draft implementation of PEP 696 for mypy.

@Gobot1234
Copy link
Contributor

https://github.com/Gobot1234/mypy/tree/TypeVar-defaults is my draft implementation of PEP 696, its not bug free and is more lax than the current specification but it should work for most basic things.

@cdce8p
Copy link
Collaborator

cdce8p commented Dec 19, 2022

I opened #14313 to add basic support for the typing_extensions variant. This doesn't include default just yet though.

@Gobot1234 Would you be able to rebase your TypeVar-defaults branch? A lot has happened in the last 2 months and it got a bit out of sync. That would make it much easier to see how we can add it to mypy.

JelleZijlstra pushed a commit that referenced this issue Dec 19, 2022
This PR only adds the existing `TypeVar` support for the
`typing_extensions` variant.
I.e. it does not include support for `default` or `infer_variance`.

Fixes #14312
@Gobot1234
Copy link
Contributor

@cdce8p should be done now, not sure how out of sync everything is, it sure does look like a lot changed

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

Successfully merging a pull request may close this issue.

4 participants