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

Open

Activity

  1. 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[/+] on Mar 7, 2022
  2. KotlinIsland commented on Mar 7, 2022

    @KotlinIsland
    Contributor

    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.

  3. flisboac commented on Mar 9, 2022

    @flisboac

    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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions