[ty] Minor change to builtins.md test (#18889)

## Summary

As far as I can tell, the two existing tests did the exact same thing.
Remove the redundant test, and add tests for all combinations of
declared/not-declared and local/"public" use of the name.

Proposing this as a separate PR before the behavior might change via
https://github.com/astral-sh/ruff/pull/18750
This commit is contained in:
David Peter 2025-06-23 12:32:50 +02:00 committed by GitHub
parent 528ae8083b
commit 21303d1a02
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,32 +1,34 @@
# Builtin scope
## Conditionally global or builtin
## Conditional local override of builtin
If a builtin name is conditionally defined as a global, a name lookup should union the builtin type
with the conditionally-defined type:
If a builtin name is conditionally shadowed by a local variable, a name lookup should union the
builtin type with the conditionally-defined type:
```py
def returns_bool() -> bool:
return True
def _(flag: bool) -> None:
if flag:
abs = 1
chr: int = 1
if returns_bool():
chr: int = 1
def f():
reveal_type(chr) # revealed: int | (def chr(i: SupportsIndex, /) -> str)
reveal_type(abs) # revealed: Literal[1] | (def abs(x: SupportsAbs[_T], /) -> _T)
reveal_type(chr) # revealed: Literal[1] | (def chr(i: SupportsIndex, /) -> str)
```
## Conditionally global or builtin, with annotation
## Conditionally global override of builtin
Same is true if the name is annotated:
If a builtin name is conditionally shadowed by a global variable, a name lookup should union the
builtin type with the conditionally-defined type:
```py
def returns_bool() -> bool:
def flag() -> bool:
return True
if returns_bool():
if flag():
abs = 1
chr: int = 1
def f():
def _():
reveal_type(abs) # revealed: Unknown | Literal[1] | (def abs(x: SupportsAbs[_T], /) -> _T)
reveal_type(chr) # revealed: int | (def chr(i: SupportsIndex, /) -> str)
```