Skip to content
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

Support for lazy import #954

Closed
tobiasdiez opened this issue Aug 18, 2020 · 2 comments
Closed

Support for lazy import #954

tobiasdiez opened this issue Aug 18, 2020 · 2 comments
Labels
as designed Not a bug, working as intended enhancement request New feature or request

Comments

@tobiasdiez
Copy link

Is your feature request related to a problem? Please describe.
In some situations, it is better to lazy import a module. For example, in sage this is via lazy_import('sage.rings.all', 'RDF', 'my_RDF') in place of the standard import ... from ... as ... syntax, see https://doc.sagemath.org/html/en/reference/misc/sage/misc/lazy_import.html.
Pyright currently does not pick up these imports and warns that the class is unknown (my_RDF in the above example).

Describe the solution you'd like
Properly recognize lazy imports.

@tobiasdiez tobiasdiez added the enhancement request New feature or request label Aug 18, 2020
@erictraut
Copy link
Collaborator

There's currently no support in the Python standard for expressing lazy imports. Pyright follows the Python standard and does not contain any special knowledge of third-party library behaviors like the one in sage.

As a workaround, you could use lazy imports in conjunction with traditional imports, but place the traditional imports within a if TYPE_CHECKING: statement. That way, it won't execute at runtime, but the type checker will know about import for type checking purposes.

@erictraut erictraut added the as designed Not a bug, working as intended label Aug 18, 2020
@tobiasdiez
Copy link
Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
as designed Not a bug, working as intended enhancement request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants