Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generics handle default argument incorrectly #16219

Closed
Ranfir opened this issue Oct 3, 2023 · 2 comments
Closed

generics handle default argument incorrectly #16219

Ranfir opened this issue Oct 3, 2023 · 2 comments
Labels
bug mypy got something wrong

Comments

@Ranfir
Copy link

Ranfir commented Oct 3, 2023

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
@Ranfir Ranfir added the bug mypy got something wrong label Oct 3, 2023
@tmke8
Copy link
Contributor

tmke8 commented Oct 5, 2023

Same as #3737?

@ilevkivskyi
Copy link
Member

Duplicate of #3737

@ilevkivskyi ilevkivskyi marked this as a duplicate of #3737 Oct 5, 2023
@ilevkivskyi ilevkivskyi closed this as not planned Won't fix, can't repro, duplicate, stale Oct 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

3 participants