Files
ruff/crates/ty_python_semantic/resources/mdtest/scopes/builtin.md
Douglas Creager 06cd249a9b [ty] Track different uses of legacy typevars, including context when rendering typevars (#19604)
This PR introduces a few related changes:

- We now keep track of each time a legacy typevar is bound in a
different generic context (e.g. class, function), and internally create
a new `TypeVarInstance` for each usage. This means the rest of the code
can now assume that salsa-equivalent `TypeVarInstance`s refer to the
same typevar, even taking into account that legacy typevars can be used
more than once.

- We also go ahead and track the binding context of PEP 695 typevars.
That's _much_ easier to track since we have the binding context right
there during type inference.

- With that in place, we can now include the name of the binding context
when rendering typevars (e.g. `T@f` instead of `T`)
2025-08-01 12:20:32 -04:00

1.0 KiB

Builtin scope

Conditional local override of builtin

If a builtin name is conditionally shadowed by a local variable, a name lookup should union the builtin type with the conditionally-defined type:

def _(flag: bool) -> None:
    if flag:
        abs = 1
        chr: int = 1

    reveal_type(abs)  # revealed: Literal[1] | (def abs(x: SupportsAbs[_T@abs], /) -> _T@abs)
    reveal_type(chr)  # revealed: Literal[1] | (def chr(i: SupportsIndex, /) -> str)

Conditionally global override of builtin

If a builtin name is conditionally shadowed by a global variable, a name lookup should union the builtin type with the conditionally-defined type:

def flag() -> bool:
    return True

if flag():
    abs = 1
    chr: int = 1

def _():
    # TODO: Should ideally be `Unknown | Literal[1] | (def abs(x: SupportsAbs[_T], /) -> _T)`
    reveal_type(abs)  # revealed: Unknown | Literal[1]
    # TODO: Should ideally be `int | (def chr(i: SupportsIndex, /) -> str)`
    reveal_type(chr)  # revealed: int