@overload with multiple Generic[Any]
#2157
Unanswered
randolf-scholz
asked this question in
Q&A
Replies: 2 comments
|
The alternative of not preserving from typing import Any, overload, assert_type
class A[T]: # covariant
def get(self) -> T: ...
@overload
def op(l: A[None], r: A[None]) -> A[None]: ...
@overload
def op(l: A[None], r: A[Any]) -> A[Any]: ...
@overload
def op(l: A[Any], r: A[None]) -> A[Any]: ...
@overload
def op(l: A[Any], r: A[Any]) -> A[Any]: ...
def op(l, r):
# dummy implementation:
if l.get() is None or r.get() is None:
return A[None]()
return A[Any]()
def test(x: A[None], y: A[Any]) -> None:
assert_type(op(x, x), A[None])
assert_type(op(x, y), A[Any]) # ❌️: (pyright, pyrefly, ty)
assert_type(op(y, x), A[Any]) # ❌️: (pyright, pyrefly, ty)
assert_type(op(y, y), A[Any]) # ❌️: (pyright, pyrefly, ty) |
0 replies
|
The short answer is that Python’s current static typing system does not support inferring a generic return type from an “indirect factory” method unless that type variable is explicitly connected to the function signature. This limitation arises because Python’s type checkers (e.g., Why this limitation exists
|
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Am I missing something, or is it impossible to annotate
opin a way that satisfies theassert_typetest-suite in the example below?The schema is relevant for type hinting numeric types that interact with
math.nan/ nullable values.All reactions