ruff/crates/red_knot_python_semantic/resources/mdtest/call/union.md
Micha Reiser 6aaf1d9446
[red-knot] Remove lint-phase (#13922)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
2024-10-25 18:40:52 +00:00

1.8 KiB

Unions in calls

Union of return types

def bool_instance() -> bool:
    return True

flag = bool_instance()

if flag:

    def f() -> int:
        return 1

else:

    def f() -> str:
        return "foo"

reveal_type(f())  # revealed: int | str

Calling with an unknown union

from nonexistent import f  # error: [unresolved-import] "Cannot resolve import `nonexistent`"

def bool_instance() -> bool:
    return True

flag = bool_instance()

if flag:

    def f() -> int:
        return 1

reveal_type(f())  # revealed: Unknown | int

Non-callable elements in a union

Calling a union with a non-callable element should emit a diagnostic.

def bool_instance() -> bool:
    return True

flag = bool_instance()

if flag:
    f = 1
else:

    def f() -> int:
        return 1

x = f()  # error: "Object of type `Literal[1] | Literal[f]` is not callable (due to union element `Literal[1]`)"
reveal_type(x)  # revealed: Unknown | int

Multiple non-callable elements in a union

Calling a union with multiple non-callable elements should mention all of them in the diagnostic.

def bool_instance() -> bool:
    return True

flag, flag2 = bool_instance(), bool_instance()

if flag:
    f = 1
elif flag2:
    f = "foo"
else:

    def f() -> int:
        return 1

# error: "Object of type `Literal[1] | Literal["foo"] | Literal[f]` is not callable (due to union elements Literal[1], Literal["foo"])"
# revealed: Unknown | int
reveal_type(f())

All non-callable union elements

Calling a union with no callable elements can emit a simpler diagnostic.

def bool_instance() -> bool:
    return True

flag = bool_instance()

if flag:
    f = 1
else:
    f = "foo"

x = f()  # error: "Object of type `Literal[1] | Literal["foo"]` is not callable"
reveal_type(x)  # revealed: Unknown