ruff/crates/red_knot_python_semantic/resources/mdtest/scopes/unbound.md
David Peter 1feb3cf41a
[red-knot] Use Unknown | T_inferred for undeclared public symbols (#15674)
## Summary

Use `Unknown | T_inferred` as the type for *undeclared* public symbols.

## Test Plan

- Updated existing tests
- New test for external `__slots__` modifications.
- New tests for external modifications of public symbols.
2025-01-24 12:47:48 +01:00

1,021 B

Unbound

Unbound class variable

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

def coinflip() -> bool:
    return True

flag = coinflip()
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: Unknown | Literal[2]
reveal_type(C.y)  # revealed: Unknown | Literal[1]

Possibly unbound in class and global scope

def coinflip() -> bool:
    return True

if coinflip():
    x = "abc"

class C:
    if coinflip():
        x = 1

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

reveal_type(C.y)  # revealed: Unknown | Literal[1, "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)