[ty] Defer identity specialization when inferring collection literals (#30689)
## Summary
To infer `[1, 2]`, ty needs the type parameter of `list`, but not an
instance of `list[T]`. Previously, we constructed that instance for
every list, set and dictionary literal, even when we inferred the type
from the elements alone.
We now read the type parameters from the class and construct the
instance on demand. We still use it to infer bounds from an expected
type such as `Sequence[int]` or from later uses such as
`items.append(1)`.
With default parallelism, a cold check of the 66 Python files in
[pypinyin 0.49.0](https://pypi.org/project/pypinyin/0.49.0/) used
4.27–4.36% fewer instructions in opposite-order pairs. Two consecutive
baseline runs differed by 0.02%. Median peak RSS was 301.4 MiB before
and 301.5 MiB after. All 23 diagnostics were identical.
Checking the 79 files in [pydicom
3.0.1](https://pypi.org/project/pydicom/3.0.1/) used 3.64–3.67% fewer
instructions. The baseline runs differed by 0.04%. Median peak RSS went
from 377.6 to 375.6 MiB, and all 214 diagnostics matched.
| Project | [Base
instructions](https://github.com/astral-sh/ruff/commit/cd2d973241d7f5bcba588e94cc686d9e356a4621)
| [PR
instructions](https://github.com/astral-sh/ruff/commit/dd0adca72ccdd40cea7357954985c62af8134709)
| Change | Paired range |
| --- | ---: | ---: | ---: | ---: |
|
[Black](https://github.com/psf/black/tree/55996d1fb8342725c8a19b6023cd9439c3c1d6f7)
(inconclusive) | 3,162,460,243.5 | 3,166,360,293.5 | +0.12% | 0.00% to
+0.24% |
|
[discord.py](https://github.com/Rapptz/discord.py/tree/65232c38702be5844cf2ce865a4777eb1928b5d0)
(inconclusive) | 8,157,799,374 | 8,142,518,177 | -0.19% | -0.32% to
-0.06% |
| [Home
Assistant](https://github.com/home-assistant/core/tree/c19ae285ede74cd64c3045a3a4aa43290c224f1b)
| 158,280,924,369 | 158,148,761,970.5 | -0.08% | -0.10% to -0.07% |
|
[isort](https://github.com/pycqa/isort/tree/a514c71ed86c2216d0040c7d60e302ed76a4f7ab)
(inconclusive) | 1,844,592,133.5 | 1,846,102,684.5 | +0.08% | +0.07% to
+0.09% |
|
[Jinja](https://github.com/pallets/jinja/tree/5ef70112a1ff19c05324ff889dd30405b1002044)
(inconclusive) | 1,935,230,936 | 1,936,304,169.5 | +0.06% | +0.05% to
+0.06% |
|
[pandas](https://github.com/pandas-dev/pandas/tree/a0225b65720018f4654390bb16d254daa6ee5e54)
(inconclusive) | 28,412,707,098 | 28,402,964,688.5 | -0.03% | -0.27% to
+0.21% |
|
[pandas-stubs](https://github.com/pandas-dev/pandas-stubs/tree/8dd6d91b1c05b26e30f7ddbe3dc3d4042a92bdd6)
(inconclusive) | 2,954,045,320.5 | 2,959,005,855 | +0.17% | +0.02% to
+0.31% |
|
[Prefect](https://github.com/PrefectHQ/prefect/tree/a9452ac21852755311913cb27887b083b5007c03)
| 4,939,167,160.5 | 4,935,801,303.5 | -0.07% | -0.08% to -0.06% |
|
[PyTorch](https://github.com/pytorch/pytorch/tree/ab92945cea42e47ae398aead69af0872d340ea0f)
| 95,685,176,868 | 95,591,388,266 | -0.10% | -0.12% to -0.07% |
|
[HTTPX](https://github.com/encode/httpx/tree/b5addb64f0161ff6bfe94c124ef76f6a1fba5254)
(inconclusive) | 1,672,067,683 | 1,673,857,603.5 | +0.11% | +0.08% to
+0.13% |
|
[NumPy](https://github.com/numpy/numpy/tree/f9f1772ce010001208475f751d19253469788410)
(inconclusive) | 8,523,129,621.5 | 8,488,440,052 | -0.41% | -0.87% to
+0.05% |
|
[FastAPI](https://github.com/fastapi/fastapi/tree/7a0a3f0b286c6c865cf4398ea0e957a87b8bffa4)
(inconclusive) | 2,983,610,879.5 | 2,983,711,740.5 | 0.00% | -0.06% to
+0.07% |
|
[SQLAlchemy](https://github.com/sqlalchemy/sqlalchemy/tree/10f4d1e2d706a7722ca78e79ce05b00333a7aa5b)
(inconclusive) | 21,289,752,771.5 | 21,272,006,557 | -0.08% | -0.29% to
+0.12% |
|
[Rich](https://github.com/Textualize/rich/tree/9d8f9a372cc5916fd4781fec207ced7ddac2f08f)
(inconclusive) | 2,714,115,316.5 | 2,712,326,280.5 | -0.07% | -0.21% to
+0.07% |
|
[Django](https://github.com/django/django/tree/b501b8493f31cf8cba31211d59b3b78642b8cb02)
(inconclusive) | 13,783,138,058.5 | 13,769,222,867 | -0.10% | -0.13% to
-0.08% |
|
[SymPy](https://github.com/sympy/sympy/tree/85d3998a4517ffbc8a9709b8efa769bb417c174a)
| 45,791,956,133 | 45,760,092,503 | -0.07% | -0.09% to -0.05% |
| [OpenAI Python
SDK](https://github.com/openai/openai-python/tree/f6e8480f9a5b2a4d02b17a1da54a1e57964bc312)
(inconclusive) | 15,878,496,004 | 15,862,647,326.5 | -0.10% | -0.12% to
-0.08% |
|
[pytest](https://github.com/pytest-dev/pytest/tree/8b005ed7f972987033ec34dc1e68b0635fb8142b)
| 8,765,366,342.5 | 8,778,644,268 | +0.15% | +0.09% to +0.21% |
|
[aiohttp](https://github.com/aio-libs/aiohttp/tree/fe099491d275ef2c21bf683e024e34d51ac05d43)
| 4,153,356,839.5 | 4,158,740,177 | +0.13% | +0.11% to +0.15% |
|
[Ansible](https://github.com/ansible/ansible/tree/b4b2b1e82a535d27f902a71fc8396c3af138fca2)
| 14,334,193,961 | 14,318,349,379.5 | -0.11% | -0.17% to -0.05% |
|
[botocore](https://github.com/boto/botocore/tree/e250abbe956da791c16086575e6ddb1f2da81a78)
| 3,006,799,682.5 | 3,002,813,480.5 | -0.13% | -0.21% to -0.06% |
|
[Celery](https://github.com/celery/celery/tree/9c2f238334331db43f15956f131235fd3bb70fd1)
(inconclusive) | 4,775,142,952 | 4,773,490,351.5 | -0.03% | -0.07% to
0.00% |
|
[Dask](https://github.com/dask/dask/tree/249a51a1db4261791b93b4ce04e67341729939c3)
| 33,773,095,622 | 33,677,058,192.5 | -0.28% | -0.33% to -0.24% |
|
[NetworkX](https://github.com/networkx/networkx/tree/6855cd586982cc2e4710b8eb87251ccf56674bf1)
| 15,579,848,962 | 15,541,097,217 | -0.25% | -0.27% to -0.23% |
|
[Pydantic](https://github.com/pydantic/pydantic/tree/560f0f96a37d6913afdfddb2de22030ef08e68c3)
(inconclusive) | 12,988,719,568.5 | 12,979,509,375.5 | -0.07% | -0.13%
to -0.01% |
|
[scikit-learn](https://github.com/scikit-learn/scikit-learn/tree/ee56c8b5d809dc7b970a140ed501269b63c69cf8)
| 61,281,931,046.5 | 61,200,406,300.5 | -0.13% | -0.14% to -0.13% |
|
[Scrapy](https://github.com/scrapy/scrapy/tree/e63a574c767d7a9438c21db158f605f1c44c6e6a)
(inconclusive) | 8,932,252,854.5 | 8,920,905,375 | -0.13% | -0.14% to
-0.11% |
|
[Transformers](https://github.com/huggingface/transformers/tree/3a134bc37f7c22c801b618e289b0255425f22647)
| 99,855,962,894 | 99,754,293,554.5 | -0.10% | -0.15% to -0.05% |