Skip to content

Incorrect resolution of type[TypeVar] returning TypeVar with numpy #20354

@Dr-Irv

Description

@Dr-Irv

Bug Report

This came up with pandas-stubs, but can reproduce with just numpy

To Reproduce

from typing import TypeVar
import numpy as np

NDArrayT = TypeVar("NDArrayT", bound=np.ndarray)


def foo(x: type[NDArrayT]) -> NDArrayT:
    return x([1, 2])


reveal_type(foo(np.ndarray))

Expected Behavior

Revealed type should be ndarray[tuple[Any, ...], dtype[Any]], which is what pyright reports.

Actual Behavior

mypy reports numpy.ndarray[Any, Any]

Your Environment

  • Mypy version used: 1.19.0
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.11
  • numpy version: 2.3.5
  • pyright version: 1.1.407

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions