[ty] Reuse file and scope checks when reporting diagnostics (#30947)
## Summary
Before reporting a diagnostic, we check whether the file is in scope and
whether an enclosing function has `@no_type_check`. These answers are
the same throughout an inference query, so we now look them up at most
once per inference context. We still check the current diagnostic mode
and inference flags each time.
We also defer looking up the rules for redundant conditions until we
reach an `if`, `assert`, `while` or `match`.
<!-- ty-pinned-project-instructions:start -->
<details>
<summary>Instruction counts by project</summary>
Both builds use the linked main commit, with this PR applied to the
second. Counts are medians of four single-worker Cachegrind runs on
Linux (x86-64). Change is the median of four paired percentages.
| Project | [Base
instructions](https://github.com/astral-sh/ruff/commit/9f70bdad1a3869e0190e5ef7014380861e34fec4)
| [PR
instructions](https://github.com/astral-sh/ruff/commit/5dadedcbd2548c10c11b2b50c51f8e8321f9f888)
| Change | Paired range |
| --- | ---: | ---: | ---: | ---: |
|
[Black](https://github.com/psf/black/tree/55996d1fb8342725c8a19b6023cd9439c3c1d6f7)
| 2,569,755,751.5 | 2,560,808,368 | -0.35% | -0.35% to -0.35% |
|
[discord.py](https://github.com/Rapptz/discord.py/tree/65232c38702be5844cf2ce865a4777eb1928b5d0)
| 6,283,648,584.5 | 6,248,280,640.5 | -0.56% | -0.58% to -0.56% |
|
[isort](https://github.com/pycqa/isort/tree/a514c71ed86c2216d0040c7d60e302ed76a4f7ab)
| 1,399,869,722 | 1,396,564,539.5 | -0.24% | -0.24% to -0.23% |
|
[Jinja](https://github.com/pallets/jinja/tree/5ef70112a1ff19c05324ff889dd30405b1002044)
| 1,496,845,149.5 | 1,489,881,833 | -0.47% | -0.49% to -0.43% |
|
[pandas](https://github.com/pandas-dev/pandas/tree/a0225b65720018f4654390bb16d254daa6ee5e54)
| 22,125,738,080.5 | 22,044,934,586 | -0.37% | -0.40% to -0.34% |
|
[pandas-stubs](https://github.com/pandas-dev/pandas-stubs/tree/8dd6d91b1c05b26e30f7ddbe3dc3d4042a92bdd6)
| 2,523,602,396.5 | 2,518,935,346 | -0.18% | -0.21% to -0.17% |
|
[Prefect](https://github.com/PrefectHQ/prefect/tree/a9452ac21852755311913cb27887b083b5007c03)
| 4,160,056,946 | 4,154,399,865 | -0.13% | -0.14% to -0.13% |
|
[PyTorch](https://github.com/pytorch/pytorch/tree/ab92945cea42e47ae398aead69af0872d340ea0f)
| 74,617,503,843 | 74,087,615,367 | -0.71% | -0.71% to -0.71% |
|
[HTTPX](https://github.com/encode/httpx/tree/b5addb64f0161ff6bfe94c124ef76f6a1fba5254)
| 1,315,925,824 | 1,311,883,429.5 | -0.31% | -0.31% to -0.30% |
|
[NumPy](https://github.com/numpy/numpy/tree/f9f1772ce010001208475f751d19253469788410)
| 7,751,113,649.5 | 7,738,460,807.5 | -0.17% | -0.19% to -0.14% |
|
[FastAPI](https://github.com/fastapi/fastapi/tree/7a0a3f0b286c6c865cf4398ea0e957a87b8bffa4)
| 2,456,910,487.5 | 2,451,036,788 | -0.24% | -0.24% to -0.23% |
|
[SQLAlchemy](https://github.com/sqlalchemy/sqlalchemy/tree/10f4d1e2d706a7722ca78e79ce05b00333a7aa5b)
| 16,225,614,506.5 | 16,139,089,241.5 | -0.53% | -0.54% to -0.53% |
|
[Rich](https://github.com/Textualize/rich/tree/9d8f9a372cc5916fd4781fec207ced7ddac2f08f)
| 2,175,089,423 | 2,165,337,174.5 | -0.45% | -0.45% to -0.44% |
|
[Django](https://github.com/django/django/tree/b501b8493f31cf8cba31211d59b3b78642b8cb02)
| 10,812,295,070 | 10,724,796,824.5 | -0.81% | -0.82% to -0.80% |
|
[SymPy](https://github.com/sympy/sympy/tree/85d3998a4517ffbc8a9709b8efa769bb417c174a)
| 34,191,919,748.5 | 33,942,049,020.5 | -0.73% | -0.73% to -0.73% |
| [OpenAI Python
SDK](https://github.com/openai/openai-python/tree/f6e8480f9a5b2a4d02b17a1da54a1e57964bc312)
| 12,034,411,695 | 11,968,351,948 | -0.55% | -0.56% to -0.54% |
|
[pytest](https://github.com/pytest-dev/pytest/tree/8b005ed7f972987033ec34dc1e68b0635fb8142b)
| 6,751,124,272 | 6,715,415,280.5 | -0.53% | -0.53% to -0.53% |
|
[aiohttp](https://github.com/aio-libs/aiohttp/tree/fe099491d275ef2c21bf683e024e34d51ac05d43)
| 3,312,726,014 | 3,297,964,882.5 | -0.44% | -0.45% to -0.44% |
|
[Ansible](https://github.com/ansible/ansible/tree/b4b2b1e82a535d27f902a71fc8396c3af138fca2)
| 11,233,185,250 | 11,165,106,765 | -0.61% | -0.61% to -0.60% |
|
[botocore](https://github.com/boto/botocore/tree/e250abbe956da791c16086575e6ddb1f2da81a78)
| 2,405,104,860.5 | 2,388,961,671 | -0.67% | -0.67% to -0.67% |
|
[Celery](https://github.com/celery/celery/tree/9c2f238334331db43f15956f131235fd3bb70fd1)
| 3,809,157,871 | 3,784,684,731.5 | -0.64% | -0.65% to -0.63% |
|
[NetworkX](https://github.com/networkx/networkx/tree/6855cd586982cc2e4710b8eb87251ccf56674bf1)
| 11,982,497,340.5 | 11,916,283,107.5 | -0.55% | -0.56% to -0.55% |
|
[Pydantic](https://github.com/pydantic/pydantic/tree/560f0f96a37d6913afdfddb2de22030ef08e68c3)
| 6,537,139,190 | 6,524,178,157 | -0.20% | -0.20% to -0.20% |
|
[scikit-learn](https://github.com/scikit-learn/scikit-learn/tree/ee56c8b5d809dc7b970a140ed501269b63c69cf8)
| 40,014,394,842 | 39,910,425,020 | -0.26% | -0.26% to -0.26% |
|
[Scrapy](https://github.com/scrapy/scrapy/tree/e63a574c767d7a9438c21db158f605f1c44c6e6a)
| 7,298,228,105 | 7,251,558,899 | -0.64% | -0.64% to -0.64% |
</details>
<details>
<summary>Memory by project</summary>
Peak RSS is the median paired change in MiB. We ran 16 pairs with
default parallelism and four with one worker. The last column estimates
retained query storage from two separate single-worker pairs. It
excludes some allocations.
| Project | Peak RSS (MiB) | Pairs with higher RSS | Peak RSS, one
worker (MiB) | Estimated query storage (MiB) |
| --- | ---: | ---: | ---: | ---: |
|
[Black](https://github.com/psf/black/tree/55996d1fb8342725c8a19b6023cd9439c3c1d6f7)
| +0.43 (+0.39%) | 11/16 | +0.05 | 0.00 (0.00%) |
|
[discord.py](https://github.com/Rapptz/discord.py/tree/65232c38702be5844cf2ce865a4777eb1928b5d0)
| +0.39 (+0.21%) | 8/16 | +0.28 | −0.05 (−0.04%) |
|
[isort](https://github.com/pycqa/isort/tree/a514c71ed86c2216d0040c7d60e302ed76a4f7ab)
| +0.29 (+0.40%) | 10/16 | 0.00 | 0.00 (0.00%) |
|
[Jinja](https://github.com/pallets/jinja/tree/5ef70112a1ff19c05324ff889dd30405b1002044)
| +0.37 (+0.45%) | 13/16 | −0.07 | 0.00 (0.00%) |
|
[pandas](https://github.com/pandas-dev/pandas/tree/a0225b65720018f4654390bb16d254daa6ee5e54)
| −0.99 (−0.19%) | 6/16 | +0.21 | −0.03 (−0.01%) |
|
[pandas-stubs](https://github.com/pandas-dev/pandas-stubs/tree/8dd6d91b1c05b26e30f7ddbe3dc3d4042a92bdd6)
| −0.05 (−0.04%) | 8/16 | +0.21 | 0.00 (0.00%) |
|
[Prefect](https://github.com/PrefectHQ/prefect/tree/a9452ac21852755311913cb27887b083b5007c03)
| +0.03 (+0.02%) | 8/16 | +0.27 | 0.00 (0.00%) |
|
[PyTorch](https://github.com/pytorch/pytorch/tree/ab92945cea42e47ae398aead69af0872d340ea0f)
| −0.02 (0.00%) | 8/16 | +0.23 | −0.05 (0.00%) |
|
[HTTPX](https://github.com/encode/httpx/tree/b5addb64f0161ff6bfe94c124ef76f6a1fba5254)
| +0.49 (+0.56%) | 11/16 | +0.05 | 0.00 (0.00%) |
|
[NumPy](https://github.com/numpy/numpy/tree/f9f1772ce010001208475f751d19253469788410)
| −1.62 (−0.69%) | 6/16 | +0.18 | 0.00 (0.00%) |
|
[FastAPI](https://github.com/fastapi/fastapi/tree/7a0a3f0b286c6c865cf4398ea0e957a87b8bffa4)
| −0.72 (−0.57%) | 7/16 | +0.26 | 0.00 (0.00%) |
|
[SQLAlchemy](https://github.com/sqlalchemy/sqlalchemy/tree/10f4d1e2d706a7722ca78e79ce05b00333a7aa5b)
| +0.13 (+0.04%) | 8/16 | +0.19 | −0.06 (−0.02%) |
|
[Rich](https://github.com/Textualize/rich/tree/9d8f9a372cc5916fd4781fec207ced7ddac2f08f)
| −0.21 (−0.20%) | 7/16 | +0.17 | 0.00 (0.00%) |
|
[Django](https://github.com/django/django/tree/b501b8493f31cf8cba31211d59b3b78642b8cb02)
| +0.95 (+0.34%) | 10/16 | +0.02 | −0.01 (0.00%) |
|
[SymPy](https://github.com/sympy/sympy/tree/85d3998a4517ffbc8a9709b8efa769bb417c174a)
| −0.60 (−0.09%) | 7/16 | +0.28 | −0.03 (−0.01%) |
| [OpenAI Python
SDK](https://github.com/openai/openai-python/tree/f6e8480f9a5b2a4d02b17a1da54a1e57964bc312)
| −0.32 (−0.11%) | 5/16 | +0.14 | −0.01 (−0.01%) |
|
[pytest](https://github.com/pytest-dev/pytest/tree/8b005ed7f972987033ec34dc1e68b0635fb8142b)
| +0.21 (+0.10%) | 9/16 | +0.09 | −0.01 (−0.01%) |
|
[aiohttp](https://github.com/aio-libs/aiohttp/tree/fe099491d275ef2c21bf683e024e34d51ac05d43)
| +0.39 (+0.33%) | 9/16 | +0.51 | 0.00 (−0.01%) |
|
[Ansible](https://github.com/ansible/ansible/tree/b4b2b1e82a535d27f902a71fc8396c3af138fca2)
| +1.05 (+0.39%) | 10/16 | +0.21 | 0.00 (0.00%) |
|
[botocore](https://github.com/boto/botocore/tree/e250abbe956da791c16086575e6ddb1f2da81a78)
| +0.13 (+0.12%) | 11/16 | +0.13 | 0.00 (0.00%) |
|
[Celery](https://github.com/celery/celery/tree/9c2f238334331db43f15956f131235fd3bb70fd1)
| +0.82 (+0.58%) | 13/16 | +0.04 | 0.00 (0.00%) |
|
[NetworkX](https://github.com/networkx/networkx/tree/6855cd586982cc2e4710b8eb87251ccf56674bf1)
| +1.38 (+0.51%) | 13/16 | +0.04 | −0.01 (−0.01%) |
|
[Pydantic](https://github.com/pydantic/pydantic/tree/560f0f96a37d6913afdfddb2de22030ef08e68c3)
| +0.08 (+0.06%) | 9/16 | +0.12 | −0.01 (−0.01%) |
|
[scikit-learn](https://github.com/scikit-learn/scikit-learn/tree/ee56c8b5d809dc7b970a140ed501269b63c69cf8)
| +0.76 (+0.13%) | 10/16 | +0.22 | −0.03 (−0.01%) |
|
[Scrapy](https://github.com/scrapy/scrapy/tree/e63a574c767d7a9438c21db158f605f1c44c6e6a)
| −0.15 (−0.07%) | 7/16 | −0.22 | −0.01 (−0.01%) |
</details>
<!-- ty-pinned-project-instructions:end -->