python / python/mypy

Resolution of disallowed `Type[Type[T]]` and casting to a blank `Type`

Open
#14,183 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

(A clear and concise description of what the bug is.)
Mypy is inferring types that are illegal to create or annotate, like Type[Type[T]], which causes errors during linting checks.
The only ways to resolve it are either # type: ignore, which seems counter-intuitive when writing type safe code, or doing a typing.cast(typing.Type) which defeats the purpose of the generic typehint

To Reproduce
Playground link

Expected Behavior
I'd expect Type[Type[T]] to coalesce into the legal generic Type[T]

Actual Behavior
Mypy sees the expected type as Type[Type[T]], causing an error when not casting or ignoring.

Your Environment

The template asks for a link to the affected repository, if it's open source. This can be found in the EnforceTypes repo

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 with the linked mypy-play reproduction using Python 3.10 and mypy 0.991, then trace how nested Type expressions are analyzed. The issue names no mypy source file or test; done means the example accepts the intended Type[T] relationship without requiring type: ignore or a blank typing.cast.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.