Support --with-requirements script.py and -r script.py to include inline dependency metadata from another script#12763
Conversation
|
That was more straight-forward than I assumed! I think we'll need to align on the interface. |
|
|
@blueraft I wonder if we can push this forward? I think support in |
Yeah, I think we could do that. Let me give that a try! |
76e0820 to
0cf1c92
Compare
0cf1c92 to
f8d9f5a
Compare
crates/uv/src/lib.rs
Outdated
| let pep723_results = join_all( | ||
| args.with_requirements | ||
| .iter() | ||
| .filter(|file| is_pep723_candidate(file)) | ||
| .map(|file| async move { | ||
| let result = Pep723Script::read(&file).await; | ||
| (file, result) | ||
| }), | ||
| ) | ||
| .await; |
There was a problem hiding this comment.
I'm sorry if this is something we've talked about before (trying to pick this back up now), but why are we doing all this here instead of in RequirementsSource::from_requirements_file? It feels weird to parse these eagerly then filter them from the subsequent call?
There was a problem hiding this comment.
As far as I can tell, RequirementsSource::from_requirements_file is used in a bunch of places, but we may not want to support reading requirements from a PEP723 script in all of those instances. I guess we could pass a flag to that function whether to accept PEP723 scripts or not but no strong preference there
There was a problem hiding this comment.
I see. I think I probably would expect it to support PEP 723 scripts everywhere, e.g., --with-requirements <path> being equivalent to pip install -r <path> and pip compile <path>. I wonder if there are edge-cases where we wouldn't want it? Like -r script.py inside a requirements.txt file? Do we already allow -r pyproject.toml inside requirements.txt files? If so, I guess this would be fine?
There was a problem hiding this comment.
--with-requirements <path>being equivalent topip install -r <path>
Does this mean we would also support uv pip install -r script.py?
Do we already allow -r pyproject.toml inside requirements.txt files?
This doesn't seem to be supported
❯ cat requirements.txt
-r pyproject.toml
flask
❯ uv pip install -r requirements.txt
error: Error parsing included file in `requirements.txt` at position 0
Caused by: Unexpected '[', expected '-c', '-e', '-r' or the start of a requirement at pyproject.toml:1:1
There was a problem hiding this comment.
Does this mean we would also support uv pip install -r script.py?
Yeah, I think so. I think it'd be inconsistent otherwise, and I'm not sure why we shouldn't support it.
This doesn't seem to be supported
Thank goodness :) though I'm confused by that error message 🤔
| bail!("Adding requirements from a `setup.py` is not supported in `uv add`"); | ||
| } | ||
| RequirementsSource::Pep723Script(_) => { | ||
| bail!("Adding requirements from a PEP723 script is not supported in `uv add`"); |
There was a problem hiding this comment.
| bail!("Adding requirements from a PEP723 script is not supported in `uv add`"); | |
| bail!("Adding requirements from a PEP 723 script is not supported in `uv add`"); |
There was a problem hiding this comment.
We probably could support this fwiw — it seems less confusing than importing from another project. It seems good to wait until there's a clear use-case though.
| Ok(Some(script)) => Pep723Item::Script(script), | ||
| Ok(None) => { | ||
| return Err(anyhow::anyhow!( | ||
| "`{}` does not contain a PEP 723 metadata tag; run `{}` to initialize the script", |
There was a problem hiding this comment.
I'd probably omit the run ... to initialize hint. That belongs outside the error message. The dependencies would be empty too, so it'd have no meaningful effect for this kind of usage.
crates/uv/tests/it/tool_run.rs
Outdated
| ----- stdout ----- | ||
|
|
||
| ----- stderr ----- | ||
| error: `not_pep723_script.py` does not contain a PEP 723 metadata tag; run `uv init --script not_pep723_script.py` to initialize the script |
There was a problem hiding this comment.
I wonder if we should just say "script metadata tag"? I generally don't want the specification to be a user-facing concept and prefer to refer to "inline script metadata".
--with-requirements script.py to include inline dependency metadata from another script--with-requirements script.py and -r script.py to include inline dependency metadata from another script
| Ok(Some(script)) => Pep723Item::Script(script), | ||
| Ok(None) => { | ||
| return Err(anyhow::anyhow!( | ||
| "`{}` does not contain an inline script metadata tag.", |
There was a problem hiding this comment.
| "`{}` does not contain an inline script metadata tag.", | |
| "`{}` does not contain inline script metadata", |
Just bike-shedding the message, but note we don't end with a period for single sentence errors..
|
Thank you so much for your patience here! |
This MR contains the following updates: | Package | Update | Change | |---|---|---| | [astral-sh/uv](https://github.com/astral-sh/uv) | patch | `0.8.15` -> `0.8.17` | 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.8.17`](https://github.com/astral-sh/uv/blob/HEAD/CHANGELOG.md#0817) [Compare Source](astral-sh/uv@0.8.16...0.8.17) Released on 2025-09-10. ##### Enhancements - Improve error message for HTTP validation in auth services ([#​15768](astral-sh/uv#15768)) - Respect `PYX_API_URL` when suggesting `uv auth login` on 401 ([#​15774](astral-sh/uv#15774)) - Add pyx as a supported PyTorch index URL ([#​15769](astral-sh/uv#15769)) ##### Bug fixes - Avoid initiating login flow for invalid API keys ([#​15773](astral-sh/uv#15773)) - Do not search for a password for requests with a token attached already ([#​15772](astral-sh/uv#15772)) - Filter pre-release Python versions in `uv init --script` ([#​15747](astral-sh/uv#15747)) ### [`v0.8.16`](https://github.com/astral-sh/uv/blob/HEAD/CHANGELOG.md#0816) [Compare Source](astral-sh/uv@0.8.15...0.8.16) ##### Enhancements - Allow `--editable` to override `editable = false` annotations ([#​15712](astral-sh/uv#15712)) - Allow `editable = false` for workspace sources ([#​15708](astral-sh/uv#15708)) - Show a dedicated error for virtual environments in source trees on build ([#​15748](astral-sh/uv#15748)) - Support Android platform tags ([#​15646](astral-sh/uv#15646)) - Support iOS platform tags ([#​15640](astral-sh/uv#15640)) - Support scripts with inline metadata in `--with-requirements` and `--requirements` ([#​12763](astral-sh/uv#12763)) ##### Preview features - Support `--no-project` in `uv format` ([#​15572](astral-sh/uv#15572)) - Allow `uv format` in unmanaged projects ([#​15553](astral-sh/uv#15553)) ##### Bug fixes - Avoid erroring when `match-runtime` target is optional ([#​15671](astral-sh/uv#15671)) - Ban empty usernames and passwords in `uv auth` ([#​15743](astral-sh/uv#15743)) - Error early for parent path in build backend ([#​15733](astral-sh/uv#15733)) - Retry on IO errors during HTTP/2 streaming ([#​15675](astral-sh/uv#15675)) - Support recursive requirements and constraints inclusion ([#​15657](astral-sh/uv#15657)) - Use token store credentials for `uv publish` ([#​15759](astral-sh/uv#15759)) - Fix virtual environment activation script compatibility with latest nushell ([#​15272](astral-sh/uv#15272)) - Skip Python interpreters that cannot be queried with permission errors ([#​15685](astral-sh/uv#15685)) ##### Documentation - Clarify that `uv auth` commands take a URL ([#​15664](astral-sh/uv#15664)) - Improve the CLI help for options that accept requirements files ([#​15706](astral-sh/uv#15706)) - Adds example for caching for managed Python downloads in Docker builds ([#​15689](astral-sh/uv#15689)) </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:eyJjcmVhdGVkSW5WZXIiOiI0MS45OC4xIiwidXBkYXRlZEluVmVyIjoiNDEuOTkuNiIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOlsiUmVub3ZhdGUgQm90Il19-->
Summary
Closes #6542
Test Plan
cargo test