-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
How should Pyright consider typing stubs with try-excepted imports? #1358
Comments
Conditional (dynamic) imports are very difficult for a static analyzer. When resolving an import symbol, Pyright uses the last declaration of the symbol in the file regardless of whether the import statement is in an if/else or try/except block. If that order doesn't work for you, you can use a a TYPE_CHECKING conditional to hide declarations that you don't want pyright to consider. Another option is to create a local stub alias. In your case you could create a |
That makes sense. Thank you! |
When working with Python >= 3.8 PyLance fails to pick up the proper types, since it always uses the last last definition of a symbol. See microsoft/pyright#1358
immutables
repo: https://github.com/MagicStack/immutables/blob/master/immutables/__init__.pyhas this code in
__init__.py
:The project has a
_map.pyi
, but nomap.pyi
. Pyright thinks that onlymap
is ever imported, so it doesn't use_map.pyi
.This is fixed by:
or:
but maybe Pyright should somehow use
_map
's typing stub in that case? Or would you say that libraries should just use the second variant, or provide typing stubs formodule
instead of_module
?The text was updated successfully, but these errors were encountered: