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

Warn when patch version in requires-python is implicitly 0 #7959

Merged
merged 3 commits into from
Oct 16, 2024

Conversation

ianpaul10
Copy link
Contributor

@ianpaul10 ianpaul10 commented Oct 7, 2024

When patch version isn't specified and a matching version is referenced, it will default patch to 0 which could be unclear/confusing. This PR warns the user of that default.

Summary

The first part of this issue #7426. Will tackle the second part mentioned (~=) in a separate PR once I know this is the correct way to warn users.

Test Plan

Unit tests were added

@zanieb zanieb self-assigned this Oct 8, 2024
@zanieb
Copy link
Member

zanieb commented Oct 8, 2024

I'll need to take a closer look, but I was originally thinking we'd only do this for requires-python and that we'd enforce it when we read that value. It'd be nice to add it for other requirements too, but I think we could add those incrementally. If you implement the warning here, there's no way to describe the associated package / field we've read the version from which seems important for having a good message.

You'll also want to use warn_user_once!.

Does that make sense?

cc @BurntSushi would you be interested in being a primary reviewer for this one?

@ianpaul10
Copy link
Contributor Author

Ah yes that makes sense, my mistake. So by "enforce it when we read that value", do you mean check it somewhere in requires_python.rs? I couldn't see exactly where it's being read for a given project.

@BurntSushi BurntSushi self-requested a review October 9, 2024 13:36
@BurntSushi BurntSushi assigned BurntSushi and unassigned zanieb Oct 9, 2024
@BurntSushi
Copy link
Member

@ianpaul10 I don't think we want to do it at a super low level like on RequiresPython itself. It's hard to say exactly when that will get used and whether a warning in all such cases would be appropriate. It's probably better to go higher level. I can see two different spots for this. One is every time we load a pyproject.toml:

/// Parse a `PyProjectToml` from a raw TOML string.
pub fn from_string(raw: String) -> Result<Self, PyprojectTomlError> {
let pyproject: toml_edit::ImDocument<_> =
toml_edit::ImDocument::from_str(&raw).map_err(PyprojectTomlError::TomlSyntax)?;
let pyproject =
PyProjectToml::deserialize(pyproject.into_deserializer()).map_err(|err| {
// TODO(konsti): A typed error would be nicer, this can break on toml upgrades.
if err.message().contains("missing field `name`") {
PyprojectTomlError::MissingName(err)
} else {
PyprojectTomlError::TomlSchema(err)
}
})?;
Ok(PyProjectToml { raw, ..pyproject })
}

Another spot is only when the user runs uv lock, which is also where we have some other warnings related to requires-python:

let requires_python = if let Some(requires_python) = requires_python {
if requires_python.is_unbounded() {
let default =
RequiresPython::greater_than_equal_version(&interpreter.python_minor_version());
warn_user_once!("The workspace `requires-python` value does not contain a lower bound: `{requires_python}`. Set a lower bound to indicate the minimum compatible Python version (e.g., `{default}`).");
}
requires_python
} else {
let default =
RequiresPython::greater_than_equal_version(&interpreter.python_minor_version());
warn_user_once!(
"No `requires-python` value found in the workspace. Defaulting to `{default}`."
);
default
};

I think I would go the latter option. And if you add the warning there, it should show up when running uv sync as well, automatically (among any other commands that try and do a lock). Otherwise, we'd be issuing this warning for almost all uv commands, and I'm not sure we want to go that route.

@ianpaul10 ianpaul10 force-pushed the feat/warn-on-pin-py-ver branch 2 times, most recently from aefc391 to 653eb84 Compare October 11, 2024 08:37
@ianpaul10
Copy link
Contributor Author

Thanks a ton for the in-depth comment. That all makes sense, I didn't really know the best place to put it so that colour is helpful. I took your advice and added it into the lock.rs file.

Let me know if that's different than what you had in mind, and if the tests I included are appropriate.

@@ -711,6 +723,7 @@ mod tests {
use std::cmp::Ordering;
use std::collections::Bound;
use std::str::FromStr;
use test_case::test_case;
Copy link
Member

Choose a reason for hiding this comment

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

Can you move this to the group of non-std crate-external imports below? With uv_pep440 and uv_distribution_filename.

The general pattern in the ecosystem is to put imports in three groups, in this order: std imports, non-crate imports, crate imports. (I also usually separate core and alloc imports too, but that's only for no_std libraries.)

#[test_case(">3.9", false)]
#[test_case("<4.0", false)]
#[test_case(">=3.10, <3.11", false)]
#[test_case("", false)]
Copy link
Member

Choose a reason for hiding this comment

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

I know we're using this macro in at least one other place too, but I'd skip on this and just write out the code instead.

#[test]
fn is_matching_without_patch() {
    let test_cases = [
        ("==3.12", true),
        ("==2.7", true),
        ("3.12.1", false),
        // ...
    ];
    for (version, expected) in test_cases {
        let version_specifiers = VersionSpecifiers::from_str(version).unwrap();
        let requires_python = RequiresPython::from_specifiers(&version_specifiers).unwrap();
        assert_eq!(requires_python.is_matching_without_patch(), expected, "version: {version}");
    }
}

The main reason for this is that it would be great to reduce our dependence on proc macros in service of helping compile times.

@@ -334,6 +334,8 @@ async fn do_lock(
let default =
RequiresPython::greater_than_equal_version(&interpreter.python_minor_version());
warn_user_once!("The workspace `requires-python` value does not contain a lower bound: `{requires_python}`. Set a lower bound to indicate the minimum compatible Python version (e.g., `{default}`).");
} else if requires_python.is_matching_without_patch() {
warn_user_once!("The workspace `requires-python` value is a matching reference without a patch version: `{requires_python}`. It will be interpreted as `{requires_python}.0`. Did you mean `{requires_python}.*`?");
Copy link
Member

Choose a reason for hiding this comment

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

The phrasing seems a touch off here. Maybe this instead? Not totally sure.

Suggested change
warn_user_once!("The workspace `requires-python` value is a matching reference without a patch version: `{requires_python}`. It will be interpreted as `{requires_python}.0`. Did you mean `{requires_python}.*`?");
warn_user_once!("The workspace `requires-python` value does not have a patch version: `{requires_python}`. It will be interpreted as `{requires_python}.0`. Did you mean `{requires_python}.*`?");

#[test_case(">=3.10", false)]
#[test_case(">3.9", false)]
#[test_case("<4.0", false)]
#[test_case(">=3.10, <3.11", false)]
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps an odd case, but this makes me think of something like, ==3.10, <3.11. I think that should return true?

@ianpaul10 ianpaul10 force-pushed the feat/warn-on-pin-py-ver branch 12 times, most recently from 0a2f0c7 to 711ab6e Compare October 14, 2024 12:44
@ianpaul10
Copy link
Contributor Author

ianpaul10 commented Oct 14, 2024

Ok thanks for all the comments, I believe I addressed them all, let me know if there's anything I missed.

Also, I'm not sure what's happening with the windows tests and mkdocs, but it appears to be happening on other PRs 🤔 Are they normally flaky or should I investigate further?

@zanieb
Copy link
Member

zanieb commented Oct 14, 2024

I'll look into that, thanks!

Warn when requires-python is set to an exact value (==) and doesn't include a patch (so it'll get set to 0)
Copy link
Member

@zanieb zanieb left a comment

Choose a reason for hiding this comment

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

Thank you and nice work!

I made some minor tweaks, let me know if you have any questions or concerns.

@zanieb zanieb enabled auto-merge (squash) October 16, 2024 04:07
@zanieb zanieb changed the title Warn when patch isn't specified Warn when patch version in requires-python is implicitly 0 Oct 16, 2024
@ianpaul10
Copy link
Contributor Author

Thanks for reviewing and improving the language! Those all make sense to me 👍

@zanieb zanieb merged commit e71b1d0 into astral-sh:main Oct 16, 2024
61 checks passed
zanieb added a commit that referenced this pull request Oct 16, 2024
Extends #7959

While I was looking at that message, I noticed I didn't love the
readability of the existing message and opted to follow-up with a change
to them both.
tmeijn pushed a commit to tmeijn/dotfiles that referenced this pull request Oct 18, 2024
This MR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [astral-sh/uv](https://github.com/astral-sh/uv) | patch | `0.4.22` -> `0.4.24` |

MR created with the help of [el-capitano/tools/renovate-bot](https://gitlab.com/el-capitano/tools/renovate-bot).

**Proposed changes to behavior should be submitted there as MRs.**

---

### Release Notes

<details>
<summary>astral-sh/uv (astral-sh/uv)</summary>

### [`v0.4.24`](https://github.com/astral-sh/uv/blob/HEAD/CHANGELOG.md#0424)

[Compare Source](astral-sh/uv@0.4.23...0.4.24)

##### Bug fixes

-   Fix Python executable name in Windows free-threaded Python distributions ([#&#8203;8310](astral-sh/uv#8310))
-   Redact index credentials from lockfile sources ([#&#8203;8307](astral-sh/uv#8307))
-   Respect `UV_INDEX_` rather than `UV_HTTP_BASIC_` as documented ([#&#8203;8306](astral-sh/uv#8306))
-   Improve sources deserialization errors ([#&#8203;8308](astral-sh/uv#8308))

##### Documentation

-   Correct pytorch-to-torch reference in docs ([#&#8203;8291](astral-sh/uv#8291))

### [`v0.4.23`](https://github.com/astral-sh/uv/blob/HEAD/CHANGELOG.md#0423)

[Compare Source](astral-sh/uv@0.4.22...0.4.23)

This release introduces a revamped system for defining package indexes, as an alternative to the existing pip-style
`--index-url` and `--extra-index-url` configuration options.

You can now define named indexes in your `pyproject.toml` file using the `[[tool.uv.index]]` table:

```toml
[[tool.uv.index]]
name = "pytorch"
url = "https://download.pytorch.org/whl/cpu"
```

Packages can be pinned to a specific index via `tool.uv.sources`, to ensure that a given package is installed from the
correct index. For example, to ensure that `torch` is *always* installed from the `pytorch` index:

```toml
[tool.uv.sources]
torch = { index = "pytorch" }

[[tool.uv.index]]
name = "pytorch"
url = "https://download.pytorch.org/whl/cpu"
```

Indexes can also be marked as `explicit = true` to prevent packages from being installed from that index
unless explicitly pinned. For example, to ensure that `torch` is installed from the `pytorch` index, but all other
packages are installed from the default index:

```toml
[tool.uv.sources]
torch = { index = "pytorch" }

[[tool.uv.index]]
name = "pytorch"
url = "https://download.pytorch.org/whl/cpu"
explicit = true
```

To define an additional index outside a `pyproject.toml` file, use the `--index` command-line argument
(or the `UV_INDEX` environment variable); to replace the default index (PyPI), use the `--default-index` command-line
argument (or `UV_DEFAULT_INDEX`).

These changes are entirely backwards-compatible with the deprecated `--index-url` and `--extra-index-url` options,
which continue to work as before.

See the [Index](https://docs.astral.sh/uv/configuration/indexes/) documentation for more.

##### Enhancements

-   Add index URLs when provided via `uv add --index` or `--default-index` ([#&#8203;7746](astral-sh/uv#7746))
-   Add support for named and explicit indexes ([#&#8203;7481](astral-sh/uv#7481))
-   Add templates for popular build backends ([#&#8203;7857](astral-sh/uv#7857))
-   Allow multiple pinned indexes in `tool.uv.sources` ([#&#8203;7769](astral-sh/uv#7769))
-   Allow users to incorporate Git tags into dynamic cache keys ([#&#8203;8259](astral-sh/uv#8259))
-   Pin named indexes in `uv add` ([#&#8203;7747](astral-sh/uv#7747))
-   Respect named `--index` and `--default-index` values in `tool.uv.sources` ([#&#8203;7910](astral-sh/uv#7910))
-   Update to latest PubGrub version ([#&#8203;8245](astral-sh/uv#8245))
-   Enable environment variable authentication for named indexes ([#&#8203;7741](astral-sh/uv#7741))
-   Avoid showing lower-bound warning outside of explicit lock and sync ([#&#8203;8234](astral-sh/uv#8234))
-   Improve logging during lock errors ([#&#8203;8258](astral-sh/uv#8258))
-   Improve styling of `requires-python` warnings ([#&#8203;8240](astral-sh/uv#8240))
-   Show hint in resolution failure on `Forbidden` (`403`) or `Unauthorized` (`401`) ([#&#8203;8264](astral-sh/uv#8264))
-   Update to latest `cargo-dist` version (includes new installer features) ([#&#8203;8270](astral-sh/uv#8270))
-   Warn when patch version in `requires-python` is implicitly `0` ([#&#8203;7959](astral-sh/uv#7959))
-   Add more context on client errors during range requests ([#&#8203;8285](astral-sh/uv#8285))

##### Bug fixes

-   Avoid writing duplicate index URLs with `--emit-index-url` ([#&#8203;8226](astral-sh/uv#8226))
-   Fix error leading to out-of-bound panic in `uv-pep508` ([#&#8203;8282](astral-sh/uv#8282))
-   Fix managed distributions of free-threaded Python on Windows ([#&#8203;8268](astral-sh/uv#8268))
-   Fix selection of free-threaded interpreters during default Python discovery ([#&#8203;8239](astral-sh/uv#8239))
-   Ignore sources in build requirements for non-source trees ([#&#8203;8235](astral-sh/uv#8235))
-   Invalid cache when adding lower bound to lockfile ([#&#8203;8230](astral-sh/uv#8230))
-   Respect index priority when storing credentials ([#&#8203;8256](astral-sh/uv#8256))
-   Respect relative paths in `uv build` sources ([#&#8203;8237](astral-sh/uv#8237))
-   Narrow what the pip3.<minor> logic drops from entry points. ([#&#8203;8273](astral-sh/uv#8273))

##### Documentation

-   Add some additional notes to `--index-url` docs ([#&#8203;8267](astral-sh/uv#8267))
-   Add upgrade note to README ([#&#8203;7937](astral-sh/uv#7937))
-   Remove note that "only a single source may be defined for each dependency" ([#&#8203;8243](astral-sh/uv#8243))

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever MR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this MR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box

---

This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy40NDAuNyIsInVwZGF0ZWRJblZlciI6IjM3LjQ0MC43IiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJSZW5vdmF0ZSBCb3QiXX0=-->
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.

3 participants