From 8e6ab6d105aeb34eaf61d52ee28e2bd2b00cbe51 Mon Sep 17 00:00:00 2001 From: Avasam Date: Fri, 7 Mar 2025 22:00:50 -0500 Subject: [PATCH] Promote usage of `uv run` (no more python vs python3) --- .gitignore | 4 ++- CONTRIBUTING.md | 5 ++- pyproject.toml | 6 ++++ scripts/create_baseline_stubs.py | 4 +-- scripts/stubsabot.py | 2 +- scripts/sync_protobuf/google_protobuf.py | 2 +- scripts/sync_protobuf/s2clientprotocol.py | 2 +- scripts/sync_protobuf/tensorflow.py | 2 +- tests/README.md | 40 +++++++++++------------ tests/REGRESSION.md | 6 ++-- tests/check_typeshed_structure.py | 2 +- tests/get_external_stub_requirements.py | 2 +- tests/get_stubtest_system_requirements.py | 2 +- tests/mypy_test.py | 2 +- tests/pyright_test.py | 2 +- tests/pytype_test.py | 2 +- tests/regr_test.py | 2 +- tests/runtests.py | 2 +- tests/stubtest_stdlib.py | 2 +- tests/stubtest_third_party.py | 2 +- tests/typecheck_typeshed.py | 2 +- 21 files changed, 51 insertions(+), 44 deletions(-) diff --git a/.gitignore b/.gitignore index 4c5c5703eb12..edd0a9568bc0 100644 --- a/.gitignore +++ b/.gitignore @@ -68,8 +68,10 @@ analyze.py # Mypy cache .mypy_cache/ -# pyenv local python version +# pyenv and uv local python version .python-version +# we don't use uv's lock as we're not actually a project +uv.lock # deliberately local test configuration files stdlib/@tests/stubtest_allowlists/*.local diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index e4f95ca4ed1c..570fbb1538fd 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -300,14 +300,13 @@ Typeshed includes `scripts/create_baseline_stubs.py`. It generates stubs automatically using a tool called [stubgen](https://mypy.readthedocs.io/en/latest/stubgen.html) that comes with mypy. -To get started, fork typeshed, clone your fork, and then -[create a virtualenv](#-or-create-a-local-development-environment). +To get started, fork typeshed, clone your fork, and then [prepare your environment](#preparing-the-environment). You can then install the library with `pip` into the virtualenv and run the script below, replacing `$INSERT_LIBRARY_NAME_HERE` with the name of the library: ```bash (.venv)$ pip install $INSERT_LIBRARY_NAME_HERE -(.venv)$ python3 scripts/create_baseline_stubs.py $INSERT_LIBRARY_NAME_HERE +(.venv)$ uv run scripts/create_baseline_stubs.py $INSERT_LIBRARY_NAME_HERE ``` When the script has finished running, it will print instructions telling you what to do next. diff --git a/pyproject.toml b/pyproject.toml index 2d6e8f2ea013..a1effa052516 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,3 +1,9 @@ +[project] +# This section is needed to avoid writing --no-project everytime https://github.com/astral-sh/uv/issues/8666 +name = "typeshed" +version = "0" +requires-python = ">=3.9" # Minimum version to run tests, used by uv run + [tool.black] line-length = 130 target-version = ["py310"] diff --git a/scripts/create_baseline_stubs.py b/scripts/create_baseline_stubs.py index 3766266adc91..93ae578d3098 100755 --- a/scripts/create_baseline_stubs.py +++ b/scripts/create_baseline_stubs.py @@ -1,9 +1,9 @@ -#!/usr/bin/env python3 +#!/usr/bin/env -S uv run """Script to generate unannotated baseline stubs using stubgen. Basic usage: -$ python3 scripts/create_baseline_stubs.py +$ uv run scripts/create_baseline_stubs.py Run with -h for more help. """ diff --git a/scripts/stubsabot.py b/scripts/stubsabot.py index 3e755ca2d5d4..9ad39745964d 100755 --- a/scripts/stubsabot.py +++ b/scripts/stubsabot.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/env -S uv run from __future__ import annotations import argparse diff --git a/scripts/sync_protobuf/google_protobuf.py b/scripts/sync_protobuf/google_protobuf.py index ee238f82618d..e4e5e258a6a7 100755 --- a/scripts/sync_protobuf/google_protobuf.py +++ b/scripts/sync_protobuf/google_protobuf.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/env -S uv run """ Generates the protobuf stubs for the given protobuf version using mypy-protobuf. Generally, new minor versions are a good time to update the stubs. diff --git a/scripts/sync_protobuf/s2clientprotocol.py b/scripts/sync_protobuf/s2clientprotocol.py index 989f57a4cd8d..42b61c1383fe 100755 --- a/scripts/sync_protobuf/s2clientprotocol.py +++ b/scripts/sync_protobuf/s2clientprotocol.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/env -S uv run """ Generates the protobuf stubs for the given s2clientprotocol version using mypy-protobuf. Generally, new minor versions are a good time to update the stubs. diff --git a/scripts/sync_protobuf/tensorflow.py b/scripts/sync_protobuf/tensorflow.py index b26ee90ccabf..bdd2d3b584a7 100755 --- a/scripts/sync_protobuf/tensorflow.py +++ b/scripts/sync_protobuf/tensorflow.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/env -S uv run """ Generates the protobuf stubs for the given tensorflow version using mypy-protobuf. Generally, new minor versions are a good time to update the stubs. diff --git a/tests/README.md b/tests/README.md index a00b1733146c..49c9fce6f925 100644 --- a/tests/README.md +++ b/tests/README.md @@ -24,17 +24,17 @@ may require extra dependencies external to typeshed to be installed in your virt prior to running the test. You can list or install all of a stubs package's external dependencies using the following script: ```bash -(.venv3)$ python tests/get_external_stub_requirements.py # List external dependencies for -(.venv3)$ python tests/get_external_stub_requirements.py # List external dependencies for and -(.venv3)$ python tests/get_external_stub_requirements.py # List external dependencies for all third-party stubs in typeshed -(.venv3)$ python scripts/install_all_third_party_dependencies.py # Install external dependencies for all third-party stubs in typeshed +(.venv)$ uv run tests/get_external_stub_requirements.py # List external dependencies for +(.venv)$ uv run tests/get_external_stub_requirements.py # List external dependencies for and +(.venv)$ uv run tests/get_external_stub_requirements.py # List external dependencies for all third-party stubs in typeshed +(.venv)$ uv run scripts/install_all_third_party_dependencies.py # Install external dependencies for all third-party stubs in typeshed ``` ## Run all tests for a specific stub Run using: ```bash -(.venv3)$ python3 tests/runtests.py / +(.venv)$ uv run tests/runtests.py / ``` This script will run all tests below for a specific typeshed directory. If a @@ -44,7 +44,7 @@ be selected. A summary of the results will be printed to the terminal. You must provide a single argument which is a path to the stubs to test, like so: `stdlib/os` or `stubs/requests`. -Run `python tests/runtests.py --help` for information on the various configuration options +Run `uv run tests/runtests.py --help` for information on the various configuration options for this script. Note that if you use the `--run-stubtest` flag with the stdlib stubs, whether or not the test passes will depend on the exact version of Python you're using, as well as various other details regarding your local environment. @@ -54,7 +54,7 @@ For more information, see the docs on [`stubtest_stdlib.py`](#stubtest_stdlibpy) Run using: ```bash -(.venv3)$ python3 tests/mypy_test.py +(.venv)$ uv run tests/mypy_test.py ``` The test has two parts: running mypy on the stdlib stubs, @@ -64,7 +64,7 @@ This test is shallow — it verifies that all stubs can be imported but doesn't check whether stubs match their implementation (in the Python standard library or a third-party package). -Run `python tests/mypy_test.py --help` for information on the various configuration options +Run `uv run tests/mypy_test.py --help` for information on the various configuration options for this script. ## pytype\_test.py @@ -74,7 +74,7 @@ Python 3.13 and above. Run using: ```bash -(.venv3)$ python3 tests/pytype_test.py +(.venv)$ uv run tests/pytype_test.py ``` This test works similarly to `mypy_test.py`, except it uses `pytype`. @@ -85,9 +85,9 @@ This test requires [Node.js](https://nodejs.org) to be installed. Although typeshed runs pyright in CI, it does not currently use this script. However, this script uses the same pyright version and configuration as the CI. ```bash -(.venv3)$ python3 tests/pyright_test.py # Check all files -(.venv3)$ python3 tests/pyright_test.py stdlib/sys.pyi # Check one file -(.venv3)$ python3 tests/pyright_test.py -p pyrightconfig.stricter.json # Check with the stricter config. +(.venv)$ uv run tests/pyright_test.py # Check all files +(.venv)$ uv run tests/pyright_test.py stdlib/sys.pyi # Check one file +(.venv)$ uv run tests/pyright_test.py -p pyrightconfig.stricter.json # Check with the stricter config. ``` `pyrightconfig.stricter.json` is a stricter configuration that enables additional @@ -101,7 +101,7 @@ This test runs mypy against the test cases for typeshed's stdlib and third-party stubs. See [the REGRESSION.md document](./REGRESSION.md) in this directory for more information about what -these test cases are for and how they work. Run `python tests/regr_test.py --help` +these test cases are for and how they work. Run `uv run tests/regr_test.py --help` for information on the various configuration options. ## check\_typeshed\_structure.py @@ -110,14 +110,14 @@ This checks that typeshed's directory structure and metadata files are correct. Run using: ```bash -$ python3 tests/check_typeshed_structure.py +$ uv run tests/check_typeshed_structure.py ``` ## stubtest\_stdlib.py Run using ```bash -(.venv3)$ python3 tests/stubtest_stdlib.py +(.venv)$ uv run tests/stubtest_stdlib.py ``` This test compares the stdlib stubs against the objects at runtime. Because of @@ -152,7 +152,7 @@ stubtest on. Run using ```bash -(.venv3)$ python3 tests/stubtest_third_party.py +(.venv)$ uv run tests/stubtest_third_party.py ``` Similar to `stubtest_stdlib.py`, but tests the third party stubs. By default, @@ -160,13 +160,13 @@ it checks all third-party stubs, but you can provide the distributions to check on the command line: ```bash -(.venv3)$ python3 tests/stubtest_third_party.py requests toml # check stubs/requests and stubs/toml +(.venv)$ uv run tests/stubtest_third_party.py requests toml # check stubs/requests and stubs/toml ``` If you have the runtime package installed in your local virtual environment, you can also run stubtest directly, with ```bash -(.venv3)$ MYPYPATH= python3 -m mypy.stubtest \ +(.venv)$ MYPYPATH= uv run -m mypy.stubtest \ --custom-typeshed-dir \ ``` @@ -200,9 +200,9 @@ for missing objects rather than trying to match the runtime in every detail. Run using ```bash -(.venv3)$ python3 tests/typecheck_typeshed.py +(.venv)$ uv run tests/typecheck_typeshed.py ``` This is a small wrapper script that uses mypy to typecheck typeshed's own code in the -`scripts` and `tests` directories. Run `python tests/typecheck_typeshed.py --help` for +`scripts` and `tests` directories. Run `uv run tests/typecheck_typeshed.py --help` for information on the various configuration options. diff --git a/tests/REGRESSION.md b/tests/REGRESSION.md index e0d37edd56f1..b5a11479f584 100644 --- a/tests/REGRESSION.md +++ b/tests/REGRESSION.md @@ -33,15 +33,15 @@ false-positive errors for idiomatic usage of these classes. ## Running the tests To verify the stdlib test cases pass with mypy, run -`python tests/regr_test.py stdlib` from the root of the typeshed repository. +`uv run tests/regr_test.py stdlib` from the root of the typeshed repository. This assumes that the development environment has been set up as described in the [CONTRIBUTING.md](../CONTRIBUTING.md) document. For third-party-library stubs, pass the name of the runtime library the stubs are for. For example, to run the tests for our `requests` stubs, run -`python tests/regr_test.py requests`. +`uv run tests/regr_test.py requests`. -Run `python tests/regr_test.py -h` for the full range of CLI options this script +Run `uv run tests/regr_test.py -h` for the full range of CLI options this script supports. There is no equivalent script for pyright currently; for pyright, the tests are checked in CI using a GitHub Action. diff --git a/tests/check_typeshed_structure.py b/tests/check_typeshed_structure.py index 81adb8c74269..eac79831719c 100755 --- a/tests/check_typeshed_structure.py +++ b/tests/check_typeshed_structure.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/env -S uv run """ Check that the typeshed repository contains the correct files in the diff --git a/tests/get_external_stub_requirements.py b/tests/get_external_stub_requirements.py index 177da48aba7f..c80867bc6d88 100755 --- a/tests/get_external_stub_requirements.py +++ b/tests/get_external_stub_requirements.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/env -S uv run # TODO: It should be possible to specify the Python version and platform # and limit the output to the packages that are compatible with that version diff --git a/tests/get_stubtest_system_requirements.py b/tests/get_stubtest_system_requirements.py index 1e01ceb4f02f..a5e2aa6a4f61 100755 --- a/tests/get_stubtest_system_requirements.py +++ b/tests/get_stubtest_system_requirements.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/env -S uv run import sys from ts_utils.requirements import get_stubtest_system_requirements diff --git a/tests/mypy_test.py b/tests/mypy_test.py index 15cd44bb8b8a..4ecdf96cff55 100755 --- a/tests/mypy_test.py +++ b/tests/mypy_test.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/env -S uv run """Run mypy on typeshed's stdlib and third-party stubs.""" from __future__ import annotations diff --git a/tests/pyright_test.py b/tests/pyright_test.py index 46986a112b4d..a611c7b88ea5 100755 --- a/tests/pyright_test.py +++ b/tests/pyright_test.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/env -S uv run import os import shutil diff --git a/tests/pytype_test.py b/tests/pytype_test.py index 7e3eeb7354bb..80848982ceff 100755 --- a/tests/pytype_test.py +++ b/tests/pytype_test.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/env -S uv run # Lack of pytype typing # pyright: reportUnknownVariableType=false, reportUnknownMemberType=false, reportUnknownArgumentType=false, reportMissingTypeStubs=false """Test runner for typeshed. diff --git a/tests/regr_test.py b/tests/regr_test.py index 248708b90d64..df6af47fdf69 100755 --- a/tests/regr_test.py +++ b/tests/regr_test.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/env -S uv run """Run mypy on the test cases for the stdlib and third-party stubs.""" from __future__ import annotations diff --git a/tests/runtests.py b/tests/runtests.py index e0aad9e95bd2..4dbdfa49f5ba 100755 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/env -S uv run from __future__ import annotations import argparse diff --git a/tests/stubtest_stdlib.py b/tests/stubtest_stdlib.py index a2d2afa90ba2..565bb8ec40f4 100755 --- a/tests/stubtest_stdlib.py +++ b/tests/stubtest_stdlib.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/env -S uv run """Test typeshed's stdlib using stubtest. stubtest is a script in the mypy project that compares stubs to the actual objects at runtime. diff --git a/tests/stubtest_third_party.py b/tests/stubtest_third_party.py index bc1db5a413d2..c4c110635794 100755 --- a/tests/stubtest_third_party.py +++ b/tests/stubtest_third_party.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/env -S uv run """Test typeshed's third party stubs using stubtest.""" from __future__ import annotations diff --git a/tests/typecheck_typeshed.py b/tests/typecheck_typeshed.py index 2fa853ad7d13..cdc9ab9c0e7f 100755 --- a/tests/typecheck_typeshed.py +++ b/tests/typecheck_typeshed.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/env -S uv run """Run mypy on the "tests" and "scripts" directories.""" from __future__ import annotations