ruff/crates/ruff_db/src
Aria Desires 2bc6c78e26
[ty] introduce local variables for `from` imports of submodules in `__init__.py(i)` (#21173)
This rips out the previous implementation in favour of a new
implementation with 3 rules:

- **froms are locals**: a `from..import` can only define locals, it does
not have global
side-effects. Specifically any submodule attribute `a` that's implicitly
introduced by either
`from .a import b` or `from . import a as b` (in an `__init__.py(i)`) is
a local and not a
global. If you do such an import at the top of a file you won't notice
this. However if you do
such an import in a function, that means it will only be function-scoped
(so you'll need to do
it in every function that wants to access it, making your code less
sensitive to execution
    order).

- **first from first serve**: only the *first* `from..import` in an
`__init__.py(i)` that imports a
particular direct submodule of the current package introduces that
submodule as a local.
Subsequent imports of the submodule will not introduce that local. This
reflects the fact that
in actual python only the first import of a submodule (in the entire
execution of the program)
introduces it as an attribute of the package. By "first" we mean "the
first time in this scope
(or any parent scope)". This pairs well with the fact that we are
specifically introducing a
local (as long as you don't accidentally shadow or overwrite the local).

- **dot re-exports**: `from . import a` in an `__init__.pyi` is
considered a re-export of `a`
(equivalent to `from . import a as a`). This is required to properly
handle many stubs in the
    wild. Currently it must be *exactly* `from . import ...`.
    
This implementation is intentionally limited/conservative (notably,
often requiring a from import to be relative). I'm going to file a ton
of followups for improvements so that their impact can be evaluated
separately.


Fixes https://github.com/astral-sh/ty/issues/133
2025-11-10 23:04:56 +00:00
..
diagnostic Standardize syntax error construction (#20903) 2025-10-16 11:56:32 -04:00
files [ty] Fix panic 'missing root' when handling completion request (#20917) 2025-10-16 16:23:02 +02:00
system [ty] Smaller refactors to server API in prep for notebook support (#21095) 2025-10-31 20:00:04 +00:00
vendored update `get-size2` to 0.7.0 2025-09-22 17:37:46 -04:00
display.rs Upgrade Rust toolchain to 1.83 (#14677) 2024-11-29 12:05:05 +00:00
file_revision.rs [ty] Track heap usage of salsa structs (#19790) 2025-08-12 13:28:44 +02:00
files.rs [ty] introduce local variables for `from` imports of submodules in `__init__.py(i)` (#21173) 2025-11-10 23:04:56 +00:00
lib.rs [ty] Disallow std::env and io methods in most ty crates (#20046) 2025-08-22 11:13:47 -07:00
panic.rs Display diffs for `ruff format --check` and add support for different output formats (#20443) 2025-09-30 12:00:51 -04:00
parsed.rs [ty] Shrink size of `AstNodeRef` (#20028) 2025-08-22 17:03:22 -04:00
source.rs [ty] Avoid overcounting shared memory usage (#19773) 2025-08-06 15:32:02 -04:00
system.rs [ty] Disallow std::env and io methods in most ty crates (#20046) 2025-08-22 11:13:47 -07:00
testing.rs Update salsa past generational id change (#18362) 2025-05-30 15:31:33 +02:00
vendored.rs Update Rust toolchain to 1.89 (#19807) 2025-08-07 18:21:50 +02:00