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 uv python pin #4950

Merged
merged 2 commits into from
Jul 10, 2024
Merged

Add uv python pin #4950

merged 2 commits into from
Jul 10, 2024

Conversation

zanieb
Copy link
Member

@zanieb zanieb commented Jul 9, 2024

Adds a uv python pin command to write to a .python-version file.

We support all of our Python version request formats. We also support a --resolved flag to pin to a specific interpreter instead of the provided version. We canonicalize the request with #4949, it's not just printed verbatim. We always attempt to find the interpreter so we can warn if it's not available. With --resolved, if we can't find the interpreter we fail. If no arguments are provided, we'll attempt to display the current pin.

In the future:

  • We should confirm that this satisfies the Requires-Python metadata if a pyproject.toml is present
  • We should support writing to a uv.python-version field if pyproject.toml or uv.toml are present
  • We should support finding and updating the "nearest" Python version file (looking in ancestors)
  • We should support finding version files in workspaces
  • We should support some sort of global pin

@zanieb zanieb added cli Related to the command line interface preview Experimental behavior labels Jul 9, 2024
@zanieb zanieb changed the base branch from main to zb/python-request-ser July 9, 2024 23:18
@zanieb zanieb changed the title zb/python pin Add uv python pin Jul 9, 2024
@zanieb zanieb force-pushed the zb/python-pin branch 2 times, most recently from c1c1d8a to 492465d Compare July 9, 2024 23:28
@zanieb zanieb marked this pull request as ready for review July 9, 2024 23:32
@zanieb zanieb force-pushed the zb/python-pin branch 2 times, most recently from 8a4d2f1 to 72c3bef Compare July 9, 2024 23:45
@charliermarsh
Copy link
Member

Is .python-version parsed by other tools, though?

@zanieb
Copy link
Member Author

zanieb commented Jul 10, 2024

I believe some tools will parse it, yes, but I'm not sure which. For most uses, we'll be writing a simple version. It may be fair to say if you go outside that it'll only be supported by us. We could also consider only supporting writing simple versions until we have pyproject.toml support, but we already support reading it so... idk.

@zanieb
Copy link
Member Author

zanieb commented Jul 10, 2024

I'll do some research, but this might just be a caveat in the documentation?

@zanieb
Copy link
Member Author

zanieb commented Jul 10, 2024

A brief Google search makes it look like just pyenv.

@charliermarsh
Copy link
Member

Mise also parses it and Rye does too IIRC.

@zanieb
Copy link
Member Author

zanieb commented Jul 10, 2024

So what are you thinking? What does mise do if it's not in the format it expects?

@charliermarsh
Copy link
Member

I'm not certain, I can test it out.

@charliermarsh
Copy link
Member

Actually, it looks like mise uses .tool-versions which is generic (not just for Python).

@charliermarsh
Copy link
Member

It looks like if Rye doesn't recognize it, it just gets ignored.

@charliermarsh
Copy link
Member

It just uses this:

/// Return the [`PythonVersionRequest`] from a `.python-version` file.
fn read_python_version(contents: &str) -> Option<PythonVersionRequest> {
    // Skip empty lines and comments.
    let ver = contents.lines().find(|line| {
        let trimmed = line.trim();
        !(trimmed.is_empty() || trimmed.starts_with('#'))
    })?;

    // Parse the version.
    let ver = ver.parse().ok()?;

    Some(ver)
}

@zanieb
Copy link
Member Author

zanieb commented Jul 10, 2024

Thanks for checking!

I think I'd prefer to support the full syntax of Python requests we support (e.g., it makes sense to allow pinning to [email protected] if that's what your project needs) and just clearly document that other tools may not support those complex requests? We could prioritize pyproject.toml support and only allow the extended syntax there but then people who aren't working in "projects" don't have a way to pin local Python versions consistent with the rest of our interface.

Base automatically changed from zb/python-request-ser to main July 10, 2024 15:24
@zanieb zanieb enabled auto-merge (squash) July 10, 2024 15:49
@zanieb zanieb force-pushed the zb/python-pin branch 2 times, most recently from 8e0be49 to d25e756 Compare July 10, 2024 16:42
@zanieb zanieb merged commit e0fae8e into main Jul 10, 2024
49 checks passed
@zanieb zanieb deleted the zb/python-pin branch July 10, 2024 16:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cli Related to the command line interface preview Experimental behavior
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants