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 for global uv python pin #12115

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open

Conversation

jtfmumm
Copy link
Contributor

@jtfmumm jtfmumm commented Mar 11, 2025

These changes add support for

uv python pin 3.12 --global 

This adds the specified version to a .python-version file in the user-level config directory. uv will now use the user-level version as a fallback if no version is found in the project directory or its ancestors.

Open question: is --global the correct parameter here? --user is more precise, but might not communicate the intent as clearly.

TODO: User documentation.

Closes #4972

Comment on lines 294 to 302
// Request Python 3.11 for local .python-version
uv_snapshot!(context.filters(), context.python_pin().arg("3.11").env(EnvVars::XDG_CONFIG_HOME, xdg.path()), @r###"
success: true
exit_code: 0
----- stdout -----
Pinned `.python-version` to `3.11`
----- stderr -----
"###);
Copy link
Member

Choose a reason for hiding this comment

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

We might want to assert specific file contents after this snapshot? Like the global one is unchanged and the local one is present?

Comment on lines 224 to 243
// On Windows, we can't snapshot an executable file.
#[cfg(not(windows))]
insta::with_settings!({
filters => context.filters(),
}, {
// Should run black in the virtual environment
assert_snapshot!(fs_err::read_to_string(executable).unwrap(), @r###"
#![TEMP_DIR]/tools/black/bin/python
# -*- coding: utf-8 -*-
import sys
from black import patched_main
if __name__ == "__main__":
if sys.argv[0].endswith("-script.pyw"):
sys.argv[0] = sys.argv[0][:-11]
elif sys.argv[0].endswith(".exe"):
sys.argv[0] = sys.argv[0][:-4]
sys.exit(patched_main())
"###);

});
Copy link
Member

Choose a reason for hiding this comment

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

We probably don't need repeated coverage for things like this here, we just need to test that the Python version is respected and this adds a lot of noise.

Comment on lines 245 to 260
insta::with_settings!({
filters => context.filters(),
}, {
// We should have a tool receipt
assert_snapshot!(fs_err::read_to_string(tool_dir.join("black").join("uv-receipt.toml")).unwrap(), @r###"
[tool]
requirements = [{ name = "black" }]
entrypoints = [
{ name = "black", install-path = "[TEMP_DIR]/bin/black" },
{ name = "blackd", install-path = "[TEMP_DIR]/bin/blackd" },
]
[tool.options]
exclude-newer = "2024-03-25T00:00:00Z"
"###);
});
Copy link
Member

Choose a reason for hiding this comment

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

We don't capture the Python version in the receipt. This is actually relevant. What happens if the global Python version changes then you reinstall or upgrade? Test coverage for that would good. I think it's fine to respect the changed global default, personally.

return Ok(None);
// Not found in directory or its ancestors. Looking in user-level config.
return Ok(match user_uv_config_dir() {
Some(user_dir) => Self::discover_user_config(user_dir, options)
Copy link
Member

@zanieb zanieb Mar 11, 2025

Choose a reason for hiding this comment

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

I wonder if we should co-locate this with the uv.toml? Like, if

pub fn system_config_file() -> Option<PathBuf> {
exists and a user-level config does not should we prefer that? I'm leaning away from it right now, but wanted to put the idea out there. Maybe can can consider a --system flag separately in the future once we have more feedback.

@samypr100
Copy link
Collaborator

samypr100 commented Mar 12, 2025

These changes add support for

uv python pin 3.12 --global 

This adds the specified version to a .python-version file in the user-level config directory. uv will now use the user-level version as a fallback if no version is found in the project directory or its ancestors.

Open question: is --global the correct parameter here? --user is more precise, but might not communicate the intent as clearly.

TODO: User documentation.

Closes #4972

I do lean towards --global. For example, git uses --global, --local, and --system.

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.

uv python pin should support "global" pins
3 participants