Skip to content

Comments

Use uv#2618

Merged
samuelhwilliams merged 12 commits intomasterfrom
use-uv
Jun 7, 2025
Merged

Use uv#2618
samuelhwilliams merged 12 commits intomasterfrom
use-uv

Conversation

@samuelhwilliams
Copy link
Contributor

@samuelhwilliams samuelhwilliams commented Jun 4, 2025

Closely following Flask's switch over to uv in
pallets/flask#5727.


Use uv to manage dev dependencies.

  • tox-uv sets up envs with uv and dependency groups
  • the min and dev test envs need to use a uv pip command to install specific versions after env setup, as there's no way to tell uv to use a different lock file or to tell tox-uv to use dependency groups and individual installs.
  • pre-commit-uv sets up envs with uv
  • workflows use uv to install dependencies, but still use setup-python to install python itself, since that's already cached by github.
  • use uv to build during publish workflow
  • read the docs uses a custom build step
  • tox config is moved into pyproject.toml

New env setup steps are:

  • install uv
  • uv run tox

To do

  • Update README/etc guidance to recommend using uv
  • Update examples to use uv
  • Fix type hints that are breaking sqlalchemy1+typing tests (@ElLorans do you want to help with this? 🙏)

Closely following Flask's switch over to uv in
pallets/flask#5727.
@ElLorans
Copy link
Contributor

ElLorans commented Jun 5, 2025

Sorry, I am new to uv. I think the dev commands should be:

  • uv python pin 3.9 (the minimum Python supported version, otherwise you get as default the last supported one, which right now is 3.12)
  • uv sync
  • tox (uv tox is not recognized on my sistem)

If you need to change Python version, the only option I found was to delete the venv and rerun the previously listed commands, but probably it's me being dense.

EDIT:
Bear with me, still learning. Should we switch to https://github.com/tox-dev/tox-uv too?

@samuelhwilliams
Copy link
Contributor Author

samuelhwilliams commented Jun 5, 2025

Apologies for not giving good instructions. We are switching to tox-uv here yep.

You should be able to, from nothing, in this repo just run uv run tox (or eg uv run tox -e typing). As long as you have uv installed globally, it will install python (if required), create your venv, install deps, and run the command all in one go.

@samuelhwilliams
Copy link
Contributor Author

uv python pin 3.9 (the minimum Python supported version, otherwise you get as default the last supported one, which right now is 3.12)

Yep good call - I think we do need to/should do this 👍

@ElLorans
Copy link
Contributor

ElLorans commented Jun 5, 2025

uv run tox -e typing
obviously gives me

typing: install_deps> python -I -m pip install -r requirements/typing.txt
typing: freeze> python -m pip freeze --all
.pkg: _optional_hooks> python /home/orens/miniforge3/lib/python3.10/site-packages/pyproject_api/_backend.py True flit_core.buildapi
.pkg: get_requires_for_build_wheel> python /home/orens/miniforge3/lib/python3.10/site-packages/pyproject_api/_backend.py True flit_core.buildapi
.pkg: build_wheel> python /home/orens/miniforge3/lib/python3.10/site-packages/pyproject_api/_backend.py True flit_core.buildapi
typing: install_package_deps> python -I -m pip install 'arrow>=0.13.0' 'azure-storage-blob>=12.0.0' 'boto3>=1.33' 'colour>=0.1.5' 'email_validator>=2' 'flask-babel>=3.0.1' 'flask-sqlalchemy>=3' 'flask>=2.0' 'geoalchemy2>=0.14.0' 'jinja2>=3.0' 'markupsafe>=2.0' 'peewee>=3.14.0' 'pillow>=10.0.0' 'pymongo>=3.7.0' 'redis>=4.0.0' 'shapely>=2' 'sqlalchemy-citext>=1.8.0' 'sqlalchemy>=1.4' 'sqlalchemy_utils>=0.38.0' 'tablib>=3.0.0' 'werkzeug>=2.0' 'wtf-peewee>=3.0.4' 'wtforms>=2.3' -c/home/orens/Desktop/MyStuff/Py/Web/flask-admin-use-uv/.tox/typing/constraints.txt
typing: install_package> python -I -m pip install --force-reinstall --no-deps /home/orens/Desktop/MyStuff/Py/Web/flask-admin-use-uv/.tox/.tmp/package/3/flask_admin-2.0.0a4-py3-none-any.whl
typing: commands[0]> mypy --python-version 3.9
Success: no issues found in 97 source files
typing: commands[1]> mypy --python-version 3.12
Success: no issues found in 97 source files
.pkg: _exit> python /home/orens/miniforge3/lib/python3.10/site-packages/pyproject_api/_backend.py True flit_core.buildapi
  typing: OK (60.00=setup[26.37]+cmd[16.90,16.73] seconds)
  congratulations :) (60.08 seconds)

I get the same with uv run --locked tox run -e typing:

.pkg: _optional_hooks> python /home/orens/miniforge3/lib/python3.10/site-packages/pyproject_api/_backend.py True flit_core.buildapi
.pkg: get_requires_for_build_wheel> python /home/orens/miniforge3/lib/python3.10/site-packages/pyproject_api/_backend.py True flit_core.buildapi
.pkg: build_wheel> python /home/orens/miniforge3/lib/python3.10/site-packages/pyproject_api/_backend.py True flit_core.buildapi
typing: install_package> python -I -m pip install --force-reinstall --no-deps /home/orens/Desktop/MyStuff/Py/Web/flask-admin-use-uv/.tox/.tmp/package/4/flask_admin-2.0.0a4-py3-none-any.whl
typing: commands[0]> mypy --python-version 3.9
Success: no issues found in 97 source files
typing: commands[1]> mypy --python-version 3.12
Success: no issues found in 97 source files
.pkg: _exit> python /home/orens/miniforge3/lib/python3.10/site-packages/pyproject_api/_backend.py True flit_core.buildapi
  typing: OK (2.27=setup[1.53]+cmd[0.37,0.37] seconds)
  congratulations :) (2.37 seconds)

This afternoon I will check if the automatic tests are installing different versions but I can immediately see that they are using Python 3.13.

@ElLorans ElLorans self-assigned this Jun 5, 2025
ElLorans added 2 commits June 6, 2025 00:06
uv pip install sqlalchemy <2 fails to reinstall compatible version of flask-sqlalchemy, so specify it.
Previously, version for typing, docs and style was hardcoded to 3.11. This should be reverted to python-version-file: pyproject.toml and pyproject.toml should specify upper bound for python version. Temporarily hardcoding again to pass tests
@ElLorans
Copy link
Contributor

ElLorans commented Jun 5, 2025

It's getting late, tomorrow I will revert 4bb577c

@ElLorans
Copy link
Contributor

ElLorans commented Jun 6, 2025

Thanks for the styling fixes, looks like pre-commit local hook does not work anymore? The good news is:

typing: commands[0]> mypy --python-version 3.9
Success: no issues found in 97 source files
typing: commands[1]> mypy --python-version 3.12
Success: no issues found in 97 source files

The bad news is that now we are checking also with pyright, which is much stricter.

typing: commands[2]> pyright
File or directory "/home/runner/work/flask-admin/flask-admin/tests" does not exist.
353 errors, 1 warning, 0 informations 

It will take me a while to fix them (I agree with pyright) since:

  1. they are not false positives, even though I will have to ignore most of them.
  2. I need to learn pyright and how to work with pyright and mypy in the same codebase

Maybe we can temporarily disable pyright (as we were doing before) and accept the PR?
I can then open a new PR.

@samuelhwilliams
Copy link
Contributor Author

samuelhwilliams commented Jun 6, 2025

I don't think we need to bring pyright in as part of this PR; we can make that a separate PR to address on its own

E: that's what you said; I agree 👍

@samuelhwilliams
Copy link
Contributor Author

Nice. I think we still need to go through an update all the docs to refer to uv. If you want to do that, feel free - otherwise I'll try and get that sorted today/this weekend 🤞

@ElLorans
Copy link
Contributor

ElLorans commented Jun 6, 2025

Nice. I think we still need to go through an update all the docs to refer to uv. If you want to do that, feel free - otherwise I'll try and get that sorted today/this weekend 🤞

I still do not see automatic pre-commit hook, even though I see the yaml. Am I doing something wrong? I ran uv sync and pre-commit is installed.
I already added some type fixes.
I would prefer to do the examples and pyright, since I have never used uv before... Can you do an example so that I can copy the .toml? We would need also a command to uninstall flask-babel, or I can provide a patch to the babel.py in flask-admin.

EDIT: sorry, almost forgot - the way in which I solved the error in sqlalchemy1 feels hacky: in 94b357f, I added a command specifying to install the maximum compatible flask-sqlalchemy version with ["uv", "pip", "install", "flask-sqlalchemy<=3.0.5"],. This looks to me something that pip used to do automatically and uv pip fails to do. Is my patch good enough? Why is uv not fixing the environment automatically?

@samuelhwilliams
Copy link
Contributor Author

Can you try doing uv run pre-commit install --install-hooks?

The hooks work for me after this - let me know if they don't?

@ElLorans
Copy link
Contributor

ElLorans commented Jun 6, 2025

Can you try doing uv run pre-commit install --install-hooks?

The hooks work for me after this - let me know if they don't?

I did that, will see next time I commit and in case we add it to the readme for development.
Another question: uv sync does not install optional dependencies and I do not find on astral docs how to do that.
EDIT:
solved with uv sync --extra all

@samuelhwilliams
Copy link
Contributor Author

samuelhwilliams commented Jun 6, 2025

Ah yeah that's annoying. Looks like uv doesn't let us set this in pyproject.toml yet: astral-sh/uv#8607

Though there is apparently a workaround we can try of adding flask-admin[all] to the dev dependencies.

@ElLorans
Copy link
Contributor

ElLorans commented Jun 6, 2025

I like being able to start with the basic environment or you never catch the mishaps like pillow in the type hints, so as long as we document the right command, it sounds good to me as it is right now

@samuelhwilliams samuelhwilliams marked this pull request as ready for review June 7, 2025 11:45
@samuelhwilliams samuelhwilliams merged commit 3fa2f54 into master Jun 7, 2025
11 checks passed
@samuelhwilliams samuelhwilliams deleted the use-uv branch June 7, 2025 11:47
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 22, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants