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

Configure auto-import to omit src from import statements #753

Open
daara-s opened this issue Feb 5, 2024 · 7 comments
Open

Configure auto-import to omit src from import statements #753

daara-s opened this issue Feb 5, 2024 · 7 comments

Comments

@daara-s
Copy link

daara-s commented Feb 5, 2024

Is your feature request related to a problem? Please describe.
I work in python projects that use poetry for dependency management, and use a src/ folder for the repository. When importing an anything from within the repo, rope defaults to importing relative to the root directory, instead of from the package source.
e.g.

from src.REPO.x import y

Describe the solution you'd like
I'd like to be able to set a configuration for rope (eg in pytool.toml) to tell Rope to not reference the src in the import, and instead just use the repo name.
e.g.

from REPO.x import y

Describe alternatives you've considered
See discussion in pylsp: python-lsp/python-lsp-server#525

I've experimented with changing the existing config of python_files and source_folders, but this hasn't given much success.

Additional context
I use rope as part of pylsp.

@daara-s
Copy link
Author

daara-s commented Feb 5, 2024

I've created a repo to show an example of what currently happens when using rope in pylsp for importing a function:
https://github.com/daara-s/poetry-repo-example/tree/master/my-package

@bagel897
Copy link
Contributor

bagel897 commented Feb 6, 2024

It'd be better to have it automatically use another venv path. You can probably do this in pylsp by editing the rope config.

@lieryan
Copy link
Member

lieryan commented Feb 6, 2024

pytool.toml is global config file, unless you want this to be applied to all of your projects (which is unlikely), you'll likely want to use a local config file for this purpose, rope reads [tool.rope] section in pyproject.toml but projectroot is given by the integration (pylsp in this case) rather than something you can configure from pyproject.toml.

Maybe rope could read poetry's section in pyproject.toml to find the real application's root? The Project(projectroot) is where you might want to poke around to see if setting the projectroot as projectroot (from editor/lsp server) / src_path_from_pyproject_toml might work.

@fast-90
Copy link

fast-90 commented Sep 29, 2024

Maybe rope could read poetry's section in pyproject.toml to find the real application's root?

I am a bit skeptical about this idea as Poetry's pyproject.toml structure does not comply with e.g. PEP 621, thereby making such a solution really specific to Poetry (which not everyone is using).

I think a better solution would be to introduce a new config in pytool.toml / [tool.rope] (if using pyproject.toml) which is something like project_root, where the user can be specific about which directory they want to use as the project root for rope. Alternatively, you can also make src the project root by default if it exists since I believe it is a sufficiently commonly used layout, but then again you would have to make some assumptions again about how people structure their repo.

@lieryan please let me know what you think, I'm happy to take a first stab if you like any of my ideas.

@last-partizan
Copy link
Contributor

I think, best possible solution is to require 0 setup effort from the user.

  • Try PEP 621 metadata
  • Try [tool.rope]
  • Try $PYTHONPATH, maybe that's a project root. (I already have it configured for some other tools)
  • Try parsing poetry-specific fields in pyproject.toml

Sure, it's adding few extra lines of code, but it just works for thousands of users.

@lieryan
Copy link
Member

lieryan commented Oct 6, 2024

Hi thanks everyone for the interest in this idea. I think it makes sense to read PEP621 metadata to get the project root structure. Reading Poetry metadata can be implemented as well as a fallback source of configuration, if there's anyone interested in it, but I agree that if we implement both, PEP621 should take higher precedence.

@fast-90 feel free to pick up PEP621 project root support, with or without Poetry, if you want to take a stab at implementing this.

I'm not a big fan of having a default list of directories like src that are just being automagically picked up without any explicit settings by the user, but I am not completely against it either. I think a default src should not be implemented yet for now, but if there's enough people wanting it, please make yourself heard and we can reconsider this.

@GereonV
Copy link

GereonV commented Nov 29, 2024

I think src should definitely work somehow and without setup, because this project-structure seems to be the officially recommended one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants