[ty] Share Pydantic validator checks across constructor fields (#30670)
## Summary
Dify’s `DifyConfig` inherits over 800 settings. For each constructor
parameter, ty scans the model and its bases for a `before` or `plain`
field validator. These validators can accept input of any type, so ty
widens the parameter to `Any`.
We now scan once for all constructor fields. We respect method overrides
and reachability, and stop at a wildcard or when every field has a
validator. The cache key includes the model and its constructor field
names. This retains a list of names, but lets specializations share a
scan and avoids copying inherited targets that don’t apply to the
subclass.
In four synthetic editor checks with many inherited validator targets,
methods, or constructor specializations, median peak RSS rose by
1.01–1.16 MiB.
A cold check of [Dify’s
`api/configs`](https://github.com/langgenius/dify/tree/63069f5f47e4a268bc7c015ae1a17c364c1ad3af/api/configs)
used 31.03–31.08% fewer instructions in two opposite-order Linux
Cachegrind pairs (2.84 billion → 1.96 billion). The unchanged binary
varied by 0.08%. Diagnostics, exit status, and the inferred constructor
signature matched. Median native peak RSS was 111.2 MiB before and 112.3
MiB after. With #30680 applied as well, a separate pair in each order
used 43.07–43.15% fewer instructions than the same base on this
workload.
## Pinned projects
The reduction on Dify doesn’t generalize to this suite. Every paired
mean was within 0.33% of the base, including Prefect (−0.33%) and
Pydantic (+0.16%).
| Project | [Base
instructions](https://github.com/astral-sh/ruff/commit/cd2d973241d7f5bcba588e94cc686d9e356a4621)
| [PR
instructions](https://github.com/astral-sh/ruff/commit/158ff0c21328a4ff7fd815bdf45d81f7007c4143)
| Change | Paired range |
| --- | ---: | ---: | ---: | ---: |
|
[Black](https://github.com/psf/black/tree/55996d1fb8342725c8a19b6023cd9439c3c1d6f7)
(inconclusive) | 3,163,410,394.5 | 3,162,908,281 | -0.02% | -0.03% to
0.00% |
|
[discord.py](https://github.com/Rapptz/discord.py/tree/65232c38702be5844cf2ce865a4777eb1928b5d0)
(inconclusive) | 8,153,637,769.5 | 8,150,873,210.5 | -0.03% | -0.07% to
0.00% |
| [Home
Assistant](https://github.com/home-assistant/core/tree/c19ae285ede74cd64c3045a3a4aa43290c224f1b)
(inconclusive) | 158,267,483,588.5 | 158,270,244,925 | 0.00% | -0.01% to
+0.01% |
|
[isort](https://github.com/pycqa/isort/tree/a514c71ed86c2216d0040c7d60e302ed76a4f7ab)
| 1,851,299,588 | 1,845,852,493 | -0.29% | -0.42% to -0.17% |
|
[Jinja](https://github.com/pallets/jinja/tree/5ef70112a1ff19c05324ff889dd30405b1002044)
(inconclusive) | 1,937,298,043.5 | 1,940,722,908.5 | +0.18% | -0.02% to
+0.37% |
|
[pandas](https://github.com/pandas-dev/pandas/tree/a0225b65720018f4654390bb16d254daa6ee5e54)
(inconclusive) | 28,408,140,816.5 | 28,420,938,996 | +0.05% | +0.04% to
+0.05% |
|
[pandas-stubs](https://github.com/pandas-dev/pandas-stubs/tree/8dd6d91b1c05b26e30f7ddbe3dc3d4042a92bdd6)
(inconclusive) | 2,956,655,073 | 2,956,160,547 | -0.02% | -0.24% to
+0.21% |
|
[Prefect](https://github.com/PrefectHQ/prefect/tree/a9452ac21852755311913cb27887b083b5007c03)
| 4,937,384,926.5 | 4,920,849,411 | -0.33% | -0.35% to -0.32% |
|
[PyTorch](https://github.com/pytorch/pytorch/tree/ab92945cea42e47ae398aead69af0872d340ea0f)
| 95,773,760,588 | 95,672,599,293.5 | -0.11% | -0.13% to -0.08% |
|
[HTTPX](https://github.com/encode/httpx/tree/b5addb64f0161ff6bfe94c124ef76f6a1fba5254)
(inconclusive) | 1,673,700,171.5 | 1,674,676,262.5 | +0.06% | -0.18% to
+0.30% |
|
[NumPy](https://github.com/numpy/numpy/tree/f9f1772ce010001208475f751d19253469788410)
(inconclusive) | 8,485,546,358 | 8,470,076,576 | -0.18% | -0.62% to
+0.26% |
|
[FastAPI](https://github.com/fastapi/fastapi/tree/7a0a3f0b286c6c865cf4398ea0e957a87b8bffa4)
(inconclusive) | 2,985,378,867 | 2,985,355,163.5 | 0.00% | -0.02% to
+0.02% |
|
[SQLAlchemy](https://github.com/sqlalchemy/sqlalchemy/tree/10f4d1e2d706a7722ca78e79ce05b00333a7aa5b)
(inconclusive) | 21,273,577,711 | 21,274,619,212.5 | +0.01% | -0.22% to
+0.23% |
|
[Rich](https://github.com/Textualize/rich/tree/9d8f9a372cc5916fd4781fec207ced7ddac2f08f)
(inconclusive) | 2,719,094,592 | 2,724,299,150 | +0.19% | +0.01% to
+0.37% |
|
[Django](https://github.com/django/django/tree/b501b8493f31cf8cba31211d59b3b78642b8cb02)
(inconclusive) | 13,800,620,312.5 | 13,788,407,534.5 | -0.09% | -0.27%
to +0.10% |
|
[SymPy](https://github.com/sympy/sympy/tree/85d3998a4517ffbc8a9709b8efa769bb417c174a)
(inconclusive) | 45,801,184,207.5 | 45,801,736,301.5 | 0.00% | -0.02% to
+0.02% |
| [OpenAI Python
SDK](https://github.com/openai/openai-python/tree/f6e8480f9a5b2a4d02b17a1da54a1e57964bc312)
| 15,877,343,707.5 | 15,863,690,781.5 | -0.09% | -0.09% to -0.08% |
|
[pytest](https://github.com/pytest-dev/pytest/tree/8b005ed7f972987033ec34dc1e68b0635fb8142b)
| 8,767,497,692.5 | 8,778,357,682 | +0.12% | +0.09% to +0.16% |
|
[aiohttp](https://github.com/aio-libs/aiohttp/tree/fe099491d275ef2c21bf683e024e34d51ac05d43)
(inconclusive) | 4,152,005,209.5 | 4,155,392,637 | +0.08% | -0.03% to
+0.19% |
|
[Ansible](https://github.com/ansible/ansible/tree/b4b2b1e82a535d27f902a71fc8396c3af138fca2)
(inconclusive) | 14,330,920,210.5 | 14,327,152,489.5 | -0.03% | -0.16%
to +0.10% |
|
[botocore](https://github.com/boto/botocore/tree/e250abbe956da791c16086575e6ddb1f2da81a78)
(inconclusive) | 3,006,170,686 | 3,007,746,827.5 | +0.05% | +0.02% to
+0.09% |
|
[Celery](https://github.com/celery/celery/tree/9c2f238334331db43f15956f131235fd3bb70fd1)
(inconclusive) | 4,777,581,732.5 | 4,772,847,489.5 | -0.10% | -0.18% to
-0.02% |
|
[Dask](https://github.com/dask/dask/tree/249a51a1db4261791b93b4ce04e67341729939c3)
| 33,766,479,163 | 33,791,203,537.5 | +0.07% | +0.04% to +0.10% |
|
[NetworkX](https://github.com/networkx/networkx/tree/6855cd586982cc2e4710b8eb87251ccf56674bf1)
| 15,575,208,704 | 15,588,468,270.5 | +0.09% | +0.08% to +0.09% |
|
[Pydantic](https://github.com/pydantic/pydantic/tree/560f0f96a37d6913afdfddb2de22030ef08e68c3)
| 12,985,650,591 | 13,006,092,139.5 | +0.16% | +0.15% to +0.16% |
|
[scikit-learn](https://github.com/scikit-learn/scikit-learn/tree/ee56c8b5d809dc7b970a140ed501269b63c69cf8)
| 61,277,824,347.5 | 61,377,835,821.5 | +0.16% | +0.15% to +0.18% |
|
[Scrapy](https://github.com/scrapy/scrapy/tree/e63a574c767d7a9438c21db158f605f1c44c6e6a)
(inconclusive) | 8,944,062,847 | 8,935,257,668.5 | -0.10% | -0.14% to
-0.05% |
|
[Transformers](https://github.com/huggingface/transformers/tree/3a134bc37f7c22c801b618e289b0255425f22647)
(inconclusive) | 99,857,620,131.5 | 99,860,335,337 | 0.00% | -0.03% to
+0.03% |