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

Add support to detect uv based projects #205

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

afeblot
Copy link
Contributor

@afeblot afeblot commented Sep 29, 2024

uv seems to become the de-facto standard Python package and project manager.

This pull request provides support to detect uv-based projects, based on

  • uv.lock
  • or pyproject.toml containing a [tool.uv] section

and initializes the virtual env with the uv.sync command.

uv sync standard behavior beeing creating the virtual env in $project_dir/.venv/, the virtual env is then handled as a "virtualenv" virtual env type.

$ cd myproj
Python uv project detected. Run mkvenv to setup autoswitching
$ mkvenv
Creating virtual environment at: .venv
[...truncated...]
Switching virtualenv: .venv [Python 3.11.6]

@afeblot
Copy link
Contributor Author

afeblot commented Oct 18, 2024

@MichaelAquilina, any chance you give me a yes/maybe/no answer?

@MichaelAquilina
Copy link
Owner

Hi @afeblot :)

I'm open to the idea of adding support for this package manager in. Thanks for your contribution.

I'll have to get a bit familiar with uv first to test out your PR

@@ -56,6 +56,10 @@ function _get_venv_type() {
venv_type="poetry"
elif [[ -f "$venv_dir/requirements.txt" || -f "$venv_dir/setup.py" ]]; then
venv_type="virtualenv"
elif [[ -f "$venv_dir/uv.lock" ]]; then
venv_type="uv"
elif [[ -f "$venv_dir/pyproject.toml" && $(grep -c '[tool.uv]' "$venv_dir/pyproject.toml") -gt 0 ]]; then
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could this line be turned into a function?

$(grep -c '[tool.uv]' "$venv_dir/pyproject.toml") -gt 0

Something like

function _check_pyproject_type {
  local result
  if [[ $(grep -c '[tool.uv]' "$venv_dir/pyproject.toml") -gt 0 ]]; then
     result = "uv"
  elif [[ $(grep -c '[tool.poetry]' "$venv_dir/pyproject.toml") -gt 0 ]]; then
    result = "poetry"
  ....

  printf "$result"
}
elif [[ -f "$venv_dir/pyproject.toml" ]]; then
    venv_type="$(_check_pyproject_type)"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm. With uv 0.4.27 bringing support for dependency groups as described by PEP 735, and the replacement of

[tool.uv]
dev-dependencies = ["some-dependencies"]

by

[dependency-groups]
dev = ["some-dependencies"]

I don't really know now how to detect that a pyproject.toml file can and is supposed to be handled by uv.
I guess we can't just assume all pyproject.toml are handled by uv.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MichaelAquilina gentle reminder for reviewing this ticket if you can spare the time 🙏

Copy link

@hanslemm hanslemm Dec 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@afeblot I would suggest to use the lock file to determine if it is a poetry or uv managed project. If uv.lock or poetry.lock, handle accordingly.

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

Successfully merging this pull request may close these issues.

4 participants