[ty] Deduplicate lower bound types after filtering by provenance (#30786)
## Summary
When ty infers the element type of a list like `["yī"]`, the solver puts
each lower bound and its provenance in an ordered set. It then selects
the bounds it needs and builds a second ordered set of their types. Even
a single bound goes through both sets.
`LowerBound` keeps one clause inline and returns its type directly if it
matches. For multiple clauses, we preserve insertion order and
deduplicate the types after filtering by provenance. The same type can
therefore contribute to both inference and validity checks. Duplicate
clauses can take up space until we finish solving for that type
variable.
<!-- ty-pinned-project-instructions:start -->
Across 28 projects, the mean change in instructions was −0.22% and the
median was −0.21%. Weighting each project by its median instruction
count on main gives a change of −0.27%.
Outside the 28 projects below, we checked the full pypinyin source
distributions on Python 3.11. [Version
0.49.0](https://pypi.org/project/pypinyin/0.49.0/) used 4.10% fewer
instructions and [0.55.0](https://pypi.org/project/pypinyin/0.55.0/)
used 0.82% fewer.
<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/cde61b53f6611abc526c74a341e361294c68cb92)
| [PR
instructions](https://github.com/astral-sh/ruff/commit/a782f07efb782efc0021bbeb7f49e719ce52f49d)
| Change | Paired range |
| --- | ---: | ---: | ---: | ---: |
|
[Black](https://github.com/psf/black/tree/55996d1fb8342725c8a19b6023cd9439c3c1d6f7)
| 2,586,850,288.5 | 2,583,353,076 | -0.14% | -0.14% to -0.12% |
|
[discord.py](https://github.com/Rapptz/discord.py/tree/65232c38702be5844cf2ce865a4777eb1928b5d0)
| 6,296,548,142.5 | 6,286,168,004.5 | -0.16% | -0.17% to -0.16% |
| [Home
Assistant](https://github.com/home-assistant/core/tree/c19ae285ede74cd64c3045a3a4aa43290c224f1b)
| 126,953,006,086 | 126,692,041,570 | -0.21% | -0.21% to -0.20% |
|
[isort](https://github.com/pycqa/isort/tree/a514c71ed86c2216d0040c7d60e302ed76a4f7ab)
| 1,406,417,603.5 | 1,405,076,548.5 | -0.09% | -0.10% to -0.09% |
|
[Jinja](https://github.com/pallets/jinja/tree/5ef70112a1ff19c05324ff889dd30405b1002044)
| 1,482,678,724.5 | 1,479,097,236 | -0.24% | -0.25% to -0.24% |
|
[pandas](https://github.com/pandas-dev/pandas/tree/a0225b65720018f4654390bb16d254daa6ee5e54)
| 22,226,938,807.5 | 22,177,326,609.5 | -0.22% | -0.23% to -0.22% |
|
[pandas-stubs](https://github.com/pandas-dev/pandas-stubs/tree/8dd6d91b1c05b26e30f7ddbe3dc3d4042a92bdd6)
| 2,519,812,144.5 | 2,520,344,504 | +0.02% | +0.02% to +0.03% |
|
[Prefect](https://github.com/PrefectHQ/prefect/tree/a9452ac21852755311913cb27887b083b5007c03)
| 4,173,868,409.5 | 4,166,261,058.5 | -0.18% | -0.19% to -0.18% |
|
[PyTorch](https://github.com/pytorch/pytorch/tree/ab92945cea42e47ae398aead69af0872d340ea0f)
| 75,060,078,411.5 | 74,862,249,179 | -0.26% | -0.27% to -0.26% |
|
[HTTPX](https://github.com/encode/httpx/tree/b5addb64f0161ff6bfe94c124ef76f6a1fba5254)
| 1,312,452,865 | 1,310,826,958.5 | -0.12% | -0.13% to -0.12% |
|
[NumPy](https://github.com/numpy/numpy/tree/f9f1772ce010001208475f751d19253469788410)
| 7,841,215,921 | 7,830,738,832 | -0.14% | -0.15% to -0.12% |
|
[FastAPI](https://github.com/fastapi/fastapi/tree/7a0a3f0b286c6c865cf4398ea0e957a87b8bffa4)
| 2,476,392,376 | 2,469,333,380 | -0.28% | -0.31% to -0.27% |
|
[SQLAlchemy](https://github.com/sqlalchemy/sqlalchemy/tree/10f4d1e2d706a7722ca78e79ce05b00333a7aa5b)
| 16,339,288,059.5 | 16,290,757,925.5 | -0.30% | -0.30% to -0.29% |
|
[Rich](https://github.com/Textualize/rich/tree/9d8f9a372cc5916fd4781fec207ced7ddac2f08f)
| 2,177,372,481 | 2,174,810,630 | -0.12% | -0.13% to -0.11% |
|
[Django](https://github.com/django/django/tree/b501b8493f31cf8cba31211d59b3b78642b8cb02)
| 10,832,827,077 | 10,806,531,287.5 | -0.24% | -0.24% to -0.24% |
|
[SymPy](https://github.com/sympy/sympy/tree/85d3998a4517ffbc8a9709b8efa769bb417c174a)
| 34,306,558,645 | 34,221,675,355.5 | -0.25% | -0.27% to -0.22% |
| [OpenAI Python
SDK](https://github.com/openai/openai-python/tree/f6e8480f9a5b2a4d02b17a1da54a1e57964bc312)
| 12,056,858,092.5 | 12,054,587,406 | -0.02% | -0.03% to -0.01% |
|
[pytest](https://github.com/pytest-dev/pytest/tree/8b005ed7f972987033ec34dc1e68b0635fb8142b)
| 6,782,984,306.5 | 6,765,925,929.5 | -0.25% | -0.26% to -0.25% |
|
[aiohttp](https://github.com/aio-libs/aiohttp/tree/fe099491d275ef2c21bf683e024e34d51ac05d43)
| 3,329,272,440 | 3,322,849,223.5 | -0.19% | -0.21% to -0.18% |
|
[Ansible](https://github.com/ansible/ansible/tree/b4b2b1e82a535d27f902a71fc8396c3af138fca2)
| 11,310,094,245 | 11,284,656,496 | -0.23% | -0.23% to -0.22% |
|
[botocore](https://github.com/boto/botocore/tree/e250abbe956da791c16086575e6ddb1f2da81a78)
| 2,417,070,779 | 2,412,785,481 | -0.18% | -0.18% to -0.18% |
|
[Celery](https://github.com/celery/celery/tree/9c2f238334331db43f15956f131235fd3bb70fd1)
| 3,826,326,840 | 3,818,460,037 | -0.21% | -0.22% to -0.18% |
|
[Dask](https://github.com/dask/dask/tree/249a51a1db4261791b93b4ce04e67341729939c3)
| 26,231,795,710.5 | 26,122,649,135.5 | -0.41% | -0.43% to -0.41% |
|
[NetworkX](https://github.com/networkx/networkx/tree/6855cd586982cc2e4710b8eb87251ccf56674bf1)
| 12,053,738,353.5 | 12,007,725,610 | -0.38% | -0.38% to -0.38% |
|
[Pydantic](https://github.com/pydantic/pydantic/tree/560f0f96a37d6913afdfddb2de22030ef08e68c3)
| 6,560,531,684 | 6,551,341,164 | -0.14% | -0.15% to -0.14% |
|
[scikit-learn](https://github.com/scikit-learn/scikit-learn/tree/ee56c8b5d809dc7b970a140ed501269b63c69cf8)
| 40,226,317,687.5 | 40,049,042,184.5 | -0.44% | -0.44% to -0.44% |
|
[Scrapy](https://github.com/scrapy/scrapy/tree/e63a574c767d7a9438c21db158f605f1c44c6e6a)
| 7,326,797,497.5 | 7,299,450,113.5 | -0.37% | -0.37% to -0.37% |
|
[Transformers](https://github.com/huggingface/transformers/tree/3a134bc37f7c22c801b618e289b0255425f22647)
| 78,667,657,629 | 78,409,443,976.5 | -0.33% | -0.33% to -0.33% |
</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.53 (−0.47%) | 6/16 | +0.07 | 0.00 (0.00%) |
|
[discord.py](https://github.com/Rapptz/discord.py/tree/65232c38702be5844cf2ce865a4777eb1928b5d0)
| −0.07 (−0.04%) | 8/16 | −0.12 | 0.00 (0.00%) |
| [Home
Assistant](https://github.com/home-assistant/core/tree/c19ae285ede74cd64c3045a3a4aa43290c224f1b)
| −0.53 (−0.02%) | 5/16 | +0.09 | 0.00 (0.00%) |
|
[isort](https://github.com/pycqa/isort/tree/a514c71ed86c2216d0040c7d60e302ed76a4f7ab)
| +0.30 (+0.42%) | 11/16 | −0.15 | 0.00 (0.00%) |
|
[Jinja](https://github.com/pallets/jinja/tree/5ef70112a1ff19c05324ff889dd30405b1002044)
| +0.05 (+0.07%) | 9/16 | −0.06 | 0.00 (0.00%) |
|
[pandas](https://github.com/pandas-dev/pandas/tree/a0225b65720018f4654390bb16d254daa6ee5e54)
| +0.51 (+0.10%) | 9/16 | −0.23 | 0.00 (0.00%) |
|
[pandas-stubs](https://github.com/pandas-dev/pandas-stubs/tree/8dd6d91b1c05b26e30f7ddbe3dc3d4042a92bdd6)
| +0.12 (+0.09%) | 8/16 | −0.19 | 0.00 (0.00%) |
|
[Prefect](https://github.com/PrefectHQ/prefect/tree/a9452ac21852755311913cb27887b083b5007c03)
| +0.05 (+0.02%) | 8/16 | −0.24 | 0.00 (0.00%) |
|
[PyTorch](https://github.com/pytorch/pytorch/tree/ab92945cea42e47ae398aead69af0872d340ea0f)
| −0.43 (−0.03%) | 8/16 | +0.25 | 0.00 (0.00%) |
|
[HTTPX](https://github.com/encode/httpx/tree/b5addb64f0161ff6bfe94c124ef76f6a1fba5254)
| +0.05 (+0.06%) | 9/16 | −0.13 | 0.00 (0.00%) |
|
[NumPy](https://github.com/numpy/numpy/tree/f9f1772ce010001208475f751d19253469788410)
| +2.93 (+1.26%) | 10/16 | +0.82 | 0.00 (0.00%) |
|
[FastAPI](https://github.com/fastapi/fastapi/tree/7a0a3f0b286c6c865cf4398ea0e957a87b8bffa4)
| −0.34 (−0.27%) | 6/16 | +0.23 | 0.00 (0.00%) |
|
[SQLAlchemy](https://github.com/sqlalchemy/sqlalchemy/tree/10f4d1e2d706a7722ca78e79ce05b00333a7aa5b)
| −2.53 (−0.69%) | 5/16 | −0.06 | 0.00 (0.00%) |
|
[Rich](https://github.com/Textualize/rich/tree/9d8f9a372cc5916fd4781fec207ced7ddac2f08f)
| −0.38 (−0.37%) | 6/16 | +0.14 | 0.00 (0.00%) |
|
[Django](https://github.com/django/django/tree/b501b8493f31cf8cba31211d59b3b78642b8cb02)
| −0.64 (−0.23%) | 5/16 | −0.12 | 0.00 (0.00%) |
|
[SymPy](https://github.com/sympy/sympy/tree/85d3998a4517ffbc8a9709b8efa769bb417c174a)
| −1.49 (−0.23%) | 4/16 | +0.03 | 0.00 (0.00%) |
| [OpenAI Python
SDK](https://github.com/openai/openai-python/tree/f6e8480f9a5b2a4d02b17a1da54a1e57964bc312)
| −1.07 (−0.36%) | 4/16 | +0.02 | 0.00 (0.00%) |
|
[pytest](https://github.com/pytest-dev/pytest/tree/8b005ed7f972987033ec34dc1e68b0635fb8142b)
| −0.17 (−0.08%) | 7/16 | +0.07 | 0.00 (0.00%) |
|
[aiohttp](https://github.com/aio-libs/aiohttp/tree/fe099491d275ef2c21bf683e024e34d51ac05d43)
| +0.34 (+0.29%) | 9/16 | −0.09 | 0.00 (0.00%) |
|
[Ansible](https://github.com/ansible/ansible/tree/b4b2b1e82a535d27f902a71fc8396c3af138fca2)
| +0.20 (+0.07%) | 10/16 | −0.02 | 0.00 (0.00%) |
|
[botocore](https://github.com/boto/botocore/tree/e250abbe956da791c16086575e6ddb1f2da81a78)
| −0.17 (−0.15%) | 6/16 | 0.00 | 0.00 (0.00%) |
|
[Celery](https://github.com/celery/celery/tree/9c2f238334331db43f15956f131235fd3bb70fd1)
| +0.24 (+0.17%) | 10/16 | −0.01 | 0.00 (0.00%) |
|
[Dask](https://github.com/dask/dask/tree/249a51a1db4261791b93b4ce04e67341729939c3)
| +1.38 (+0.24%) | 9/16 | −0.09 | 0.00 (0.00%) |
|
[NetworkX](https://github.com/networkx/networkx/tree/6855cd586982cc2e4710b8eb87251ccf56674bf1)
| −0.53 (−0.20%) | 5/16 | +0.15 | 0.00 (0.00%) |
|
[Pydantic](https://github.com/pydantic/pydantic/tree/560f0f96a37d6913afdfddb2de22030ef08e68c3)
| +0.07 (+0.06%) | 8/16 | +0.10 | 0.00 (0.00%) |
|
[scikit-learn](https://github.com/scikit-learn/scikit-learn/tree/ee56c8b5d809dc7b970a140ed501269b63c69cf8)
| −0.15 (−0.03%) | 7/16 | −0.06 | 0.00 (0.00%) |
|
[Scrapy](https://github.com/scrapy/scrapy/tree/e63a574c767d7a9438c21db158f605f1c44c6e6a)
| +0.32 (+0.15%) | 10/16 | +0.31 | 0.00 (0.00%) |
|
[Transformers](https://github.com/huggingface/transformers/tree/3a134bc37f7c22c801b618e289b0255425f22647)
| +1.11 (+0.07%) | 8/16 | +0.20 | 0.00 (0.00%) |
</details>
<!-- ty-pinned-project-instructions:end -->