[ty] Avoid rebuilding ordinary function signatures to check for type guards (#30671)
## Summary
For a function like `def read(n: int) -> bytes: ...`, ty builds a
signature to validate the definition, then builds it again to find out
whether the return type is `TypeIs` or `TypeGuard`.
We now reuse the checked return type to rule out a guard when the
definition is unchanged. If a decorator replaces the function, we check
the replacement’s signature. We also check the full signature if the
return type is a guard or an explicit annotation resolves to `Unknown`.
When signatures refer back to one another, ty can cache `Unknown` even
for a `TypeIs` or `TypeGuard` annotation.
All measurements below are from
[d26ad62a](https://github.com/astral-sh/ruff/commit/d26ad62a3f790ad15c613066fe34001c284f1825),
before we added the fallback for `Unknown`.
A cold check of all 58 PySide6 Essentials 6.10.0 stubs used 3.71–4.32%
fewer instructions in two runs in opposite orders, versus 0.85%
variation when repeating the baseline. Median native peak RSS over three
runs fell from 327.3 to 324.9 MiB. Checking just `QtOpenGL.pyi` used
5.19–5.20% fewer instructions, versus 0.007% variation in the control.
## Pinned projects
Cachegrind counted fewer instructions in both orders on every project.
On Prefect and scikit-learn, the change was within the variation of the
controls. Diagnostics and exit codes matched in all checks. The counts
are whole-process means from two cold checks per revision with default
concurrency.
| Project | [Base
instructions](https://github.com/astral-sh/ruff/commit/cd2d973241d7f5bcba588e94cc686d9e356a4621)
| [PR
instructions](https://github.com/astral-sh/ruff/commit/d26ad62a3f790ad15c613066fe34001c284f1825)
| Change | Paired range |
| --- | ---: | ---: | ---: | ---: |
|
[Black](https://github.com/psf/black/tree/55996d1fb8342725c8a19b6023cd9439c3c1d6f7)
| 3,161,489,101 | 3,153,959,051.5 | -0.24% | -0.26% to -0.21% |
|
[discord.py](https://github.com/Rapptz/discord.py/tree/65232c38702be5844cf2ce865a4777eb1928b5d0)
| 8,157,651,396 | 8,098,186,313.5 | -0.73% | -0.90% to -0.55% |
| [Home
Assistant](https://github.com/home-assistant/core/tree/c19ae285ede74cd64c3045a3a4aa43290c224f1b)
| 158,315,346,465.5 | 157,724,138,950 | -0.37% | -0.38% to -0.37% |
|
[isort](https://github.com/pycqa/isort/tree/a514c71ed86c2216d0040c7d60e302ed76a4f7ab)
| 1,853,240,536.5 | 1,849,120,811 | -0.22% | -0.35% to -0.09% |
|
[Jinja](https://github.com/pallets/jinja/tree/5ef70112a1ff19c05324ff889dd30405b1002044)
| 1,934,229,356 | 1,925,988,814.5 | -0.43% | -0.50% to -0.35% |
|
[pandas](https://github.com/pandas-dev/pandas/tree/a0225b65720018f4654390bb16d254daa6ee5e54)
| 28,430,037,053 | 28,329,180,551 | -0.35% | -0.41% to -0.30% |
|
[pandas-stubs](https://github.com/pandas-dev/pandas-stubs/tree/8dd6d91b1c05b26e30f7ddbe3dc3d4042a92bdd6)
| 2,951,539,031.5 | 2,922,210,373.5 | -0.99% | -1.03% to -0.95% |
|
[Prefect](https://github.com/PrefectHQ/prefect/tree/a9452ac21852755311913cb27887b083b5007c03)
(inconclusive) | 4,933,499,049 | 4,929,846,234.5 | -0.07% | -0.08% to
-0.06% |
|
[PyTorch](https://github.com/pytorch/pytorch/tree/ab92945cea42e47ae398aead69af0872d340ea0f)
| 95,747,917,640.5 | 95,386,503,464 | -0.38% | -0.45% to -0.31% |
|
[HTTPX](https://github.com/encode/httpx/tree/b5addb64f0161ff6bfe94c124ef76f6a1fba5254)
| 1,671,234,716 | 1,667,004,123 | -0.25% | -0.26% to -0.24% |
|
[NumPy](https://github.com/numpy/numpy/tree/f9f1772ce010001208475f751d19253469788410)
| 8,454,323,216 | 8,409,629,187 | -0.53% | -0.68% to -0.37% |
|
[FastAPI](https://github.com/fastapi/fastapi/tree/7a0a3f0b286c6c865cf4398ea0e957a87b8bffa4)
| 2,982,104,774 | 2,975,331,829 | -0.23% | -0.33% to -0.12% |
|
[SQLAlchemy](https://github.com/sqlalchemy/sqlalchemy/tree/10f4d1e2d706a7722ca78e79ce05b00333a7aa5b)
| 21,262,515,922.5 | 21,134,619,289.5 | -0.60% | -0.66% to -0.54% |
|
[Rich](https://github.com/Textualize/rich/tree/9d8f9a372cc5916fd4781fec207ced7ddac2f08f)
| 2,713,211,342.5 | 2,705,363,460 | -0.29% | -0.37% to -0.21% |
|
[Django](https://github.com/django/django/tree/b501b8493f31cf8cba31211d59b3b78642b8cb02)
| 13,774,431,947.5 | 13,702,305,798 | -0.52% | -0.66% to -0.39% |
|
[SymPy](https://github.com/sympy/sympy/tree/85d3998a4517ffbc8a9709b8efa769bb417c174a)
| 45,803,382,423 | 45,595,950,296 | -0.45% | -0.51% to -0.40% |
| [OpenAI Python
SDK](https://github.com/openai/openai-python/tree/f6e8480f9a5b2a4d02b17a1da54a1e57964bc312)
| 15,871,121,289.5 | 15,826,476,155.5 | -0.28% | -0.33% to -0.24% |
|
[pytest](https://github.com/pytest-dev/pytest/tree/8b005ed7f972987033ec34dc1e68b0635fb8142b)
| 8,766,324,981 | 8,735,258,910 | -0.35% | -0.44% to -0.27% |
|
[aiohttp](https://github.com/aio-libs/aiohttp/tree/fe099491d275ef2c21bf683e024e34d51ac05d43)
| 4,149,292,608 | 4,137,698,021 | -0.28% | -0.35% to -0.21% |
|
[Ansible](https://github.com/ansible/ansible/tree/b4b2b1e82a535d27f902a71fc8396c3af138fca2)
| 14,328,463,468 | 14,292,264,126 | -0.25% | -0.27% to -0.24% |
|
[botocore](https://github.com/boto/botocore/tree/e250abbe956da791c16086575e6ddb1f2da81a78)
| 3,003,917,541.5 | 2,984,499,209 | -0.65% | -0.69% to -0.60% |
|
[Celery](https://github.com/celery/celery/tree/9c2f238334331db43f15956f131235fd3bb70fd1)
| 4,775,374,563 | 4,747,897,133 | -0.58% | -0.59% to -0.56% |
|
[Dask](https://github.com/dask/dask/tree/249a51a1db4261791b93b4ce04e67341729939c3)
| 33,744,213,170 | 33,708,756,563 | -0.11% | -0.14% to -0.07% |
|
[NetworkX](https://github.com/networkx/networkx/tree/6855cd586982cc2e4710b8eb87251ccf56674bf1)
| 15,575,270,130.5 | 15,531,758,619.5 | -0.28% | -0.34% to -0.21% |
|
[Pydantic](https://github.com/pydantic/pydantic/tree/560f0f96a37d6913afdfddb2de22030ef08e68c3)
| 12,990,088,643 | 12,981,483,286 | -0.07% | -0.07% to -0.06% |
|
[scikit-learn](https://github.com/scikit-learn/scikit-learn/tree/ee56c8b5d809dc7b970a140ed501269b63c69cf8)
(inconclusive) | 61,287,874,022.5 | 61,225,020,827.5 | -0.10% | -0.18%
to -0.02% |
|
[Scrapy](https://github.com/scrapy/scrapy/tree/e63a574c767d7a9438c21db158f605f1c44c6e6a)
| 8,924,967,195 | 8,864,980,532.5 | -0.67% | -0.70% to -0.64% |
|
[Transformers](https://github.com/huggingface/transformers/tree/3a134bc37f7c22c801b618e289b0255425f22647)
| 99,780,402,940.5 | 99,413,156,088 | -0.37% | -0.39% to -0.35% |