ruff
2bc6c78e - [ty] introduce local variables for `from` imports of submodules in `__init__.py(i)` (#21173)

Commit
87 days ago
[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
Author
Parents
Loading