Skip to content

no error when value of type type (aka type[Any]) passed to function with generic with constraint, generic inferred as the first constraint #12300

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

Open
DetachHead opened this issue Mar 7, 2022 · 2 comments
Labels
bug mypy got something wrong topic-type-variables

Comments

@DetachHead
Copy link
Contributor

DetachHead commented Mar 7, 2022

@DetachHead DetachHead added the bug mypy got something wrong label Mar 7, 2022
@DetachHead DetachHead changed the title no error when value of type type (aka type[Any] passed to function with generic with constraint, generic inferred as the first constraint no error when value of type type (aka type[Any]) passed to function with generic with constraint, generic inferred as the first constraint Mar 7, 2022
@KotlinIsland
Copy link
Contributor

KotlinIsland commented Mar 7, 2022

I think this is three issues in one 😳

  1. mypy doesn't care about type without type parameter and hides Any errors (🐞) No error with disallow-any-generics and type #12301
  2. type[T] generics default to <nothing> when they are any
from typing import _T

def foo(t: type[_T]) -> _T: ...

t: type
reveal_type(foo(t)) # <nothing>
  1. Constrained TypeVars default to their first constraint when they are anyified example in OP, and:
from typing import TypeVar

T = TypeVar("T", int, str)

def foo(cls: type[T]) -> T: ...

a = []

reveal_type(foo(a)) # int

This seems quite broken.

@flisboac
Copy link

flisboac commented Mar 9, 2022

Regarding item 2, PEP-483 says the following:

If a generic type appears in a type annotation with a type variable omitted, it is assumed to be Any.

So, it is a bug. Curiously, with Type the correct behaviour is shown:

from typing import TypeVar, Any, Type

T = TypeVar('T')

t: Type

def test(cls: Type[T]) -> T: ...

reveal_type(t)  # note: Revealed type is "Type[Any]"

reveal_type(test(t))  # note: Revealed type is "Any"

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-type-variables
Projects
None yet
Development

No branches or pull requests

4 participants