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

Class imported from same-named module in __init__.py is marked as Module instead of Class #867

Closed
vishwa-raj opened this issue Jan 22, 2021 · 3 comments
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@vishwa-raj
Copy link

Environment data

  • Language Server version: 2021.1.2
  • OS and version: win32 x64
  • Python version (and distribution if applicable, e.g. Anaconda):
  • python.analysis.typeCheckingMode: strict

Actual behaviour

In the following example:

  1. In Class_B.py, Class_A is correctly marked as class (red) in from .Class_A import .Class_A.
  2. But in init.py, Class_B is marked as module in from .Class_B import Class_A, Class_B

image

@erictraut
Copy link
Contributor

Thanks for the bug report.

This is a bit of an edge case in Python. The symbol Class_B is both a module and a class in this file. That's because there's a special-case rule in the loader for imports of the form from .A import B if that import is within an "init.py" file. In this case, the local symbol A is assigned the module object. In the case of from .A import A, that local symbol is then immediately overwritten with the value of the imported symbol. So technically, A in this context is both a module and a class. The hover provider currently uses the first declaration to display the type of a symbol. That works best in most cases, but it's not great in this case. I've added some special-case logic to handle this particular situation better.

@erictraut erictraut added bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version and removed triage labels Jan 22, 2021
@vishwa-raj
Copy link
Author

@erictraut Thanks. Looking forward to the fix.

@jakebailey
Copy link
Member

This issue has been fixed in version 2021.1.3, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#202113-27-january-2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version
Projects
None yet
Development

No branches or pull requests

3 participants