mirror of
https://github.com/astral-sh/ruff.git
synced 2026-11-24 01:39:22 +08:00
## Summary In `x is not None and x.value`, ty needs the narrowing from the first operand while it visits the second. Once the paths rejoin, the bindings are the same as they were on entry. We used to take a snapshot at every point where the expression could short-circuit and merge them afterward. We now save the entry state once and restore it after visiting the operands. A walrus expression can introduce a binding, and an active exception handler can observe an intermediate state. Both cases still need the individual snapshots. <!-- ty-pinned-project-instructions:start --> The table shows median instructions per cold `ty check`. | Project | Parent instructions | PR instructions | Change | Paired range | |---|---:|---:|---:|---:| | [Black](https://github.com/psf/black/tree/55996d1fb8342725c8a19b6023cd9439c3c1d6f7) | 3,327,629,858 | 3,228,489,024 | -2.99% | -3.13% to -2.81% | | [discord.py](https://github.com/Rapptz/discord.py/tree/65232c38702be5844cf2ce865a4777eb1928b5d0) | 8,526,139,909 | 8,463,328,726 | -0.76% | -0.88% to -0.45% | | [Django](https://github.com/django/django/tree/b501b8493f31cf8cba31211d59b3b78642b8cb02) | 14,526,261,720 | 14,406,319,474 | -0.76% | -0.95% to -0.74% | | [FastAPI](https://github.com/fastapi/fastapi/tree/7a0a3f0b286c6c865cf4398ea0e957a87b8bffa4) | 3,208,278,164 | 3,185,735,825 | -0.69% | -0.73% to -0.62% | | [Home Assistant](https://github.com/home-assistant/core/tree/c19ae285ede74cd64c3045a3a4aa43290c224f1b) | 160,432,235,038 | 159,688,122,787 | -0.46% | -0.51% to -0.45% | | [HTTPX](https://github.com/encode/httpx/tree/b5addb64f0161ff6bfe94c124ef76f6a1fba5254) | 1,759,762,092 | 1,714,778,980 | -2.53% | -2.58% to -2.46% | | [isort](https://github.com/pycqa/isort/tree/a514c71ed86c2216d0040c7d60e302ed76a4f7ab) | 2,115,726,444 | 2,071,864,550 | -2.08% | -2.16% to -1.86% | | [Jinja](https://github.com/pallets/jinja/tree/5ef70112a1ff19c05324ff889dd30405b1002044) | 1,921,906,044 | 1,921,630,812 | 0.00% | -0.17% to +0.11% | | [NumPy](https://github.com/numpy/numpy/tree/f9f1772ce010001208475f751d19253469788410) | 14,350,543,068 | 14,337,534,500 | -0.09% | -0.66% to -0.02% | | [OpenAI Python SDK](https://github.com/openai/openai-python/tree/f6e8480f9a5b2a4d02b17a1da54a1e57964bc312) | 17,911,735,261 | 17,808,248,965 | -0.54% | -2.55% to +1.45% | | [pandas](https://github.com/pandas-dev/pandas/tree/a0225b65720018f4654390bb16d254daa6ee5e54) | 30,143,629,900 | 30,011,481,130 | -0.48% | -0.56% to -0.31% | | [pandas-stubs](https://github.com/pandas-dev/pandas-stubs/tree/8dd6d91b1c05b26e30f7ddbe3dc3d4042a92bdd6) | 3,586,149,998 | 3,574,843,506 | -0.35% | -0.54% to -0.25% | | [Prefect](https://github.com/PrefectHQ/prefect/tree/a9452ac21852755311913cb27887b083b5007c03) | 5,087,567,478 | 5,030,294,525 | -1.14% | -1.18% to -1.01% | | [pytest](https://github.com/pytest-dev/pytest/tree/8b005ed7f972987033ec34dc1e68b0635fb8142b) | 8,663,862,776 | 8,614,190,006 | -0.56% | -0.71% to -0.51% | | [PyTorch](https://github.com/pytorch/pytorch/tree/ab92945cea42e47ae398aead69af0872d340ea0f) | 98,260,011,188 | 97,510,158,026 | -0.77% | -0.85% to -0.68% | | [Rich](https://github.com/Textualize/rich/tree/9d8f9a372cc5916fd4781fec207ced7ddac2f08f) | 2,778,090,819 | 2,756,011,028 | -0.73% | -1.37% to -0.67% | | [SQLAlchemy](https://github.com/sqlalchemy/sqlalchemy/tree/10f4d1e2d706a7722ca78e79ce05b00333a7aa5b) | 21,141,258,225 | 21,076,436,106 | -0.29% | -0.49% to -0.25% | | [SymPy](https://github.com/sympy/sympy/tree/85d3998a4517ffbc8a9709b8efa769bb417c174a) | 47,484,365,830 | 47,286,307,006 | -0.40% | -0.48% to -0.38% | These measurements use PR revision `517ed31f6968`. <!-- ty-pinned-project-instructions:end --> --------- Co-authored-by: Charlie Marsh <charliemarsh@openai.com>
2.5 KiB
2.5 KiB
Narrowing in boolean expressions
In or expressions, the right-hand side is evaluated only if the left-hand side is falsy. So
when the right-hand side is evaluated, we know the left side has failed.
Similarly, in and expressions, the right-hand side is evaluated only if the left-hand side is
truthy. So when the right-hand side is evaluated, we know the left side has succeeded.
Narrowing in or
class A: ...
def _(x: A | None):
isinstance(x, A) or reveal_type(x) # revealed: None
x is None or reveal_type(x) # revealed: A
reveal_type(x) # revealed: A | None
Narrowing in and
from typing import final
class A: ...
def _(x: A | None):
isinstance(x, A) and reveal_type(x) # revealed: A
x is None and reveal_type(x) # revealed: None
reveal_type(x) # revealed: A | None
@final
class FinalClass: ...
# We know that no subclass of `FinalClass` can exist,
# therefore no subtype of `FinalClass` can define `__bool__`
# or `__len__`, therefore `FinalClass` can safely be considered
# always-truthy, therefore this always resolves to `None`
reveal_type(FinalClass() and None) # revealed: None
Multiple and arms
class A: ...
def _(x: A | None, flag1: bool, flag2: bool):
flag1 and isinstance(x, A) and reveal_type(x) # revealed: A
isinstance(x, A) and flag1 and reveal_type(x) # revealed: A
reveal_type(x) and isinstance(x, A) and flag2 # revealed: A | None
Multiple or arms
class A: ...
def _(x: A | None, flag1: bool, flag2: bool, flag3: bool):
flag1 or isinstance(x, A) or reveal_type(x) # revealed: None
isinstance(x, A) or flag2 or reveal_type(x) # revealed: None
reveal_type(x) or isinstance(x, A) or flag3 # revealed: A | None
Multiple predicates
from typing import Literal
class A: ...
def _(x: A | None | Literal[1]):
x is None or isinstance(x, A) or reveal_type(x) # revealed: Literal[1]
Mix of and and or
from typing import Literal
class A: ...
def _(x: A | None | Literal[1]):
isinstance(x, A) or x is not None and reveal_type(x) # revealed: Literal[1]
Narrowing after a nested boolean expression
A later operand sees the narrowing from earlier operands. After the expression, only the narrowing that preceded it still applies.
def check(value: int | str | None, flag: bool):
if value is None:
return
isinstance(value, str) and (flag or reveal_type(value)) # revealed: str
reveal_type(value) # revealed: int | str