Skip to content

(🐞) Error when using new union syntax and TypeVar in generic TypeAlias #12311

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

Closed
KotlinIsland opened this issue Mar 8, 2022 · 7 comments
Closed
Labels
bug mypy got something wrong

Comments

@KotlinIsland
Copy link
Contributor

KotlinIsland commented Mar 8, 2022

Originally spotted by @Eldar1205

from typing import *
T = TypeVar("T")

A: TypeAlias = Union[T, int] 
B: TypeAlias = T | int  # error
@KotlinIsland KotlinIsland added the bug mypy got something wrong label Mar 8, 2022
@JelleZijlstra
Copy link
Member

This isn't an error; it creates a generic alias.

@KotlinIsland
Copy link
Contributor Author

from typing import *
T = TypeVar("T")

A: TypeAlias = list[T]  # no error

def foo(a: A) -> A: ...

reveal_type(foo([1]))  # list[Any]

Well something seems broken

@KotlinIsland
Copy link
Contributor Author

Also these forms are then inconsistent:

A: TypeAlias = T | None  # error
B: TypeAlias = T  # error

@JelleZijlstra
Copy link
Member

Your first example produces main.py:6: error: Missing type parameters for generic type "A" with --disallow-any-generics. Agree that the T | None case should be accepted.

@KotlinIsland KotlinIsland changed the title (🐞) No error when using unbound TypeVar as type parameter in TypeAlias (🐞) Error when using new union syntax and TypeVar in TypeAlias Mar 8, 2022
@KotlinIsland KotlinIsland changed the title (🐞) Error when using new union syntax and TypeVar in TypeAlias (🐞) Error when using new union syntax and TypeVar in generic TypeAlias Mar 8, 2022
@KotlinIsland
Copy link
Contributor Author

@JelleZijlstra Fair enough, thanks for explaining. I've updated the title and OP, want to reopen it?

@AlexWaygood
Copy link
Member

Agree that the T | None case should be accepted.

That one's a PEP 604 bug; it works fine with typing.Union. https://mypy-play.net/?mypy=latest&python=3.10&gist=440bf42209bcc85bd6faa7b03b7e23d7

And it's already been reported in a different issue, lemme just find it

@AlexWaygood
Copy link
Member

Here: #12211

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

No branches or pull requests

3 participants