uv icon indicating copy to clipboard operation
uv copied to clipboard

Suggesting `uv add --sync-constraints [URL or file path]` to simply update pyproject.toml constraints

Open agosmou opened this issue 1 month ago • 6 comments

Summary

I'd like to suggest adding a flag to uv add such as: uv add --sync-constraints https://example.com/my-constraints.txt or
uv add --sync-constraints path/to/file/requirements.txt

This would only fetch and update the constraints file section in the pyproject.toml, making the constraints explicit/static and reproducible for future operations (i.e. uv lock/sync).

Example:

Remote constraints file at https://example.com/my-constraints.txt

boto3==1.40.61

pyproject.toml BEFORE syncing:

[project]
dependencies = ["boto3"]

[tool.uv]
constraint-dependencies = ["boto3==1.40.00"]

Run proposed command:

uv add --sync-constraints https://example.com/my-constraints.txt

pyproject.toml AFTER syncing:

[project]
dependencies = ["boto3"]

[tool.uv]
constraint-dependencies = ["boto3==1.40.61"] # <--- updated

Context

  • There was some discussion on this previously so this is an idea forked from this discussion: https://github.com/astral-sh/uv/issues/6518
  • https://docs.astral.sh/uv/reference/cli/#uv-add
  • https://docs.astral.sh/uv/reference/settings/#constraint-dependencies
  • https://packaging.python.org/en/latest/guides/writing-pyproject-toml/

agosmou avatar Oct 30 '25 07:10 agosmou

Another possible approach could be to allow setting the constraints file as a parameter in pyproject.toml - the goal of either one of these is to prevent having to always pass in the --constraints <> flag on a uv add command because that's kind of error prone, especially if you have some projects that need constraints and others that don't.

Our current use case is that we're using uv for writing Apache Airflow DAGs and code - and that is a LONG constraint file (which I manually converted to constraints in our project and do not want to do again when we update)

rogersei avatar Nov 19 '25 22:11 rogersei

Another possible approach could be to allow setting the constraints file as a parameter in pyproject.toml - the goal of either one of these is to prevent having to always pass in the --constraints <> flag on a uv add command because that's kind of error prone, especially if you have some projects that need constraints and others that don't.

From #6518 it seemed like they want static pyproject.toml dependencies

What about something like:

uv add --init-constraints example.com Or uv add --init-constraints path-to-file

And then the pyproject.toml will fill in the constraints and save the source. Any future uv sync could check the source and update if there is a diff?

Idk brainstorming to iron this out and could use some input


I have a similar use case but feel like I could be satisfied in the meantime by something like:

uv add --sync-constraints example.com && uv sync

agosmou avatar Nov 19 '25 22:11 agosmou

I wonder if uv add --constraints ... without any package name should just import the constraints as described?

zanieb avatar Nov 20 '25 00:11 zanieb

I wonder if uv add --constraints ... without any package name should just import the constraints as described?

Yeah I think this syntax is nice and the scope is decent

uv add --constraints constraints.txt Or uv add --constraints example.com

And then we could pair with a:

uv sync

seamlessly without the crazy manual wrangling of constraints 👍

agosmou avatar Nov 20 '25 00:11 agosmou

uv add --constraints constraints.txt Or uv add --constraints example.com

@zanieb Should I start trying to work through this? I don't think there are more requirements than what we've discussed already.

agosmou avatar Nov 23 '25 23:11 agosmou

Feel free to put up a pull request.

zanieb avatar Nov 24 '25 16:11 zanieb

Feel free to put up a pull request.

Will do!

agosmou avatar Nov 26 '25 09:11 agosmou