astral-sh / uv

An extremely fast Python package and project manager, written in Rust.
https://docs.astral.sh/uv
Apache License 2.0
28.16k stars 806 forks source link

Question about reducing code duplication in `[tool.uv.sources]` #8415

Open zmeir opened 1 month ago

zmeir commented 1 month ago

I'll start by saying this is probably not a very common use-case, but I wanted to ask this in case there is some better way of doing what I'm doing that I couldn't find in the documentation.

I have a project with some internal packages as dependencies. These packages are published to a private index, which is then mirrored to 2 different locations. One of these locations is on the same network segment as our development environments, so for development purposes is it faster to download packages from this index. The second one is faster for our ci/automation/production environments. I can differentiate between the environments with the sys_platform marker (we develop on mac and windows, but deploy on linux).

I thought about setting up the [tool.uv.sources] table in pyproject.toml like this:

[tool.uv.sources]
internal-package-1 = [
    { index = "private-index-dev", marker = "sys_platform == 'darwin' or sys_platform == 'win32'" },
    { index = "private-index-prd", marker = "sys_platform != 'darwin' and sys_platform != 'win32'" },
]
internal-package-2 = [
    { index = "private-index-dev", marker = "sys_platform == 'darwin' or sys_platform == 'win32'" },
    { index = "private-index-prd", marker = "sys_platform != 'darwin' and sys_platform != 'win32'" },
]
internal-package-3 = [
    { index = "private-index-dev", marker = "sys_platform == 'darwin' or sys_platform == 'win32'" },
    { index = "private-index-prd", marker = "sys_platform != 'darwin' and sys_platform != 'win32'" },
]

The same 2 lines for private-index-dev and private-index-prd are repeated for each internal package.

Is there any way to prevent this duplication?

As far as I know TOML doesn't support variable substitution, so I can't do something like this:

[tool.uv.sources]
internal-package-settings = [
    { index = "private-index-dev", marker = "sys_platform == 'darwin' or sys_platform == 'win32'" },
    { index = "private-index-prd", marker = "sys_platform != 'darwin' and sys_platform != 'win32'" },
]
internal-package-1 = {internal-package-settings}
internal-package-2 = {internal-package-settings}
internal-package-3 = {internal-package-settings}
zanieb commented 1 month ago

Interesting. Thanks for sharing. I guess we could add something like [tool.uv.source-definitions] then allow tool.uv.sources.<package> = { definition = ... }

It certainly adds some complexity though, so I'm hesitant to pursue it without more requests.