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

Argument of type "X" cannot be assigned to parameter of type "X" despite cast #9953

Open
bersbersbers opened this issue Feb 21, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@bersbersbers
Copy link

Describe the bug
I would expect that after array = cast("FloatArray", array), I should always be able to call foo(array) with def foo(array: FloatArray). However, pyright does not like it:

bug.py:20:9 - error: Argument of type "FloatArray" cannot be assigned to parameter "array" of type "FloatArray" in function "foo"
"ndarray[Any, dtype[floating[Any] | integer[Any]]]" is not assignable to "ndarray[Any, dtype[floating[Any]]]"
Type parameter "_DType_co@ndarray" is covariant, but "dtype[floating[Any] | integer[Any]]" is not a subtype of "dtype[floating[Any]]"
"dtype[floating[Any] | integer[Any]]" is not assignable to "dtype[floating[Any]]"
Type parameter "_DTypeScalar_co@dtype" is covariant, but "floating[Any] | integer[Any]" is not a subtype of "floating[Any]"
Type "floating[Any] | integer[Any]" is not assignable to type "floating[Any]" (reportArgumentType)
1 error, 0 warnings, 0 informations

Code or Screenshots

from typing import Any, cast

import numpy as np
import numpy.typing as npt

type FloatArray = npt.NDArray[np.floating[Any]]
type NumberArray = npt.NDArray[np.floating[Any] | np.integer[Any]]


def foo(array: FloatArray) -> None: ...


def bar(array: NumberArray) -> None:
    # this works:
    array2 = cast("FloatArray", array)
    foo(array2)

    # this seems to confuse pyright
    array = cast("FloatArray", array)
    foo(array)

VS Code extension or command-line
pyright 1.1.394 from PyPI

@bersbersbers bersbersbers added the bug Something isn't working label Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant