Skip to content

Emit errors with --disallow-any-generics if type[T] or TypeForm[T] receives a generic T with missing type arguments #21145

@bzoracler

Description

@bzoracler

Feature

If --disallow-any-generics or --strict is on, I'd like mypy to emit E: Missing type parameters for generic type "list" [type-arg] when calling f(list) for the following function definitions:

  • def f[T](arg: type[T], /) -> T: ...
  • def f[T](arg: TypeForm[T], /) -> T: ...

Currently, there's no error report, and reveal_type(f(list)) shows Any.

Pitch

Due to the lack of error reporting in the above situation, implicit Anys currently easily leak when using type[T] or TypeForm[T], creating an implicit source of unsafety, which is surprising under --strict mode. Adapting the examples from PEP 747: Motivation:

# mypy: enable-incomplete-feature=TypeForm, disable-error-code=empty-body

from collections.abc import Callable
from typing_extensions import TypeForm, TypeIs

def trycast_list_item[T](typx: TypeForm[list[T]], value: object) -> T | None: ...
reveal_type(trycast_list_item(list, [1]))  # N: Revealed type is "Any | None"


def isassignable_list_item[T](value: object, typx: TypeForm[list[T]]) -> TypeIs[T]: ...

a: object
if isassignable_list_item(a, list):
    reveal_type(a)  # N: Revealed type is "Any"
else:
    reveal_type(a)  # N: Revealed type is "builtins.object"

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions