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

Importing from tests prefers deeper imports over package entry-point

Open DanielRosenwasser opened this issue 11 months ago • 1 comments

Setup

Repro contents available here: https://github.com/DanielRosenwasser/daniel-ts-repros/tree/deepPyAutoImport

# Install pipx
python -m pip install --user pipx
python -m pipx ensurepath

# Install Poetry
pipx install poetry

# Clone Repro
git clone https://github.com/DanielRosenwasser/daniel-ts-repros --branch deepPyAutoImport
cd daniel-ts-repros/pytyped-repro-dir
poetry install
poetry shell
Alternative setup
# Install pipx
python -m pip install --user pipx
python -m pipx ensurepath

# Install Poetry
pipx install poetry

# Create a new src-layout project
poetry new pytyped-repro-dir --src

# Enter the environment
cd pytyped-repro-dir
poetry install
poetry shell

# Turn on strict type-checking for Pyright and *disable extra path lookups*.
printf "\n\n[tool.pyright]\n\ntypeCheckingMode = \"strict\"\nextraPaths = []" >> pyproject.toml

Then create the following files:

# pytyped-repro-dir/src/pytyped_repro_dir/_internal/foo.py

def bar():
  return 42
# pytyped-repro-dir/src/pytyped_repro_dir/__init__.py

from pytyped_repro_dir._internal.foo import bar

__all__ = [
    "bar"
]
# pytyped-repro-dir/tests/__init__.py

bar

Steps

With or without the correct virtual environment activated, in pytyped-repro-dir/tests/__init__.py, request the auto-import quick fix on bar.

Current Behavior

from pytyped_repro_dir._internal.foo import bar

Expected Behavior

from pytyped_repro_dir import bar

DanielRosenwasser avatar Mar 15 '24 19:03 DanielRosenwasser

it is due to us not considering alias symbol on user file.

we probably want to support it when we do full indexing mode.

related to https://github.com/microsoft/pylance-release/issues/5434

heejaechang avatar Mar 15 '24 19:03 heejaechang

This issue has been fixed in prerelease version 2024.9.102, which we've just released. You can find the changelog here: CHANGELOG.md

Please note that you'll need to set python.analysis.includeAliasFromUserFiles to true in order to include alias symbols from user files. This will make alias symbols appear in features like Add Import and Auto Import.

StellaHuang95 avatar Sep 26 '24 21:09 StellaHuang95