ruff
247e0f61 - [ty] Reuse generic call solutions with expected return types and bounds (#30909)

Commit
1 day ago
[ty] Reuse generic call solutions with expected return types and bounds (#30909) ## Summary ty [reuses inferred type arguments](https://github.com/astral-sh/ruff/pull/30815) across calls with the same argument types. It skips the cache when the caller expects a particular return type, since that can change the answer. It also skips bounded type variables to avoid solving rejected overloads twice. We now key the cache on the expected type and how it guides inference, and reuse solutions for bounded type variables too. In [Pydantic](https://github.com/pydantic/pydantic/blob/560f0f96a37d6913afdfddb2de22030ef08e68c3/pydantic/experimental/pipeline.py#L486), several branches assign `s.copy()` back to a `CoreSchema`. We can infer the return type once for each of the 59 possible types of `s`, while still checking arguments and reporting errors at each call site. | Platform | Workers | [Base instructions](https://github.com/astral-sh/ruff/commit/0cc5087b01b99157a18429c5115a98ba393fcbcf) | [PR instructions](https://github.com/astral-sh/ruff/commit/8fc2de0ff07fbc0a546e52838fff0ba82e67d7d3) | Change | Paired range | | --- | --- | ---: | ---: | ---: | ---: | | Linux (x86-64) | Default | 38,012,906,501 | 36,818,217,807.5 | -3.11% | -3.22% to -3.05% | | Linux (x86-64) | One | 37,919,447,354 | 36,714,177,437 | -3.18% | -3.18% to -3.18% | | macOS (ARM64) | Default | 46,732,258,132 | 45,305,441,196 | -3.46% | -3.72% to -2.80% | | macOS (ARM64) | One | 45,137,917,098 | 43,589,956,935 | -3.47% | -3.68% to -3.07% | <details> <summary>Instruction counts by project</summary> | Project | [Base instructions](https://github.com/astral-sh/ruff/commit/0cc5087b01b99157a18429c5115a98ba393fcbcf) | [PR instructions](https://github.com/astral-sh/ruff/commit/8fc2de0ff07fbc0a546e52838fff0ba82e67d7d3) | Change | Paired range | Peak RSS change | | --- | ---: | ---: | ---: | ---: | ---: | | [Black](https://github.com/psf/black/tree/55996d1fb8342725c8a19b6023cd9439c3c1d6f7) | 3,339,984,697 | 3,295,731,576.5 | -1.32% | -1.33% to -1.26% | -0.10% | | [discord.py](https://github.com/Rapptz/discord.py/tree/65232c38702be5844cf2ce865a4777eb1928b5d0) | 8,024,761,617 | 7,863,043,758 | -2.01% | -2.10% to -1.91% | +0.40% | | [Home Assistant](https://github.com/home-assistant/core/tree/c19ae285ede74cd64c3045a3a4aa43290c224f1b) | 175,347,669,898.5 | 172,621,949,396 | -1.61% | -1.78% to -1.33% | +0.32% | | [isort](https://github.com/pycqa/isort/tree/a514c71ed86c2216d0040c7d60e302ed76a4f7ab) | 1,785,818,931 | 1,759,137,529 | -1.53% | -1.67% to -1.46% | +0.01% | | [Jinja](https://github.com/pallets/jinja/tree/5ef70112a1ff19c05324ff889dd30405b1002044) | 1,986,576,533.5 | 1,958,664,078 | -1.38% | -1.42% to -1.30% | -0.07% | | [pandas](https://github.com/pandas-dev/pandas/tree/a0225b65720018f4654390bb16d254daa6ee5e54) | 28,435,311,732.5 | 27,820,742,462 | -2.18% | -2.38% to -1.91% | +1.07% | | [pandas-stubs](https://github.com/pandas-dev/pandas-stubs/tree/8dd6d91b1c05b26e30f7ddbe3dc3d4042a92bdd6) | 3,226,177,326.5 | 3,216,437,115.5 | -0.31% | -1.18% to -0.02% | +0.40% | | [Prefect](https://github.com/PrefectHQ/prefect/tree/a9452ac21852755311913cb27887b083b5007c03) | 5,614,953,208.5 | 5,572,525,040.5 | -0.76% | -0.83% to -0.69% | +0.29% | | [PyTorch](https://github.com/pytorch/pytorch/tree/ab92945cea42e47ae398aead69af0872d340ea0f) | 97,849,686,826 | 95,577,225,341 | -2.40% | -2.68% to -2.30% | +0.40% | | [HTTPX](https://github.com/encode/httpx/tree/b5addb64f0161ff6bfe94c124ef76f6a1fba5254) | 1,781,448,156.5 | 1,750,390,415 | -1.71% | -1.79% to -1.26% | +0.00% | | [NumPy](https://github.com/numpy/numpy/tree/f9f1772ce010001208475f751d19253469788410) | 9,196,192,190.5 | 9,174,892,667 | -0.23% | -0.33% to -0.21% | +0.49% | | [FastAPI](https://github.com/fastapi/fastapi/tree/7a0a3f0b286c6c865cf4398ea0e957a87b8bffa4) | 3,269,196,964.5 | 3,254,199,025 | -0.38% | -0.77% to -0.31% | -0.02% | | [SQLAlchemy](https://github.com/sqlalchemy/sqlalchemy/tree/10f4d1e2d706a7722ca78e79ce05b00333a7aa5b) | 20,663,315,100 | 20,200,133,451.5 | -2.23% | -2.39% to -2.16% | +0.54% | | [Rich](https://github.com/Textualize/rich/tree/9d8f9a372cc5916fd4781fec207ced7ddac2f08f) | 2,880,195,548.5 | 2,841,066,026 | -1.34% | -2.39% to -0.65% | +0.25% | | [Django](https://github.com/django/django/tree/b501b8493f31cf8cba31211d59b3b78642b8cb02) | 15,112,163,787 | 15,037,107,637 | -0.45% | -3.97% to +0.57% | +0.29% | | [SymPy](https://github.com/sympy/sympy/tree/85d3998a4517ffbc8a9709b8efa769bb417c174a) | 45,099,532,310.5 | 43,620,889,561 | -3.43% | -3.64% to -3.07% | -0.21% | | [OpenAI Python SDK](https://github.com/openai/openai-python/tree/f6e8480f9a5b2a4d02b17a1da54a1e57964bc312) | 16,984,636,041.5 | 16,620,622,667.5 | -2.11% | -2.34% to -1.98% | +1.20% | | [pytest](https://github.com/pytest-dev/pytest/tree/8b005ed7f972987033ec34dc1e68b0635fb8142b) | 9,368,713,672 | 8,797,702,205 | -6.02% | -6.41% to -5.99% | +0.52% | | [aiohttp](https://github.com/aio-libs/aiohttp/tree/fe099491d275ef2c21bf683e024e34d51ac05d43) | 4,190,028,893 | 4,119,314,906.5 | -1.69% | -1.78% to -1.43% | +0.08% | | [Ansible](https://github.com/ansible/ansible/tree/b4b2b1e82a535d27f902a71fc8396c3af138fca2) | 14,999,362,992.5 | 14,702,488,320.5 | -1.76% | -2.50% to -1.46% | +0.11% | | [botocore](https://github.com/boto/botocore/tree/e250abbe956da791c16086575e6ddb1f2da81a78) | 3,465,389,284 | 3,472,496,220.5 | -0.11% | -3.77% to +1.83% | -0.08% | | [Celery](https://github.com/celery/celery/tree/9c2f238334331db43f15956f131235fd3bb70fd1) | 5,127,744,625.5 | 5,043,931,163.5 | -1.63% | -1.90% to -1.18% | +0.23% | | [Dask](https://github.com/dask/dask/tree/249a51a1db4261791b93b4ce04e67341729939c3) | 34,770,603,802 | 33,970,353,416.5 | -2.39% | -2.48% to -2.15% | +0.77% | | [NetworkX](https://github.com/networkx/networkx/tree/6855cd586982cc2e4710b8eb87251ccf56674bf1) | 16,327,349,515 | 15,733,765,711.5 | -3.44% | -3.86% to -3.41% | +0.72% | | [Pydantic](https://github.com/pydantic/pydantic/tree/560f0f96a37d6913afdfddb2de22030ef08e68c3) | 10,434,492,896.5 | 7,471,098,959 | -28.44% | -28.59% to -28.30% | +1.06% | | [scikit-learn](https://github.com/scikit-learn/scikit-learn/tree/ee56c8b5d809dc7b970a140ed501269b63c69cf8) | 55,682,598,909.5 | 52,605,795,798 | -5.46% | -5.67% to -5.37% | +2.13% | | [Scrapy](https://github.com/scrapy/scrapy/tree/e63a574c767d7a9438c21db158f605f1c44c6e6a) | 9,610,778,990 | 9,492,443,887.5 | -1.24% | -1.26% to -1.20% | +0.67% | | [Transformers](https://github.com/huggingface/transformers/tree/3a134bc37f7c22c801b618e289b0255425f22647) | 102,409,356,294 | 100,208,355,859.5 | -2.04% | -2.33% to -1.98% | +1.08% | </details> <!-- ty-pinned-project-instructions:start --> Across 28 projects, the mean change in instructions was −2.50% and the median was −1.46%. We compared cold checks against [main at d83eb73b](https://github.com/astral-sh/ruff/commit/d83eb73b279c28ff94a6b8fbc30690f816221ba5). These tables measure [the PR revision before the rebase](https://github.com/astral-sh/ruff/commit/d603145ef0574465914e9f061c7180bb2d1c5391) applied to that commit. They predate #30480. Any figures above are from earlier runs. Output varied across runs on Django. The [eight-alternative overload benchmark](https://github.com/astral-sh/ruff/blob/b7ada1f7d7972c18563be77af8e4942e488589e6/crates/ruff_benchmark/benches/ty_constraint_set.rs#L441) flagged by CodSpeed also used more instructions. The increase was 17.66% on the original base and 17.62% on the base used for the project tables. We repeated this benchmark after #30480. Median instructions rose from 147,927,648 on [main](https://github.com/astral-sh/ruff/commit/96c09ab6e37ce7608ae420cb8de243c1ee9b7d98) to 173,874,462.5 on [this revision](https://github.com/astral-sh/ruff/commit/b7ada1f7d7972c18563be77af8e4942e488589e6) (+17.54%) in four paired Cachegrind runs. Both builds reported zero diagnostics. In a separate Callgrind profile, the [second pass over the arguments](https://github.com/astral-sh/ruff/blob/b7ada1f7d7972c18563be77af8e4942e488589e6/crates/ty_python_semantic/src/types/call/bind.rs#L7385) used 25.92 million instructions. The total rose by 25.90 million. In the single-worker Django checks in these tables, the PR changed `Divergent` to `Unknown` in two assignment errors and a note in `django/db/models/sql/query.py`. Both builds produced both variants with default parallelism. After the rebase we repeated the diagnostic checks against #30480. The 907-file run and a separate 29-file reproducer matched on both builds, including with the file order reversed. <details> <summary>Instruction counts by project on Linux</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. We marked a result inconclusive when the pairs disagreed in direction or the change was within the variation between runs of either build. | Project | [Base instructions](https://github.com/astral-sh/ruff/commit/d83eb73b279c28ff94a6b8fbc30690f816221ba5) | [PR instructions](https://github.com/astral-sh/ruff/commit/d603145ef0574465914e9f061c7180bb2d1c5391) | Change | Paired range | | --- | ---: | ---: | ---: | ---: | | [Black](https://github.com/psf/black/tree/55996d1fb8342725c8a19b6023cd9439c3c1d6f7) | 2,757,244,751 | 2,723,159,538 | -1.24% | -1.25% to -1.23% | | [discord.py](https://github.com/Rapptz/discord.py/tree/65232c38702be5844cf2ce865a4777eb1928b5d0) | 6,716,778,631 | 6,597,157,138 | -1.78% | -1.79% to -1.77% | | [Home Assistant](https://github.com/home-assistant/core/tree/c19ae285ede74cd64c3045a3a4aa43290c224f1b) | 137,625,332,518.5 | 135,618,822,551.5 | -1.46% | -1.46% to -1.46% | | [isort](https://github.com/pycqa/isort/tree/a514c71ed86c2216d0040c7d60e302ed76a4f7ab) | 1,511,901,166 | 1,490,137,332 | -1.44% | -1.46% to -1.43% | | [Jinja](https://github.com/pallets/jinja/tree/5ef70112a1ff19c05324ff889dd30405b1002044) | 1,634,155,350.5 | 1,613,949,015.5 | -1.24% | -1.26% to -1.23% | | [pandas](https://github.com/pandas-dev/pandas/tree/a0225b65720018f4654390bb16d254daa6ee5e54) | 23,942,348,667.5 | 23,461,116,838.5 | -2.01% | -2.01% to -2.01% | | [pandas-stubs](https://github.com/pandas-dev/pandas-stubs/tree/8dd6d91b1c05b26e30f7ddbe3dc3d4042a92bdd6) (inconclusive) | 2,652,446,682 | 2,652,533,646.5 | 0.00% | -0.03% to +0.05% | | [Prefect](https://github.com/PrefectHQ/prefect/tree/a9452ac21852755311913cb27887b083b5007c03) | 4,462,930,368 | 4,435,700,718 | -0.61% | -0.62% to -0.60% | | [PyTorch](https://github.com/pytorch/pytorch/tree/ab92945cea42e47ae398aead69af0872d340ea0f) | 81,832,013,096 | 80,091,523,956.5 | -2.13% | -2.13% to -2.13% | | [HTTPX](https://github.com/encode/httpx/tree/b5addb64f0161ff6bfe94c124ef76f6a1fba5254) | 1,420,924,076.5 | 1,401,965,263 | -1.34% | -1.34% to -1.33% | | [NumPy](https://github.com/numpy/numpy/tree/f9f1772ce010001208475f751d19253469788410) | 8,021,462,168.5 | 8,010,827,193.5 | -0.13% | -0.14% to -0.09% | | [FastAPI](https://github.com/fastapi/fastapi/tree/7a0a3f0b286c6c865cf4398ea0e957a87b8bffa4) | 2,635,429,107.5 | 2,623,242,150 | -0.46% | -0.48% to -0.45% | | [SQLAlchemy](https://github.com/sqlalchemy/sqlalchemy/tree/10f4d1e2d706a7722ca78e79ce05b00333a7aa5b) | 17,436,369,264.5 | 17,104,711,481 | -1.90% | -1.91% to -1.90% | | [Rich](https://github.com/Textualize/rich/tree/9d8f9a372cc5916fd4781fec207ced7ddac2f08f) | 2,380,509,531 | 2,348,256,478.5 | -1.36% | -1.38% to -1.34% | | [Django](https://github.com/django/django/tree/b501b8493f31cf8cba31211d59b3b78642b8cb02) | 11,779,031,574.5 | 11,659,480,633.5 | -1.01% | -1.02% to -1.01% | | [SymPy](https://github.com/sympy/sympy/tree/85d3998a4517ffbc8a9709b8efa769bb417c174a) | 37,749,282,500 | 36,578,881,897 | -3.10% | -3.11% to -3.10% | | [OpenAI Python SDK](https://github.com/openai/openai-python/tree/f6e8480f9a5b2a4d02b17a1da54a1e57964bc312) | 13,797,577,156.5 | 13,501,124,113 | -2.15% | -2.15% to -2.15% | | [pytest](https://github.com/pytest-dev/pytest/tree/8b005ed7f972987033ec34dc1e68b0635fb8142b) | 7,668,458,592 | 7,241,937,602 | -5.57% | -5.57% to -5.55% | | [aiohttp](https://github.com/aio-libs/aiohttp/tree/fe099491d275ef2c21bf683e024e34d51ac05d43) | 3,547,804,159.5 | 3,495,735,886.5 | -1.47% | -1.47% to -1.46% | | [Ansible](https://github.com/ansible/ansible/tree/b4b2b1e82a535d27f902a71fc8396c3af138fca2) | 12,325,858,647.5 | 12,140,307,805.5 | -1.51% | -1.51% to -1.50% | | [botocore](https://github.com/boto/botocore/tree/e250abbe956da791c16086575e6ddb1f2da81a78) | 2,612,822,795.5 | 2,599,997,107 | -0.49% | -0.49% to -0.49% | | [Celery](https://github.com/celery/celery/tree/9c2f238334331db43f15956f131235fd3bb70fd1) | 4,174,976,363.5 | 4,127,292,877.5 | -1.14% | -1.15% to -1.14% | | [Dask](https://github.com/dask/dask/tree/249a51a1db4261791b93b4ce04e67341729939c3) | 29,059,090,407 | 28,429,859,380 | -2.16% | -2.18% to -2.15% | | [NetworkX](https://github.com/networkx/networkx/tree/6855cd586982cc2e4710b8eb87251ccf56674bf1) | 13,344,587,573.5 | 12,922,794,185.5 | -3.16% | -3.17% to -3.15% | | [Pydantic](https://github.com/pydantic/pydantic/tree/560f0f96a37d6913afdfddb2de22030ef08e68c3) | 8,636,893,834 | 6,656,026,577.5 | -22.94% | -22.94% to -22.93% | | [scikit-learn](https://github.com/scikit-learn/scikit-learn/tree/ee56c8b5d809dc7b970a140ed501269b63c69cf8) | 46,537,676,726.5 | 44,153,994,664.5 | -5.12% | -5.12% to -5.12% | | [Scrapy](https://github.com/scrapy/scrapy/tree/e63a574c767d7a9438c21db158f605f1c44c6e6a) | 7,825,056,603 | 7,738,437,010 | -1.11% | -1.11% to -1.10% | | [Transformers](https://github.com/huggingface/transformers/tree/3a134bc37f7c22c801b618e289b0255425f22647) | 85,117,709,720.5 | 83,503,048,436.5 | -1.90% | -1.91% to -1.89% | </details> <details> <summary>Memory by project on Linux</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.80 (+0.73%) | 11/16 | +0.27 | −0.18 (−0.27%) | | [discord.py](https://github.com/Rapptz/discord.py/tree/65232c38702be5844cf2ce865a4777eb1928b5d0) | +0.84 (+0.44%) | 13/16 | +1.16 | +0.39 (+0.29%) | | [Home Assistant](https://github.com/home-assistant/core/tree/c19ae285ede74cd64c3045a3a4aa43290c224f1b) | +10.67 (+0.32%) | 16/16 | +13.49 | +2.46 (+0.08%) | | [isort](https://github.com/pycqa/isort/tree/a514c71ed86c2216d0040c7d60e302ed76a4f7ab) | +0.34 (+0.47%) | 9/16 | +0.37 | −0.32 (−0.90%) | | [Jinja](https://github.com/pallets/jinja/tree/5ef70112a1ff19c05324ff889dd30405b1002044) | +0.71 (+0.88%) | 13/16 | +0.32 | +0.08 (+0.22%) | | [pandas](https://github.com/pandas-dev/pandas/tree/a0225b65720018f4654390bb16d254daa6ee5e54) | +7.12 (+1.35%) | 14/16 | +5.14 | +3.80 (+0.85%) | | [pandas-stubs](https://github.com/pandas-dev/pandas-stubs/tree/8dd6d91b1c05b26e30f7ddbe3dc3d4042a92bdd6) | −0.40 (−0.29%) | 7/16 | +0.15 | 0.00 (+0.01%) | | [Prefect](https://github.com/PrefectHQ/prefect/tree/a9452ac21852755311913cb27887b083b5007c03) | +0.58 (+0.29%) | 13/16 | +0.47 | +0.39 (+0.27%) | | [PyTorch](https://github.com/pytorch/pytorch/tree/ab92945cea42e47ae398aead69af0872d340ea0f) | +6.61 (+0.45%) | 12/16 | +5.48 | −1.99 (−0.15%) | | [HTTPX](https://github.com/encode/httpx/tree/b5addb64f0161ff6bfe94c124ef76f6a1fba5254) | +0.16 (+0.18%) | 9/16 | +0.24 | +0.02 (+0.05%) | | [NumPy](https://github.com/numpy/numpy/tree/f9f1772ce010001208475f751d19253469788410) | −0.25 (−0.11%) | 8/16 | +0.55 | +0.11 (+0.07%) | | [FastAPI](https://github.com/fastapi/fastapi/tree/7a0a3f0b286c6c865cf4398ea0e957a87b8bffa4) | +0.77 (+0.63%) | 9/16 | +0.38 | +0.11 (+0.15%) | | [SQLAlchemy](https://github.com/sqlalchemy/sqlalchemy/tree/10f4d1e2d706a7722ca78e79ce05b00333a7aa5b) | +3.68 (+1.02%) | 14/16 | +3.70 | +2.47 (+0.84%) | | [Rich](https://github.com/Textualize/rich/tree/9d8f9a372cc5916fd4781fec207ced7ddac2f08f) | −0.51 (−0.49%) | 6/16 | +0.13 | +0.07 (+0.12%) | | [Django](https://github.com/django/django/tree/b501b8493f31cf8cba31211d59b3b78642b8cb02) | +0.40 (+0.14%) | 12/16 | +0.91 | +0.14 (+0.06%) | | [SymPy](https://github.com/sympy/sympy/tree/85d3998a4517ffbc8a9709b8efa769bb417c174a) | +3.49 (+0.54%) | 14/16 | +4.01 | −0.43 (−0.08%) | | [OpenAI Python SDK](https://github.com/openai/openai-python/tree/f6e8480f9a5b2a4d02b17a1da54a1e57964bc312) | +1.07 (+0.35%) | 13/16 | +1.69 | +0.99 (+0.42%) | | [pytest](https://github.com/pytest-dev/pytest/tree/8b005ed7f972987033ec34dc1e68b0635fb8142b) | +1.62 (+0.74%) | 14/16 | +1.60 | +0.70 (+0.44%) | | [aiohttp](https://github.com/aio-libs/aiohttp/tree/fe099491d275ef2c21bf683e024e34d51ac05d43) | +0.65 (+0.56%) | 13/16 | +0.26 | −0.13 (−0.18%) | | [Ansible](https://github.com/ansible/ansible/tree/b4b2b1e82a535d27f902a71fc8396c3af138fca2) | +0.06 (+0.02%) | 8/16 | +1.15 | +0.08 (+0.04%) | | [botocore](https://github.com/boto/botocore/tree/e250abbe956da791c16086575e6ddb1f2da81a78) | −0.29 (−0.27%) | 4/16 | +0.12 | +0.04 (+0.06%) | | [Celery](https://github.com/celery/celery/tree/9c2f238334331db43f15956f131235fd3bb70fd1) | +0.73 (+0.52%) | 14/16 | +0.38 | +0.14 (+0.15%) | | [Dask](https://github.com/dask/dask/tree/249a51a1db4261791b93b4ce04e67341729939c3) | +4.54 (+0.79%) | 16/16 | +4.73 | +2.93 (+0.61%) | | [NetworkX](https://github.com/networkx/networkx/tree/6855cd586982cc2e4710b8eb87251ccf56674bf1) | +0.90 (+0.33%) | 14/16 | +1.46 | +0.23 (+0.11%) | | [Pydantic](https://github.com/pydantic/pydantic/tree/560f0f96a37d6913afdfddb2de22030ef08e68c3) | +1.72 (+1.36%) | 14/16 | +1.73 | +0.19 (+0.25%) | | [scikit-learn](https://github.com/scikit-learn/scikit-learn/tree/ee56c8b5d809dc7b970a140ed501269b63c69cf8) | +10.48 (+1.77%) | 16/16 | +11.25 | +7.31 (+1.36%) | | [Scrapy](https://github.com/scrapy/scrapy/tree/e63a574c767d7a9438c21db158f605f1c44c6e6a) | −0.13 (−0.06%) | 8/16 | +0.39 | +0.30 (+0.19%) | | [Transformers](https://github.com/huggingface/transformers/tree/3a134bc37f7c22c801b618e289b0255425f22647) | +5.96 (+0.40%) | 14/16 | +4.87 | +0.80 (+0.06%) | </details> <!-- ty-pinned-project-instructions:end -->
Author
Parents
Loading