-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pyright fails to detect aliased module with submodules #3884
Comments
This is intended behavior. You are relying on implicit import side effects, and pyright intentionally does not model those because they lead to code fragility. If you want your code to work with pyright, you will need to change it to avoid this pattern. Instead, you can do one of the following:
|
In Flask-SQLAlchemy, I want to do this with SQLAlchemy and its submodules. `sqlalchemy... is a long name to type everywhere, and there's so many objects used from there that I want to keep them all under the module namespace instead of importing them all locally. import sqlalchemy as sa
import sqlalchemy.orm
import sqlalchemy.events
# etc.
sa.create_engine
sa.orm.relationship
sa.events.DDLEvents If I add Could you elaborate on what is implicit and fragile about this? Both aliasing imports and importing submodules to add to the module's namespace are documented features of the import system. Why does pyright raise the error when using the alias? |
@davidism I believe you've misunderstood the suggestion - it says to alias the submodule, like But you're right that there is nothing fragile about this. After |
Describe the bug
When aliasing a module, like
import foo as f
, one can access submodules withf.sub
in the code, considering the submodule was brought into scope withimoport foo.sub
, but Pyright fails in two ways: it reports thatimport foo.sub
is unused and errors onf.sub.whatever
.To Reproduce
First, create a module
foo
like so:Now the following code will fail in pyright:
Pyright version
1.1.267
Expected behavior
I would expect the code to type-check.
Additional context
Tested on mypy, and it type-checks
The text was updated successfully, but these errors were encountered: