Skip to content

generics handle default argument incorrectly #16219

Closed as not planned
Closed as not planned
@Ranfir

Description

@Ranfir

Bug Report

Mypy doesn't appear to handle generic parameters with default values correctly.

To Reproduce

from typing import TypeVar, reveal_type

import numpy as np
from numpy.typing import NBitBase, NDArray

T = TypeVar("T", bound=NBitBase)

def func(dtype: type[np.floating[T]] = np.float64) -> NDArray[np.floating[T]]:  # error: Incompatible default for argument "dtype" (default has type "type[floating[Any]]", argument has type "type[floating[T]]")  [assignment]
    return np.empty(1, dtype)

reveal_type(func())  # Revealed type is "numpy.ndarray[Any, numpy.dtype[numpy.floating[<nothing>]]]"
reveal_type(func(np.float32))  # Revealed type is "numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy._typing._32Bit]]]"

Expected Behavior

scratch.py:11: note: Revealed type is "numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy._typing._64Bit]]]"
scratch.py:12: note: Revealed type is "numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy._typing._32Bit]]]"

Actual Behavior

scratch.py:8: error: Incompatible default for argument "dtype" (default has type "type[floating[Any]]", argument has type "type[floating[T]]")  [assignment]
scratch.py:11: note: Revealed type is "numpy.ndarray[Any, numpy.dtype[numpy.floating[<nothing>]]]"
scratch.py:12: note: Revealed type is "numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy._typing._32Bit]]]"
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.5.1
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.11.5
$ .venv/bin/pip freeze
mypy==1.5.1
mypy-extensions==1.0.0
numpy==1.26.0
typing_extensions==4.8.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions