Skip to content

Unknown ReturnType when TypeIs[] with walrus-operator := #2225

@AClon314

Description

@AClon314
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]

    pass

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: otherOther topics not covered

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions