python / python/mypy

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

Open
#21,145 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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"

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the two generic function examples with --disallow-any-generics or --strict, including the reveal_type calls and TypeForm incomplete-feature setting. Trace how calls using list are analyzed for type[T] and TypeForm[T]; done means the missing type-argument error is emitted and the implicit Any no longer leaks in the shown reveals.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.