pylance-release icon indicating copy to clipboard operation
pylance-release copied to clipboard

Symbols defined in private modules and re-exported in public modules are auto-imported from private modules

Open rsokl opened this issue 10 months ago • 3 comments

Description

For a local project, when a symbol is defined in a private module (prefixed with _) and re-exported in a public module, Pylance's auto-import feature suggests importing from the private module instead of the public interface.

Reproduction Steps

Here's a minimal project structure to reproduce the issue:

my_package/
├── __init__.py
└── _private.py

With the following content:

my_package/_private.py

class MyClass:
    def hello(self):
        return "Hello from MyClass"

my_package/__init__.py

from .public import MyClass

__all__ = ["MyClass"]

Expected Behavior

When typing MyClass in a new file outside the package, Pylance should suggest:

from my_package import MyClass

Actual Behavior

Instead, Pylance suggests:

from my_package._private import MyClass

Environment

  • VS Code version: 1.97
  • Pylance version: 2025.2.1

rsokl avatar Feb 26 '25 14:02 rsokl

I have also seen this.

Import suggestions should recommend the shortest public import path available before suggesting importing from private modules. For this to work people will need to explicitly re-export symbols by including it in __all__, otherwise it's undecidable if an import is a re-export or a use.

ktbarrett avatar Mar 18 '25 16:03 ktbarrett

Any chance that this gets fixed?

sh-shahrokhi avatar May 23 '25 23:05 sh-shahrokhi

Take a look at https://github.com/microsoft/pylance-release/blob/main/docs/settings/python_analysis_includeAliasesFromUserFiles.md

heejaechang avatar May 23 '25 23:05 heejaechang