Files
ruff/crates/red_knot_python_semantic/resources/mdtest/scopes/unbound.md
Douglas Creager 918358aaa6 Migrate some inference tests to mdtests (#14795)
As part of #13696, this PR ports a smallish number of inference tests
over to the mdtest framework.
2024-12-06 11:19:22 +01:00

1.0 KiB

Unbound

Unbound class variable

Name lookups within a class scope fall back to globals, but lookups of class attributes don't.

def bool_instance() -> bool:
    return True

flag = bool_instance()
x = 1

class C:
    y = x
    if flag:
        x = 2

# error: [possibly-unbound-attribute] "Attribute `x` on type `Literal[C]` is possibly unbound"
reveal_type(C.x)  # revealed: Literal[2]
reveal_type(C.y)  # revealed: Literal[1]

Possibly unbound in class and global scope

def bool_instance() -> bool:
    return True

if bool_instance():
    x = "abc"

class C:
    if bool_instance():
        x = 1

    # error: [possibly-unresolved-reference]
    y = x

reveal_type(C.y)  # revealed: Literal[1] | Literal["abc"]

Unbound function local

An unbound function local that has definitions in the scope does not fall back to globals.

x = 1

def f():
    # error: [unresolved-reference]
    # revealed: Unknown
    reveal_type(x)
    x = 2
    # revealed: Literal[2]
    reveal_type(x)