mirror of
https://github.com/astral-sh/ruff.git
synced 2026-11-26 06:15:39 +08:00
## Summary
PEP 484's numeric tower means we represent a `float` annotation as the
semantic union `int | float*` and `complex` as `int | float* |
complex*`. We previously rendered those internal unions directly, which
exposed an implementation detail in user-facing output:
```python
def f() -> float:
return 1
reveal_type(f()) # Before: int | float; after: float
```
This change recognizes complete numeric-tower unions during display and
renders them using their canonical annotation spelling, `float` or
`complex`.
The implementation does _not_ preserve explicit unions, e.g., if the
user writes out `int | float`, we still collapse that rather than trying
to preserve it, mostly for simplicity (as per
https://github.com/astral-sh/ty/issues/2184#issuecomment-4345219164).
Closes https://github.com/astral-sh/ty/issues/2184.
808 B
808 B
Integer literals
Literals
We can infer an integer literal type:
reveal_type(1) # revealed: Literal[1]
Variable
x = 1
reveal_type(x) # revealed: Literal[1]
Overflow
We only track integer literals within the range of an i64:
reveal_type(9223372036854775808) # revealed: int
Big int
We don't support big integer literals; we just infer int type instead:
x = 10_000_000_000_000_000_000
reveal_type(x) # revealed: int
Negated
x = -1
y = -1234567890987654321
z = --987
reveal_type(x) # revealed: Literal[-1]
reveal_type(y) # revealed: Literal[-1234567890987654321]
reveal_type(z) # revealed: Literal[987]
Floats
reveal_type(1.0) # revealed: float*
Complex
reveal_type(2j) # revealed: complex*