-
Notifications
You must be signed in to change notification settings - Fork 290
Closed as not planned
Labels
topic: otherOther topics not coveredOther topics not covered
Description
from typing import overload, Any, TypeIs, Literal
from collections.abc import Callable
@overload
def unpack_method(deco: classmethod, cls: type) -> Callable: ...
@overload
def unpack_method(deco: staticmethod) -> Callable: ...
@overload
def unpack_method(deco: Callable) -> Callable: ...
@overload
def unpack_method(deco: Any) -> TypeIs[Callable]: ...
# 😟TypeIs can not tell the return type of `unpack_method()`.
def unpack_method(
deco: classmethod | staticmethod, cls: type | None = None
) -> Callable | None:
if isinstance(deco, classmethod):
return deco.__get__(None, cls)
elif isinstance(deco, staticmethod):
return deco.__get__(None, None)
elif isinstance(deco, Callable):
return deco
return None
obj: Literal["123"] | Callable[[], str] = "123"
if func := unpack_method(obj):
# ty error: Object of type `TypeIs[Top[(...) -> object] @ obj] & ~AlwaysFalsy` is not callable (call-non-callable)
# pyright error: Object of type "TypeIs[(...) -> Unknown]" is not callable. Attribute "__call__" is unknown (reportCallIssue)
# mypy error: "Literal[True]" not callable [misc]
func()
print("unpack success, func is", func)
elif "123" in func:
# ty error: Unsupported `in` operation (unsupported-operator)
# pyright error: Operator "in" not supported for types "Literal['123']" and "TypeIs[(...) -> Unknown]" (reportOperatorIssue)
# mypy error: Unsupported right operand type for in ("Literal[False]") [operator]
passReactions are currently unavailable
Metadata
Metadata
Assignees
Labels
topic: otherOther topics not coveredOther topics not covered