Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Allow some auto imports to be relative and others absolute #6462

Closed
maxeonyx opened this issue Sep 27, 2024 · 2 comments
Closed

Allow some auto imports to be relative and others absolute #6462

maxeonyx opened this issue Sep 27, 2024 · 2 comments
Assignees
Labels
auto-import import resolve failure Issues related to not finding imports

Comments

@maxeonyx
Copy link

maxeonyx commented Sep 27, 2024

Hi - I have run into some frustrating behaviour with relative import suggestions. I hope this issue can be helpful.

When using a project that has multiple separate module hierarchies, it is impossible to configure the automatic imports in a way that is satisfactory.

The behaviour we want is that imports from the local module are imported relatively, and imports across modules are imported absolutely.

As far as I can tell, there is no way to get this behaviour, no matter what I set in python.analysis.include, python.analysis.exclude and python.analysis.extraPaths.

I should be able to specify which directories are expected on the python path at runtime, but this does not seem possible.

Environment data

  • Pylance version: v2024.9.2
  • OS and version: Fedora Linux 40 Workstation
  • Python version: Python 3.12.6 (system provided)

Code Snippet

Screenshot from 2024-09-27 13-34-17

A minimal example can be found here: https://github.com/maxeonyx/pyimport-issue-minimal

To run the project, use:

PYTHONPATH=appsrc:depsrc python -m appmodule

Repro Steps

  1. Clone the following repo: https://github.com/maxeonyx/pyimport-issue-minimal

  2. Comment out your VSCode user settings so that they don't interfere with the workspace settings in this project.

  3. Open the repo in VSCode.

  4. Open appsrc/appmodule/__main__.py. Take the suggested action. The created import is not correct

Expected behavior

Relative mode

The following imports should be inserted in relative import mode:

from . import do_thing
from sharedmodule.patterns import retry

Absolute mode

The following imports should be inserted in absolute import mode:

from appmodule import do_thing
from sharedmodule.patterns import retry

Actual behavior

The following imports are what are actually inserted:

Relative mode:

from . import do_thing
from ...depsrc.sharedmodule.patterns import retry

The ...depsrc.sharedmodule.patterns import is NOT correct as per my python.analysis.* settings.

Absolute mode

This works correctly:

from appmodule import do_thing
from sharedmodule.patterns import retry

Note

Note that ...depsrc is NOT expected to be on the python path at runtime. I have set python.analysis.autoSearchPaths to false so the workspace root should not be one of the search paths.

If we have the from ...depsrc.sharedmodule.patterns import retry version, when we run with the following command, we will get ImportError: attempted relative import beyond top-level package.

PYTHONPATH=appsrc:depsrc python -m appmodule
@github-actions github-actions bot added the needs repro Issue has not been reproduced yet label Sep 27, 2024
@rchiodo
Copy link
Contributor

rchiodo commented Sep 27, 2024

Thanks for the issue. This would be an enhancement request, being able to specify which paths should be absolute and which are relative. Probably requiring a new setting.

@rchiodo rchiodo changed the title Broken relative imports inserted with multiple module paths Allow some auto imports to be relative and others absolute Sep 27, 2024
@rchiodo rchiodo removed the needs repro Issue has not been reproduced yet label Sep 27, 2024
@rchiodo
Copy link
Contributor

rchiodo commented Sep 27, 2024

Transferring to a discussion item or up votes.

@rchiodo rchiodo added auto-import import resolve failure Issues related to not finding imports labels Sep 27, 2024
@microsoft microsoft locked and limited conversation to collaborators Sep 27, 2024
@rchiodo rchiodo converted this issue into discussion #6467 Sep 27, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
auto-import import resolve failure Issues related to not finding imports
Projects
None yet
Development

No branches or pull requests

2 participants