uv icon indicating copy to clipboard operation
uv copied to clipboard

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

Open zmeir opened this issue 1 year ago • 1 comments

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}

zmeir avatar Oct 21 '24 11:10 zmeir

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.

zanieb avatar Oct 21 '24 13:10 zanieb

@zmeir Can you please evaluate KCL for this as a more generic and powerful solution? @zanieb the functional scope of such tools may serve as a good boundary for uv's vision on the scope wrt. templating functionality. After all, implementing such functionality is costly and prone to design issues that need to be revisited later (breaking changes). Then functionally, it's also bound to be complex for users which then results in documentation cost and support cost.

@zmeir you would write a template pyproject.k and do the transformations/DRY stuff in there.

kcl run pyproject.k --format toml

sanmai-NL avatar Feb 24 '25 10:02 sanmai-NL

@sanmai-NL Thanks for the suggestion. Right now, with a handful of packages, this isn't such a big deal for me. At least not enough to add another level of indirection to my projects. But if it gets too complex I'll be sure to take a crack at it.

zmeir avatar Feb 24 '25 20:02 zmeir