[ty] Avoid interning the parent module for `from` imports (#31071)
## Summary
To check `from pkg import child`, we resolve `pkg`, then intern a module
literal for it just to look up `child`. For packages, the importing file
is part of the key, so we can end up storing a separate literal for
every file that imports from the same package.
We need to know which file we’re checking because an explicit import of
a submodule can shadow a package attribute. For example, if
`pkg.child.nested` exists, ty resolves `child` differently in these two
files.
```py
# pkg/__init__.py
child = 1
# a.py
from pkg import child # Literal[1]
# b.py
import pkg.child.nested
from pkg import child # the pkg.child module
```
We now look up `child` directly on the resolved package. We only intern
a literal for `pkg` if we need it for a diagnostic.
We can also skip the shadowing check in `a.py`. The import index records
every parent of a dotted import, so if `pkg` is absent, we know the file
hasn’t explicitly imported `pkg.child`. If the package doesn’t define
`child`, we still try to resolve it as a submodule.
<!-- ty-pinned-project-instructions:start -->
<details>
<summary>Instruction counts by project</summary>
| Project | [Base
instructions](https://github.com/astral-sh/ruff/commit/d67aa96e6aa18733304d95ce085add18339d4aef)
| [PR
instructions](https://github.com/astral-sh/ruff/commit/a28743a035791634f93540168da64527be47889c)
| Change | Paired range |
| --- | ---: | ---: | ---: | ---: |
|
[Black](https://github.com/psf/black/tree/55996d1fb8342725c8a19b6023cd9439c3c1d6f7)
| 2,373,788,114.5 | 2,372,877,407.5 | -0.04% | -0.04% to -0.04% |
|
[discord.py](https://github.com/Rapptz/discord.py/tree/65232c38702be5844cf2ce865a4777eb1928b5d0)
| 5,715,076,465 | 5,712,115,573 | -0.05% | -0.06% to -0.05% |
| [Home
Assistant](https://github.com/home-assistant/core/tree/c19ae285ede74cd64c3045a3a4aa43290c224f1b)
| 115,432,917,616.5 | 115,301,819,072 | -0.11% | -0.11% to -0.11% |
|
[isort](https://github.com/pycqa/isort/tree/a514c71ed86c2216d0040c7d60e302ed76a4f7ab)
| 1,293,127,514.5 | 1,292,726,416.5 | -0.03% | -0.04% to -0.03% |
|
[Jinja](https://github.com/pallets/jinja/tree/5ef70112a1ff19c05324ff889dd30405b1002044)
(inconclusive) | 1,341,411,095.5 | 1,341,034,149 | -0.04% | -0.06% to
-0.02% |
|
[pandas](https://github.com/pandas-dev/pandas/tree/a0225b65720018f4654390bb16d254daa6ee5e54)
| 20,452,996,192 | 20,443,932,754.5 | -0.04% | -0.05% to -0.04% |
|
[pandas-stubs](https://github.com/pandas-dev/pandas-stubs/tree/8dd6d91b1c05b26e30f7ddbe3dc3d4042a92bdd6)
| 2,285,421,656.5 | 2,283,513,448 | -0.08% | -0.10% to -0.06% |
|
[Prefect](https://github.com/PrefectHQ/prefect/tree/a9452ac21852755311913cb27887b083b5007c03)
| 3,839,034,208 | 3,837,795,964.5 | -0.03% | -0.04% to -0.03% |
|
[PyTorch](https://github.com/pytorch/pytorch/tree/ab92945cea42e47ae398aead69af0872d340ea0f)
| 68,785,798,746.5 | 68,750,795,568.5 | -0.05% | -0.05% to -0.05% |
|
[HTTPX](https://github.com/encode/httpx/tree/b5addb64f0161ff6bfe94c124ef76f6a1fba5254)
| 1,204,189,167.5 | 1,203,665,933 | -0.04% | -0.04% to -0.04% |
|
[NumPy](https://github.com/numpy/numpy/tree/f9f1772ce010001208475f751d19253469788410)
| 7,158,802,027 | 7,157,258,099.5 | -0.02% | -0.03% to -0.01% |
|
[FastAPI](https://github.com/fastapi/fastapi/tree/7a0a3f0b286c6c865cf4398ea0e957a87b8bffa4)
(inconclusive) | 2,253,553,174 | 2,253,149,353.5 | -0.02% | -0.05% to
0.00% |
|
[SQLAlchemy](https://github.com/sqlalchemy/sqlalchemy/tree/10f4d1e2d706a7722ca78e79ce05b00333a7aa5b)
| 14,931,404,439.5 | 14,926,705,653 | -0.03% | -0.03% to -0.03% |
|
[Rich](https://github.com/Textualize/rich/tree/9d8f9a372cc5916fd4781fec207ced7ddac2f08f)
| 1,990,208,502 | 1,989,148,867.5 | -0.05% | -0.08% to -0.03% |
|
[Django](https://github.com/django/django/tree/b501b8493f31cf8cba31211d59b3b78642b8cb02)
| 9,880,843,157.5 | 9,873,069,909 | -0.08% | -0.08% to -0.08% |
|
[SymPy](https://github.com/sympy/sympy/tree/85d3998a4517ffbc8a9709b8efa769bb417c174a)
| 31,537,547,697 | 31,517,332,034.5 | -0.06% | -0.07% to -0.05% |
| [OpenAI Python
SDK](https://github.com/openai/openai-python/tree/f6e8480f9a5b2a4d02b17a1da54a1e57964bc312)
| 10,976,838,439 | 10,965,739,501 | -0.10% | -0.11% to -0.09% |
|
[pytest](https://github.com/pytest-dev/pytest/tree/8b005ed7f972987033ec34dc1e68b0635fb8142b)
| 6,156,127,823.5 | 6,152,571,928.5 | -0.06% | -0.07% to -0.06% |
|
[aiohttp](https://github.com/aio-libs/aiohttp/tree/fe099491d275ef2c21bf683e024e34d51ac05d43)
| 3,044,133,460.5 | 3,042,929,521 | -0.04% | -0.04% to -0.04% |
|
[Ansible](https://github.com/ansible/ansible/tree/b4b2b1e82a535d27f902a71fc8396c3af138fca2)
| 10,422,708,203 | 10,417,011,459 | -0.05% | -0.06% to -0.05% |
|
[botocore](https://github.com/boto/botocore/tree/e250abbe956da791c16086575e6ddb1f2da81a78)
| 2,193,929,150 | 2,192,667,948 | -0.06% | -0.06% to -0.05% |
|
[Celery](https://github.com/celery/celery/tree/9c2f238334331db43f15956f131235fd3bb70fd1)
| 3,501,104,393 | 3,499,253,353.5 | -0.05% | -0.06% to -0.05% |
|
[Dask](https://github.com/dask/dask/tree/249a51a1db4261791b93b4ce04e67341729939c3)
| 24,300,364,311.5 | 24,292,464,234.5 | -0.03% | -0.03% to -0.03% |
|
[NetworkX](https://github.com/networkx/networkx/tree/6855cd586982cc2e4710b8eb87251ccf56674bf1)
| 11,099,337,465 | 11,091,530,212 | -0.07% | -0.07% to -0.07% |
|
[Pydantic](https://github.com/pydantic/pydantic/tree/560f0f96a37d6913afdfddb2de22030ef08e68c3)
| 6,189,641,409.5 | 6,185,572,437.5 | -0.07% | -0.07% to -0.06% |
|
[scikit-learn](https://github.com/scikit-learn/scikit-learn/tree/ee56c8b5d809dc7b970a140ed501269b63c69cf8)
| 37,349,237,087.5 | 37,335,352,856.5 | -0.04% | -0.04% to -0.03% |
|
[Scrapy](https://github.com/scrapy/scrapy/tree/e63a574c767d7a9438c21db158f605f1c44c6e6a)
| 6,620,392,423 | 6,614,797,671 | -0.08% | -0.09% to -0.08% |
|
[Transformers](https://github.com/huggingface/transformers/tree/3a134bc37f7c22c801b618e289b0255425f22647)
| 71,997,730,215.5 | 71,951,250,361 | -0.06% | -0.07% to -0.06% |
</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.04 (+0.03%) | 8/16 | +0.17 | −0.02 (−0.02%) |
|
[discord.py](https://github.com/Rapptz/discord.py/tree/65232c38702be5844cf2ce865a4777eb1928b5d0)
| +0.17 (+0.09%) | 9/16 | +0.10 | −0.01 (−0.01%) |
| [Home
Assistant](https://github.com/home-assistant/core/tree/c19ae285ede74cd64c3045a3a4aa43290c224f1b)
| −2.22 (−0.07%) | 4/16 | −2.28 | −3.08 (−0.10%) |
|
[isort](https://github.com/pycqa/isort/tree/a514c71ed86c2216d0040c7d60e302ed76a4f7ab)
| −0.93 (−1.30%) | 7/16 | −0.24 | −0.01 (−0.02%) |
|
[Jinja](https://github.com/pallets/jinja/tree/5ef70112a1ff19c05324ff889dd30405b1002044)
| +0.47 (+0.59%) | 13/16 | +0.16 | −0.01 (−0.02%) |
|
[pandas](https://github.com/pandas-dev/pandas/tree/a0225b65720018f4654390bb16d254daa6ee5e54)
| +1.07 (+0.21%) | 9/16 | −0.19 | −0.16 (−0.03%) |
|
[pandas-stubs](https://github.com/pandas-dev/pandas-stubs/tree/8dd6d91b1c05b26e30f7ddbe3dc3d4042a92bdd6)
| −0.23 (−0.17%) | 7/16 | +0.25 | −0.04 (−0.04%) |
|
[Prefect](https://github.com/PrefectHQ/prefect/tree/a9452ac21852755311913cb27887b083b5007c03)
| +0.07 (+0.03%) | 8/16 | +0.28 | −0.06 (−0.04%) |
|
[PyTorch](https://github.com/pytorch/pytorch/tree/ab92945cea42e47ae398aead69af0872d340ea0f)
| +0.25 (+0.02%) | 8/16 | −0.54 | −0.78 (−0.06%) |
|
[HTTPX](https://github.com/encode/httpx/tree/b5addb64f0161ff6bfe94c124ef76f6a1fba5254)
| +0.07 (+0.08%) | 9/16 | +0.04 | −0.02 (−0.03%) |
|
[NumPy](https://github.com/numpy/numpy/tree/f9f1772ce010001208475f751d19253469788410)
| −4.75 (−2.15%) | 5/16 | +0.05 | −0.05 (−0.03%) |
|
[FastAPI](https://github.com/fastapi/fastapi/tree/7a0a3f0b286c6c865cf4398ea0e957a87b8bffa4)
| +0.15 (+0.13%) | 10/16 | −0.01 | −0.02 (−0.03%) |
|
[SQLAlchemy](https://github.com/sqlalchemy/sqlalchemy/tree/10f4d1e2d706a7722ca78e79ce05b00333a7aa5b)
| +1.79 (+0.51%) | 10/16 | −0.14 | −0.09 (−0.03%) |
|
[Rich](https://github.com/Textualize/rich/tree/9d8f9a372cc5916fd4781fec207ced7ddac2f08f)
| −0.32 (−0.31%) | 6/16 | −0.08 | −0.03 (−0.04%) |
|
[Django](https://github.com/django/django/tree/b501b8493f31cf8cba31211d59b3b78642b8cb02)
| −0.45 (−0.17%) | 7/16 | −0.26 | −0.14 (−0.06%) |
|
[SymPy](https://github.com/sympy/sympy/tree/85d3998a4517ffbc8a9709b8efa769bb417c174a)
| −0.14 (−0.02%) | 7/16 | −0.06 | −0.23 (−0.04%) |
| [OpenAI Python
SDK](https://github.com/openai/openai-python/tree/f6e8480f9a5b2a4d02b17a1da54a1e57964bc312)
| −0.47 (−0.17%) | 4/16 | +0.10 | −0.27 (−0.11%) |
|
[pytest](https://github.com/pytest-dev/pytest/tree/8b005ed7f972987033ec34dc1e68b0635fb8142b)
| −0.07 (−0.03%) | 8/16 | −0.24 | −0.04 (−0.02%) |
|
[aiohttp](https://github.com/aio-libs/aiohttp/tree/fe099491d275ef2c21bf683e024e34d51ac05d43)
| +0.27 (+0.24%) | 10/16 | +0.27 | −0.02 (−0.03%) |
|
[Ansible](https://github.com/ansible/ansible/tree/b4b2b1e82a535d27f902a71fc8396c3af138fca2)
| +0.80 (+0.31%) | 11/16 | +0.12 | −0.10 (−0.05%) |
|
[botocore](https://github.com/boto/botocore/tree/e250abbe956da791c16086575e6ddb1f2da81a78)
| −0.23 (−0.22%) | 7/16 | −0.09 | −0.02 (−0.02%) |
|
[Celery](https://github.com/celery/celery/tree/9c2f238334331db43f15956f131235fd3bb70fd1)
| 0.00 (0.00%) | 8/16 | −0.12 | −0.04 (−0.04%) |
|
[Dask](https://github.com/dask/dask/tree/249a51a1db4261791b93b4ce04e67341729939c3)
| +1.34 (+0.24%) | 12/16 | −0.20 | −0.10 (−0.02%) |
|
[NetworkX](https://github.com/networkx/networkx/tree/6855cd586982cc2e4710b8eb87251ccf56674bf1)
| −0.13 (−0.05%) | 8/16 | −0.10 | −0.07 (−0.03%) |
|
[Pydantic](https://github.com/pydantic/pydantic/tree/560f0f96a37d6913afdfddb2de22030ef08e68c3)
| −1.89 (−1.52%) | 5/16 | −0.04 | 0.00 (0.00%) |
|
[scikit-learn](https://github.com/scikit-learn/scikit-learn/tree/ee56c8b5d809dc7b970a140ed501269b63c69cf8)
| +0.49 (+0.09%) | 10/16 | −0.17 | −0.20 (−0.04%) |
|
[Scrapy](https://github.com/scrapy/scrapy/tree/e63a574c767d7a9438c21db158f605f1c44c6e6a)
| −0.06 (−0.03%) | 7/16 | −0.18 | −0.11 (−0.06%) |
|
[Transformers](https://github.com/huggingface/transformers/tree/3a134bc37f7c22c801b618e289b0255425f22647)
| +0.80 (+0.06%) | 8/16 | −0.65 | −0.77 (−0.05%) |
</details>
<!-- ty-pinned-project-instructions:end -->