ruff/crates/red_knot_python_semantic/resources/mdtest/expression/attribute.md
David Peter f1f3bd1cd3
[red-knot] Review remaining 'possibly unbound' call sites (#14284)
## Summary

- Emit diagnostics when looking up (possibly) unbound attributes
- More explicit test assertions for unbound symbols
- Review remaining call sites of `Symbol::ignore_possibly_unbound`. Most
of them are something like `builtins_symbol(self.db,
"Ellipsis").ignore_possibly_unbound().unwrap_or(Type::Unknown)` which
look okay to me, unless we want to emit additional diagnostics. There is
one additional case in enum literal handling, which has a TODO comment
anyway.

part of #14022

## Test Plan

New MD tests for (possibly) unbound attributes.
2024-11-11 20:48:49 +01:00

635 B

Attribute access

Boundness

def flag() -> bool: ...

class A:
    always_bound = 1

    if flag():
        union = 1
    else:
        union = "abc"

    if flag():
        possibly_unbound = "abc"

reveal_type(A.always_bound)  # revealed: Literal[1]

reveal_type(A.union)  # revealed: Literal[1] | Literal["abc"]

# error: [possibly-unbound-attribute] "Attribute `possibly_unbound` on type `Literal[A]` is possibly unbound"
reveal_type(A.possibly_unbound)  # revealed: Literal["abc"]

# error: [unresolved-attribute] "Type `Literal[A]` has no attribute `non_existent`"
reveal_type(A.non_existent)  # revealed: Unknown