[ty] Skip the builtins recursion check outside module scope (#30699)
## Summary
When resolving a name like `int` in a class body, ty looks up the global
scope of `builtins` just to check whether it’s already in that scope. It
then looks up the name itself.
Only a module scope can match. We now skip the first lookup in nested
scopes. The recursion check still runs at module level, and
project-defined builtins still take precedence.
On a cold check of `PySide6/QtOpenGL.pyi` from PySide6 Essentials
6.10.0, Cachegrind counted 2.36% fewer instructions in both orders,
versus 0.26% variation when repeating the baseline. Diagnostics and exit
codes matched. Median native peak RSS over three runs was 137.6 MiB
before and 138.7 MiB after. The two checks of all 58 stubs disagreed
(−1.40% and +0.31%).
## Pinned projects
Mean instructions fell on every project, though nine results were
inconclusive. Diagnostics and exit codes matched on all 28. The counts
are whole-process Cachegrind means from two cold checks per revision in
opposite orders with default concurrency.
| Project | [Base
instructions](https://github.com/astral-sh/ruff/commit/cd2d973241d7f5bcba588e94cc686d9e356a4621)
| [PR
instructions](https://github.com/astral-sh/ruff/commit/543bdf62d60a26a61c56ad540726425b17fb9aa1)
| Change | Paired range |
| --- | ---: | ---: | ---: | ---: |
|
[Black](https://github.com/psf/black/tree/55996d1fb8342725c8a19b6023cd9439c3c1d6f7)
| 3,162,392,949 | 3,157,823,425 | -0.14% | -0.16% to -0.13% |
|
[discord.py](https://github.com/Rapptz/discord.py/tree/65232c38702be5844cf2ce865a4777eb1928b5d0)
| 8,146,827,783.5 | 8,121,981,213.5 | -0.30% | -0.40% to -0.21% |
| [Home
Assistant](https://github.com/home-assistant/core/tree/c19ae285ede74cd64c3045a3a4aa43290c224f1b)
| 158,290,683,515 | 157,916,277,604 | -0.24% | -0.24% to -0.23% |
|
[isort](https://github.com/pycqa/isort/tree/a514c71ed86c2216d0040c7d60e302ed76a4f7ab)
| 1,853,479,193 | 1,851,151,202.5 | -0.13% | -0.18% to -0.08% |
|
[Jinja](https://github.com/pallets/jinja/tree/5ef70112a1ff19c05324ff889dd30405b1002044)
| 1,937,188,139 | 1,932,054,544.5 | -0.26% | -0.30% to -0.23% |
|
[pandas](https://github.com/pandas-dev/pandas/tree/a0225b65720018f4654390bb16d254daa6ee5e54)
| 28,415,700,276.5 | 28,359,834,724 | -0.20% | -0.20% to -0.19% |
|
[pandas-stubs](https://github.com/pandas-dev/pandas-stubs/tree/8dd6d91b1c05b26e30f7ddbe3dc3d4042a92bdd6)
(inconclusive) | 2,957,623,719.5 | 2,945,052,659 | -0.42% | -0.60% to
-0.25% |
|
[Prefect](https://github.com/PrefectHQ/prefect/tree/a9452ac21852755311913cb27887b083b5007c03)
(inconclusive) | 4,936,950,950.5 | 4,930,672,518.5 | -0.13% | -0.19% to
-0.07% |
|
[PyTorch](https://github.com/pytorch/pytorch/tree/ab92945cea42e47ae398aead69af0872d340ea0f)
| 95,698,214,941 | 95,428,531,055.5 | -0.28% | -0.33% to -0.24% |
|
[HTTPX](https://github.com/encode/httpx/tree/b5addb64f0161ff6bfe94c124ef76f6a1fba5254)
(inconclusive) | 1,676,518,512.5 | 1,672,003,374.5 | -0.27% | -0.27% to
-0.27% |
|
[NumPy](https://github.com/numpy/numpy/tree/f9f1772ce010001208475f751d19253469788410)
(inconclusive) | 8,490,358,999.5 | 8,429,770,392 | -0.71% | -1.09% to
-0.34% |
|
[FastAPI](https://github.com/fastapi/fastapi/tree/7a0a3f0b286c6c865cf4398ea0e957a87b8bffa4)
| 2,984,070,153 | 2,976,325,207 | -0.26% | -0.26% to -0.26% |
|
[SQLAlchemy](https://github.com/sqlalchemy/sqlalchemy/tree/10f4d1e2d706a7722ca78e79ce05b00333a7aa5b)
| 21,258,868,290 | 21,227,619,834 | -0.15% | -0.16% to -0.13% |
|
[Rich](https://github.com/Textualize/rich/tree/9d8f9a372cc5916fd4781fec207ced7ddac2f08f)
(inconclusive) | 2,712,173,436.5 | 2,708,700,497.5 | -0.13% | -0.23% to
-0.02% |
|
[Django](https://github.com/django/django/tree/b501b8493f31cf8cba31211d59b3b78642b8cb02)
| 13,790,925,573 | 13,754,981,181.5 | -0.26% | -0.39% to -0.13% |
|
[SymPy](https://github.com/sympy/sympy/tree/85d3998a4517ffbc8a9709b8efa769bb417c174a)
| 45,788,014,159 | 45,755,072,455.5 | -0.07% | -0.13% to -0.01% |
| [OpenAI Python
SDK](https://github.com/openai/openai-python/tree/f6e8480f9a5b2a4d02b17a1da54a1e57964bc312)
| 15,883,999,826.5 | 15,846,160,786 | -0.24% | -0.28% to -0.20% |
|
[pytest](https://github.com/pytest-dev/pytest/tree/8b005ed7f972987033ec34dc1e68b0635fb8142b)
(inconclusive) | 8,774,037,412.5 | 8,754,711,493.5 | -0.22% | -0.29% to
-0.15% |
|
[aiohttp](https://github.com/aio-libs/aiohttp/tree/fe099491d275ef2c21bf683e024e34d51ac05d43)
| 4,152,333,353.5 | 4,145,349,514 | -0.17% | -0.20% to -0.13% |
|
[Ansible](https://github.com/ansible/ansible/tree/b4b2b1e82a535d27f902a71fc8396c3af138fca2)
| 14,330,560,953 | 14,299,128,112 | -0.22% | -0.29% to -0.15% |
|
[botocore](https://github.com/boto/botocore/tree/e250abbe956da791c16086575e6ddb1f2da81a78)
| 3,005,424,370.5 | 3,001,326,094 | -0.14% | -0.15% to -0.12% |
|
[Celery](https://github.com/celery/celery/tree/9c2f238334331db43f15956f131235fd3bb70fd1)
(inconclusive) | 4,776,352,741.5 | 4,766,109,128.5 | -0.21% | -0.31% to
-0.12% |
|
[Dask](https://github.com/dask/dask/tree/249a51a1db4261791b93b4ce04e67341729939c3)
| 33,753,350,660.5 | 33,703,646,196 | -0.15% | -0.19% to -0.10% |
|
[NetworkX](https://github.com/networkx/networkx/tree/6855cd586982cc2e4710b8eb87251ccf56674bf1)
| 15,569,480,228.5 | 15,549,609,857.5 | -0.13% | -0.14% to -0.11% |
|
[Pydantic](https://github.com/pydantic/pydantic/tree/560f0f96a37d6913afdfddb2de22030ef08e68c3)
(inconclusive) | 12,985,037,680.5 | 12,983,556,660 | -0.01% | -0.04% to
+0.02% |
|
[scikit-learn](https://github.com/scikit-learn/scikit-learn/tree/ee56c8b5d809dc7b970a140ed501269b63c69cf8)
(inconclusive) | 61,268,427,799 | 61,255,408,686 | -0.02% | -0.09% to
+0.05% |
|
[Scrapy](https://github.com/scrapy/scrapy/tree/e63a574c767d7a9438c21db158f605f1c44c6e6a)
| 8,933,912,999 | 8,912,574,838 | -0.24% | -0.25% to -0.23% |
|
[Transformers](https://github.com/huggingface/transformers/tree/3a134bc37f7c22c801b618e289b0255425f22647)
| 99,890,523,572 | 99,562,665,166 | -0.33% | -0.38% to -0.27% |