-
Notifications
You must be signed in to change notification settings - Fork 288
@overload with multiple Generic[Any]
#2157
Unanswered
randolf-scholz
asked this question in
Q&A
-
Am I missing something, or is it impossible to annotate op in a way that satisfies the assert_type test-suite in the example below?
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[None]: ... @overload def op(l: A[Any], r: A[None]) -> A[None]: ... @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[None]) # ❌️: (mypy, zuban) assert_type(op(y, x), A[None]) # ❌️: (mypy, zuban) assert_type(op(y, y), A[Any]) # ❌️: (pyright, ty, pyrefly)
The schema is relevant for type hinting numeric types that interact with math.nan / nullable values.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment
-
The alternative of not preserving None in the mixed case makes mypy and zuban happy, but gives even worse results on the other type checkers:
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)
Beta Was this translation helpful? Give feedback.
All reactions
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment