Pyright error with generics that deal with numpy arrays and floats #9818
Unanswered
sabarish-vm
asked this question in
Q&A
Replies: 1 comment
-
You are using a value-constrained type variable here. The behavior of value-constrained type variables are not well-defined in the typing spec, and I recommend against using them. There's almost always a better approach. This concept is not found in any other type system or language — and for good reason. Because their behavior is not well defined, you are likely to see inconsistencies in behavior across type checkers if you use them. For additional details, refer to this documentation. In the case of your code above, I recommend using an overload to define the polymorphic behavior of this function. @overload
def f(r: np.ndarray) -> np.ndarray: ...
@overload
def f(r: float) -> float: ...
def f(r: np.ndarray | float) -> np.ndarray | float:
return r**3.0 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello, Below I provide a small snippet to demonstrate the problem.
I would naively expect that this should not raise an error. For example, mypy doesn't flag an error here, but pyright does.
Are there some flags that I can turn on to avoid this ?
I get the following Pyright error message :
Type "NDArray[float64] | Any" is not assignable to type "T@f" [reportReturnType]
But apart from disabling the flagging. I am also wondering why does pyright think that the return type is Any.
That is reveal_type(r**3.0) reveals that it is of type Any.
I just started using typing in python and I am not sure if I am doing something wrong.
Beta Was this translation helpful? Give feedback.
All reactions