python / python/mypy

Cannot instantiate Type[Type[...]]

Open
#12,425 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

Bug Report

Mypy thinks that Type[Type[...]] cannot be instantiated. I understand that it can be difficult to support, but is there any reason to consider it an error?

Type[Type[something]] is type or its subclass. It can be instantiated (code below runs without errors).

To Reproduce

Write the following code, run mypy on it.

class A:
    pass

B = type(A)('B', (A,), {})
b = B()

Playground

Expected Behavior

No errors (but I'm not sure that I'm not just missing something and under certain circumstances such code can be incorrect).

Actual Behavior

mypy reports:

main.py:4: error: Cannot instantiate type "Type[Type[A]]"

Your Environment

  • Mypy version used: 0.941 latest (from pip & playground)
  • Mypy command-line flags: nothing
  • Mypy configuration options from mypy.ini (and other config files): nothing
  • Python version used: 3.10
  • Operating system and version: Ubuntu 22.04

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 putting the reported class definitions and type() call in main.py and running mypy with the stated Python 3.10 setup to reproduce the diagnostic. Trace the handling of Type[Type[A]] and add a regression test covering this case; done means mypy accepts the example without weakening unrelated instantiation checks.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.