[ty] Skip union comparisons between intersections with unrelated class bounds (#30737)
## Summary
After a failed `isinstance` check, ty can end up comparing `A & ~C` with
`B & ~C` for every pair of classes in a union. LlamaIndex does this
repeatedly as it checks the type of each streaming event.
Skip the comparison when both intersections exclude the same nominal
types and each requires a class unrelated to every positive bound on the
other side. Neither alternative can subsume the other or restore an
excluded type.
Cold checks of Pydantic AI with its default dependencies used 8.72–8.74%
fewer instructions on 2.68.0 and 9.32–9.40% fewer on 1.31.0. Median peak
RSS fell from 703 to 634 MiB and from 462 to 440 MiB, respectively.
Diagnostics matched.
With openai 3.51.0, llama-index-llms-openai 0.9.0 used 18.62–18.67%
fewer instructions and 11 MiB less peak RSS.
<!-- ty-pinned-project-instructions:start -->
| Project | [Base
instructions](https://github.com/astral-sh/ruff/commit/7be385f21d0848e58668b75cd90d0927637c84b6)
| [PR
instructions](https://github.com/astral-sh/ruff/commit/ad9b35a7114246799d9f9541a947353026df4e32)
| Change | Paired range |
| --- | ---: | ---: | ---: | ---: |
|
[Black](https://github.com/psf/black/tree/55996d1fb8342725c8a19b6023cd9439c3c1d6f7)
(inconclusive) | 3,098,543,693 | 3,096,314,192 | -0.07% | -0.08% to
-0.07% |
|
[discord.py](https://github.com/Rapptz/discord.py/tree/65232c38702be5844cf2ce865a4777eb1928b5d0)
(inconclusive) | 7,964,719,708 | 7,956,557,079.5 | -0.10% | -0.22% to
+0.02% |
| [Home
Assistant](https://github.com/home-assistant/core/tree/c19ae285ede74cd64c3045a3a4aa43290c224f1b)
| 152,971,879,231 | 152,502,988,869.5 | -0.31% | -0.32% to -0.29% |
|
[isort](https://github.com/pycqa/isort/tree/a514c71ed86c2216d0040c7d60e302ed76a4f7ab)
(inconclusive) | 1,799,531,070.5 | 1,799,543,090 | 0.00% | -0.02% to
+0.02% |
|
[Jinja](https://github.com/pallets/jinja/tree/5ef70112a1ff19c05324ff889dd30405b1002044)
(inconclusive) | 1,889,467,554.5 | 1,887,690,719 | -0.09% | -0.11% to
-0.08% |
|
[pandas](https://github.com/pandas-dev/pandas/tree/a0225b65720018f4654390bb16d254daa6ee5e54)
| 27,313,567,535 | 27,248,154,967.5 | -0.24% | -0.26% to -0.21% |
|
[pandas-stubs](https://github.com/pandas-dev/pandas-stubs/tree/8dd6d91b1c05b26e30f7ddbe3dc3d4042a92bdd6)
(inconclusive) | 2,866,591,743 | 2,867,771,255 | +0.04% | -0.19% to
+0.27% |
|
[Prefect](https://github.com/PrefectHQ/prefect/tree/a9452ac21852755311913cb27887b083b5007c03)
(inconclusive) | 4,849,949,527.5 | 4,848,774,538.5 | -0.02% | -0.05% to
0.00% |
|
[PyTorch](https://github.com/pytorch/pytorch/tree/ab92945cea42e47ae398aead69af0872d340ea0f)
| 92,783,458,106.5 | 92,632,864,124.5 | -0.16% | -0.24% to -0.08% |
|
[HTTPX](https://github.com/encode/httpx/tree/b5addb64f0161ff6bfe94c124ef76f6a1fba5254)
(inconclusive) | 1,643,085,759 | 1,643,028,783.5 | 0.00% | -0.01% to
+0.01% |
|
[NumPy](https://github.com/numpy/numpy/tree/f9f1772ce010001208475f751d19253469788410)
(inconclusive) | 8,098,897,568.5 | 8,084,358,639 | -0.18% | -0.32% to
-0.04% |
|
[FastAPI](https://github.com/fastapi/fastapi/tree/7a0a3f0b286c6c865cf4398ea0e957a87b8bffa4)
| 2,934,441,480 | 2,935,344,017 | +0.03% | +0.02% to +0.04% |
|
[SQLAlchemy](https://github.com/sqlalchemy/sqlalchemy/tree/10f4d1e2d706a7722ca78e79ce05b00333a7aa5b)
(inconclusive) | 20,621,834,886.5 | 20,594,435,999.5 | -0.13% | -0.16%
to -0.11% |
|
[Rich](https://github.com/Textualize/rich/tree/9d8f9a372cc5916fd4781fec207ced7ddac2f08f)
(inconclusive) | 2,653,742,418.5 | 2,650,376,459 | -0.13% | -0.21% to
-0.04% |
|
[Django](https://github.com/django/django/tree/b501b8493f31cf8cba31211d59b3b78642b8cb02)
(inconclusive) | 13,338,657,303 | 13,366,268,478 | +0.21% | +0.14% to
+0.27% |
|
[SymPy](https://github.com/sympy/sympy/tree/85d3998a4517ffbc8a9709b8efa769bb417c174a)
(inconclusive) | 44,318,485,707.5 | 44,333,497,423.5 | +0.03% | +0.01%
to +0.06% |
| [OpenAI Python
SDK](https://github.com/openai/openai-python/tree/f6e8480f9a5b2a4d02b17a1da54a1e57964bc312)
| 15,297,555,196 | 15,250,935,748.5 | -0.30% | -0.34% to -0.27% |
|
[pytest](https://github.com/pytest-dev/pytest/tree/8b005ed7f972987033ec34dc1e68b0635fb8142b)
(inconclusive) | 8,543,164,960.5 | 8,538,275,393.5 | -0.06% | -0.17% to
+0.06% |
|
[aiohttp](https://github.com/aio-libs/aiohttp/tree/fe099491d275ef2c21bf683e024e34d51ac05d43)
(inconclusive) | 4,045,737,959.5 | 4,048,145,007 | +0.06% | -0.01% to
+0.13% |
|
[Ansible](https://github.com/ansible/ansible/tree/b4b2b1e82a535d27f902a71fc8396c3af138fca2)
(inconclusive) | 13,973,505,769.5 | 13,964,521,430 | -0.06% | -0.11% to
-0.02% |
|
[botocore](https://github.com/boto/botocore/tree/e250abbe956da791c16086575e6ddb1f2da81a78)
(inconclusive) | 2,920,621,577 | 2,918,006,803.5 | -0.09% | -0.12% to
-0.06% |
|
[Celery](https://github.com/celery/celery/tree/9c2f238334331db43f15956f131235fd3bb70fd1)
(inconclusive) | 4,630,114,219 | 4,628,868,920 | -0.03% | -0.03% to
-0.02% |
|
[Dask](https://github.com/dask/dask/tree/249a51a1db4261791b93b4ce04e67341729939c3)
| 32,361,140,180 | 32,384,392,966 | +0.07% | +0.05% to +0.09% |
|
[NetworkX](https://github.com/networkx/networkx/tree/6855cd586982cc2e4710b8eb87251ccf56674bf1)
(inconclusive) | 15,092,823,909 | 15,088,492,371.5 | -0.03% | -0.04% to
-0.02% |
|
[Pydantic](https://github.com/pydantic/pydantic/tree/560f0f96a37d6913afdfddb2de22030ef08e68c3)
(inconclusive) | 12,907,309,661 | 12,910,268,535.5 | +0.02% | +0.01% to
+0.03% |
|
[scikit-learn](https://github.com/scikit-learn/scikit-learn/tree/ee56c8b5d809dc7b970a140ed501269b63c69cf8)
(inconclusive) | 57,161,070,601 | 57,175,139,540.5 | +0.02% | -0.04% to
+0.08% |
|
[Scrapy](https://github.com/scrapy/scrapy/tree/e63a574c767d7a9438c21db158f605f1c44c6e6a)
(inconclusive) | 8,671,257,021 | 8,673,421,330 | +0.03% | -0.10% to
+0.15% |
|
[Transformers](https://github.com/huggingface/transformers/tree/3a134bc37f7c22c801b618e289b0255425f22647)
(inconclusive) | 95,900,365,487 | 95,872,345,626 | -0.03% | -0.07% to
+0.01% |
<!-- ty-pinned-project-instructions:end -->