python / python/mypy

type without arguments becomes type[nothing] instead of type[Any]

Open
#16,001 0 comments 2 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

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.11&gist=07773fc6948673f0c4279ecf835537cc

from typing import Any, TypeVar, assert_type

T = TypeVar("T")

def fn(x: type[T]) -> list[T]:
    return []

def test(x: type) -> None:
    assert_type(fn(x), list[Any])

Expected Behavior

I expect this to work. According to PEP 484: https://peps.python.org/pep-0484/#the-type-of-class-objects

Plain Type without brackets is equivalent to Type[Any] and this in turn is equivalent to type (the root of Python’s metaclass hierarchy).

Actual Behavior

main.py:9: error: Expression is of type "list[<nothing>]", not "list[Any]"  [assert-type]

The type parameter for type became nothing instead of Any. However, if I explicitly use type[Any] this works as expected:

def test(x: type[Any]) -> None:
    assert_type(fn(x), list[Any])

It also works if I use typing.Type without arguments instead of type.

Your Environment

  • Mypy version used: 1.5.1
  • Python version used: 3.11

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 and inspect how an unparameterized built-in type is handled when inferring the type variable in fn. The fix is done when the reproduction accepts assert_type(fn(x), list[Any]) while preserving the existing behavior for explicit type[Any] and typing.Type.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.