ruff/crates/ty_python_semantic/resources/mdtest/import
Carl Meyer fe4ee81b97
[ty] prefer submodule over module __getattr__ in from-imports (#21260)
Fixes https://github.com/astral-sh/ty/issues/1053

## Summary

Other type checkers prioritize a submodule over a package `__getattr__`
in `from mod import sub`, even though the runtime precedence is the
other direction. In effect, this is making an implicit assumption that a
module `__getattr__` will not handle (that is, will raise
`AttributeError`) for names that are also actual submodules, rather than
shadowing them. In practice this seems like a realistic assumption in
the ecosystem? Or at least the ecosystem has adapted to it, and we need
to adapt this precedence also, for ecosystem compatibility.

The implementation is a bit ugly, precisely because it departs from the
runtime semantics, and our implementation is oriented toward modeling
runtime semantics accurately. That is, `__getattr__` is modeled within
the member-lookup code, so it's hard to split "member lookup result from
module `__getattr__`" apart from other member lookup results. I did this
via a synthetic `TypeQualifier::FROM_MODULE_GETATTR` that we attach to a
type resulting from a member lookup, which isn't beautiful but it works
well and doesn't introduce inefficiency (e.g. redundant member lookups).

## Test Plan

Updated mdtests.

Also added a related mdtest formalizing our support for a module
`__getattr__` that is explicitly annotated to accept a limited set of
names. In principle this could be an alternative (more explicit) way to
handle the precedence problem without departing from runtime semantics,
if the ecosystem would adopt it.

### Ecosystem analysis

Lots of removed diagnostics which are an improvement because we now
infer the expected submodule.

Added diagnostics are mostly unrelated issues surfaced now because we
previously had an earlier attribute error resulting in `Unknown`; now we
correctly resolve the module so that earlier attribute error goes away,
we get an actual type instead of `Unknown`, and that triggers a new
error.

In scipy and sklearn, the module `__getattr__` which we were respecting
previously is un-annotated so returned a forgiving `Unknown`; now we
correctly see the actual module, which reveals some cases of
https://github.com/astral-sh/ty/issues/133 that were previously hidden
(`scipy/optimize/__init__.py` [imports `from
._tnc`](eff82ca575/scipy/optimize/__init__.py (L429)).)

---------

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
2025-11-03 15:24:01 -05:00
..
basic.md [ty] Check typeshed VERSIONS for parent modules when reporting failed stdlib imports (#20908) 2025-10-16 13:25:08 +00:00
builtins.md
case_sensitive.md
conditional.md [ty] No union with Unknown for module-global symbols (#20664) 2025-10-01 16:40:30 +02:00
conflicts.md
conventions.md [ty] Rename "possibly unbound" diagnostics to "possibly missing" (#20492) 2025-09-23 14:26:55 +00:00
cyclic.md [ty] Recognize submodules in self-referential imports (#18005) 2025-05-13 16:59:11 +02:00
dunder_all.md [ty] No union with Unknown for module-global symbols (#20664) 2025-10-01 16:40:30 +02:00
errors.md [ty] Fix bug where ty would think all types had an __mro__ attribute (#20995) 2025-10-27 11:19:12 +00:00
invalid_syntax.md Consistently wrap tokens in parser diagnostics in backticks instead of 'quotes' (#21163) 2025-10-31 11:59:11 -04:00
legacy_namespace.md [ty] add legacy namespace package support (#20897) 2025-10-17 03:16:37 +00:00
module_getattr.md [ty] prefer submodule over module __getattr__ in from-imports (#21260) 2025-11-03 15:24:01 -05:00
namespace.md [ty] No union with Unknown for module-global symbols (#20664) 2025-10-01 16:40:30 +02:00
nonstandard_conventions.md [ty] Support implicit imports of submodules in __init__.pyi (#20855) 2025-10-31 14:29:24 +00:00
partial_stub_packages.md [ty] Add link for namespaces being partial (#20015) 2025-08-20 21:28:57 -07:00
relative.md [ty] Improve error messages for unresolved attribute diagnostics (#20963) 2025-10-19 10:58:25 +01:00
site_packages_discovery.md [ty] Improve tests for site-packages discovery (#18374) 2025-05-30 07:32:21 +01:00
star.md [syntax-errors] Alternative match patterns bind different names (#20682) 2025-10-17 21:35:48 +00:00
stub_packages.md [ty] Fix relative imports in stub packages (#18132) 2025-05-16 15:30:10 +02:00
stubs.md
tracking.md