build: uv migration - #94
MattiaSarti wants to merge 66 commits into
Conversation
This reverts commit b617bd8.
| terraform-checks: | ||
| name: Terraform | ||
| uses: canonical/charmed-kubeflow-workflows/.github/workflows/terraform-checks.yaml@main | ||
| uses: canonical/charmed-kubeflow-workflows/.github/workflows/terraform-checks.yaml@feat/tox-uv-support |
There was a problem hiding this comment.
Let's not forget to update this to the main branch once canonical/charmed-kubeflow-workflows#110 is merged
There was a problem hiding this comment.
Yes, let's keep this comment conversation as unresolved in order not to forget.
mvlassis
left a comment
There was a problem hiding this comment.
Excellent job with the exploration @MattiaSarti! Very thorough, and much more compact since now everything will be in a single file.
We could potentially add a CONTRIBUTING.md file that includes the information you have already written for using uv to update dependencies, and run tox environments. here is an example in the kfp-operators repo. Not sure whether this is worth the effort though, as using uv is quite straightforward.
Thank you so much! 🥹 |
Description
Purpose
This pull request addesses this issue by proposing a way of migrating to
uv(for Python dependency and environment management) that is natively compatible with the ecosystem of tools already employed - namelycharmcraftandtox.Dependency Maintainability
Instead of combining
.inand.txtrequiements files manually for different environment viapipand derived tools such aspip-compile,uvmanages dependencies for different environments in a unified way and allows to combine dependencies for some environments from others neatly. As a result, manual scripts to combine environment dependencies by compiling and orderly assembling different.inand.txtrequiements files are no longer necessary.Requirements are all listed in
pyproject.tomlacross different extras (groups) than can be used as building blocks to combine all dependencies required by each environment (e.g.: the unit testing environment may define its dependencies as the union of the charm dependencies and some extra dependencies specific to unit tests only). Defining also the charm dependencies as such extras, instead of as mere project dependencies, allows such maintainable logic to use charm dependencies too as building blocks.By default,
uvdoes not allow for any dependency conflicts across environment building blocks. If you wanted to, though,uvcould be configured to ignore such conflicts - as far as the conflicting blocks are never combined to build environments.uvis adopted not only to manage dependencies of the charm itself, thanks to theuvplugin ofcharmcraftand its keywords to specify extras/groups, but also to manage dependencies for all the environments of the CI process withtox.toxEnvironmentsIn fact,
toxcan work by referring to the environments defined inpyproject.toml. Nevertheless, while this achieves defining the dependencies of the differenttoxenvironmentsin a unified way withuv, it does not imply that the dependencies are actually built and run viauvwhen runningtox. As a matter of fact,toxends up internally using pip for that by default (as confirmed by old test run logs). To accomplish this latter purpose, thetox-uvplugin (a tool maintained by the officialtoxteam) was employed, making it possible fortoxto internally rely onuvfor real.An important remark: the reason why dependencies are specified by combining extras instead of dependency groups, despite the
uvplugin ofcharmcraftallowing for both, is that thetox-uvplugin requires extras to work.Building from Source
Also,
uvcan be configured to build packages from source either viapyproject.tomlor via CLI. For the former option, mind the required workarounds for this temporary issue. Nevertheless, as mentioned in the same issue discussion, the latter option will soon be added to theuvplugin ofcharmcraft, so that charms will be made to build from source. Although this pull request does not enable any such options for building from source viacharmcraft, it was locally ensured that theuvsetup allows to build environment dependencies in both modes - see the test section below. Yet, even in the future, it would be beneficial not to build from source the environments run viatoxor any other local command directly executed byuv, to make development and testing lighter and faster - without building dependencies and without requiring other dependencies for the build process in turn. Ideally, only the charm dependencies would be built from source and only when built viacharmcraft.Renovate
Moreover,
uvis supported by Renovate for automated dependency scanning/updating, in particular via the extras of[project.optional-dependencies]that are so relevant to our use case, so the inherited Renovate configurations from upstream repositories can be updated accordingly.Instructions for Adoption
uvis the only tool required locally.Updating Dependencies and/or Python
To add/update/remove any dependencies and/or to upgrade Python, simply:
[project.optional-dependencies]("extras") inpyproject.toml, and/or upgrade Python itself in.python-version[project.optional-dependencies]as extras, specifically in thecharmsection, and not below[project]as project dependencies (see above why)uv lockto just update your lock fileuv sync --extra <your-extra-a> --extra <your-extra-b>(oruv sync --all-extras) if you also want to update your local environment together with your lock file, so that you will be able to run Python code from youruvenvironment locally usinguv run python3 <whatever>By point 2.,
uvwill let you know if there are any dependency conflicts to solve.Running
toxTo run
toxenvironments locally, just:toxas anuvtool together with the requiredtox-uvplugin:uv tool install tox --with tox-uvtoxas you would natively (e.g.:tox -e lint)How Were Changes Tested?
All tests were run during CI and successfully passed. Test steps were modified to install and use
uvin place of the existing dependency management solutions together with itstoxplugin. To this end, some inherited tests had to be modified upstream and were tested connecting to the branch of the respective pull request.Moreover, manual tests were locally executed by performing the following steps on an Ubuntu 24.04 machine and in the root folder of this repository checked out at the last commit of this branch:
Charm Test
test details
Notes:
The following commands aimed at:
Commands:
uvBuild Test: From Source and Nottest details
Notes:
The following commands were executed both with
no-binary = trueand withno-binary = falseinpyproject.toml, confirminguvcan successfully build the project environment(s) both from source, ascharmcraftwill automatically require with future releases of itsuvplugin, and not from source, as currently coded and for lightweight development and testing.Commands: