From 79ffad62ce8ebdbb3b80dd607f2edfcf791e071f Mon Sep 17 00:00:00 2001 From: Avasam Date: Mon, 2 Jan 2023 19:31:55 -0500 Subject: [PATCH 1/4] Support third-party stub external dependencies in pytype --- .github/workflows/daily.yml | 2 +- .github/workflows/stubtest_third_party.yml | 2 +- .github/workflows/tests.yml | 31 ++++++++++++++++--- CONTRIBUTING.md | 4 ++- stubs/D3DShot/METADATA.toml | 2 +- stubs/JACK-Client/METADATA.toml | 1 + stubs/pycocotools/METADATA.toml | 1 + tests/README.md | 13 +++++++- tests/get_external_stub_requirements.py | 18 +++++++++++ ...py => get_stubtest_system_requirements.py} | 0 tests/utils.py | 4 +-- 11 files changed, 66 insertions(+), 12 deletions(-) create mode 100644 tests/get_external_stub_requirements.py rename tests/{get_packages.py => get_stubtest_system_requirements.py} (100%) mode change 100755 => 100644 diff --git a/.github/workflows/daily.yml b/.github/workflows/daily.yml index 82a8b46b56a1..164bd058a3f1 100644 --- a/.github/workflows/daily.yml +++ b/.github/workflows/daily.yml @@ -71,7 +71,7 @@ jobs: - name: Run stubtest shell: bash run: | - PACKAGES=$(python tests/get_packages.py) + PACKAGES=$(python tests/get_stubtest_system_requirements.py) if [ "${{ runner.os }}" = "Linux" ]; then if [ -n "$PACKAGES" ]; then diff --git a/.github/workflows/stubtest_third_party.yml b/.github/workflows/stubtest_third_party.yml index a908d3695c8a..03eb9b6c9b2a 100644 --- a/.github/workflows/stubtest_third_party.yml +++ b/.github/workflows/stubtest_third_party.yml @@ -58,7 +58,7 @@ jobs: if [ -n "$STUBS" ]; then echo "Testing $STUBS..." - PACKAGES=$(python tests/get_packages.py $STUBS) + PACKAGES=$(python tests/get_stubtest_system_requirements.py $STUBS) if [ "${{ runner.os }}" = "Linux" ]; then if [ -n "$PACKAGES" ]; then diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index a9905ae1c7ed..d5a87ebb977c 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -7,8 +7,8 @@ on: - main pull_request: paths-ignore: - - '**/*.md' - - 'scripts/**' + - "**/*.md" + - "scripts/**" permissions: contents: read @@ -69,6 +69,13 @@ jobs: cache: pip cache-dependency-path: requirements-tests.txt - run: pip install -r requirements-tests.txt + - name: Install external dependencies for 3rd-party stubs + run: | + DEPENDENCIES=$(python tests/get_external_stub_requirements.py) + if [ -n "$DEPENDENCIES" ]; then + echo "Installing packages: $DEPENDENCIES" + pip install $DEPENDENCIES + fi - run: ./tests/pytype_test.py --print-stderr mypy: @@ -112,12 +119,26 @@ jobs: fail-fast: false steps: - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 + with: + python-version: "3.10" + cache: pip + cache-dependency-path: requirements-tests.txt + - name: Install external dependencies for 3rd-party stubs + run: | + pip install -r requirements-tests.txt + DEPENDENCIES=$(python tests/get_external_stub_requirements.py) + if [ -n "$DEPENDENCIES" ]; then + echo "Installing packages: $DEPENDENCIES" + pip install $DEPENDENCIES + fi + - run: pip freeze --all - name: Get pyright version uses: SebRollen/toml-action@v1.0.2 id: pyright_version with: - file: 'pyproject.toml' - field: 'tool.typeshed.pyright_version' + file: "pyproject.toml" + field: "tool.typeshed.pyright_version" - uses: jakebailey/pyright-action@v1 with: version: ${{ steps.pyright_version.outputs.value }} @@ -137,7 +158,7 @@ jobs: version: ${{ steps.pyright_version.outputs.value }} python-platform: ${{ matrix.python-platform }} python-version: ${{ matrix.python-version }} - no-comments: ${{ matrix.python-version != '3.10' || matrix.python-platform != 'Linux' }} # Having each job create the same comment is too noisy. + no-comments: ${{ matrix.python-version != '3.10' || matrix.python-platform != 'Linux' }} # Having each job create the same comment is too noisy. stub-uploader: name: Run the stub_uploader tests diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 102199c8a6b1..a0ea7f42b870 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -46,9 +46,11 @@ virtual environment. If you're not familiar with what it is and how it works, please refer to this [documentation](https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/). +Some tests require extra setup steps to install the required dependencies. + ### Linux/Mac OS -On Linux and Mac OS, you will be able to run the full test suite on Python 3.8, +On Linux and Mac OS, you will be able to run the full test suite on Python 3.9 or 3.10. To install the necessary requirements, run the following commands from a terminal window: diff --git a/stubs/D3DShot/METADATA.toml b/stubs/D3DShot/METADATA.toml index b66c346f9a8b..2ac9cf7ffb14 100644 --- a/stubs/D3DShot/METADATA.toml +++ b/stubs/D3DShot/METADATA.toml @@ -1,5 +1,5 @@ version = "0.1.*" -requires = ["types-Pillow"] +requires = ["types-Pillow", "numpy"] [tool.stubtest] # TODO: figure out how to run stubtest for this package diff --git a/stubs/JACK-Client/METADATA.toml b/stubs/JACK-Client/METADATA.toml index 20062a169165..b4de4a0b2bf5 100644 --- a/stubs/JACK-Client/METADATA.toml +++ b/stubs/JACK-Client/METADATA.toml @@ -1,4 +1,5 @@ version = "0.5.*" +requires = ["numpy"] [tool.stubtest] ignore_missing_stub = false diff --git a/stubs/pycocotools/METADATA.toml b/stubs/pycocotools/METADATA.toml index 424bb5bd8834..d7aa2ab6ae9a 100644 --- a/stubs/pycocotools/METADATA.toml +++ b/stubs/pycocotools/METADATA.toml @@ -1,4 +1,5 @@ version = "2.0.*" +requires = ["numpy"] [tool.stubtest] ignore_missing_stub = false diff --git a/tests/README.md b/tests/README.md index 4c526becb856..acd3bc465639 100644 --- a/tests/README.md +++ b/tests/README.md @@ -17,7 +17,18 @@ objects at runtime. in the `tests` and `scripts` directories. To run the tests, follow the [setup instructions](../CONTRIBUTING.md#preparing-the-environment) -in the `CONTRIBUTING.md` document. In particular, we recommend running with Python 3.9+. +in the `CONTRIBUTING.md` document. In particular, you have to run with Python 3.9+. + +In order for `pytype_test` and `pyright_test` to work correctly, some third-party stubs may require dependencies external to typeshed to be installed in your virtual environment 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 +# Install external dependencies for all third-party stubs in typeshed +(.venv3)$ DEPENDENCIES=$(python tests/get_external_stub_requirements.py) +(.venv3)$ if [ -n "$DEPENDENCIES" ]; then pip install $DEPENDENCIES; fi +``` ## Run all tests for a specific stub diff --git a/tests/get_external_stub_requirements.py b/tests/get_external_stub_requirements.py new file mode 100644 index 000000000000..df86253e3f87 --- /dev/null +++ b/tests/get_external_stub_requirements.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 +from __future__ import annotations + +import os +import sys + +from utils import read_dependencies + +distributions = sys.argv[1:] +if not distributions: + distributions = os.listdir("stubs") + +requirements = set[str]() +for distribution in distributions: + requirements.update(read_dependencies(distribution).external_pkgs) + +for requirement in sorted(requirements): + print(requirement) diff --git a/tests/get_packages.py b/tests/get_stubtest_system_requirements.py old mode 100755 new mode 100644 similarity index 100% rename from tests/get_packages.py rename to tests/get_stubtest_system_requirements.py diff --git a/tests/utils.py b/tests/utils.py index 208bab902108..a0f0d56ef734 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -7,10 +7,10 @@ import subprocess import sys import venv -from collections.abc import Mapping +from collections.abc import Iterable, Mapping from functools import cache from pathlib import Path -from typing import Iterable, NamedTuple +from typing import NamedTuple from typing_extensions import Annotated import pathspec # type: ignore[import] From 78dcd725c646c3faa9d85096f1e4ef0c638495ce Mon Sep 17 00:00:00 2001 From: Avasam Date: Tue, 3 Jan 2023 19:19:16 -0500 Subject: [PATCH 2/4] Remove demo changes --- stubs/D3DShot/METADATA.toml | 2 +- stubs/JACK-Client/METADATA.toml | 1 - stubs/pycocotools/METADATA.toml | 1 - 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/stubs/D3DShot/METADATA.toml b/stubs/D3DShot/METADATA.toml index 2ac9cf7ffb14..b66c346f9a8b 100644 --- a/stubs/D3DShot/METADATA.toml +++ b/stubs/D3DShot/METADATA.toml @@ -1,5 +1,5 @@ version = "0.1.*" -requires = ["types-Pillow", "numpy"] +requires = ["types-Pillow"] [tool.stubtest] # TODO: figure out how to run stubtest for this package diff --git a/stubs/JACK-Client/METADATA.toml b/stubs/JACK-Client/METADATA.toml index b4de4a0b2bf5..20062a169165 100644 --- a/stubs/JACK-Client/METADATA.toml +++ b/stubs/JACK-Client/METADATA.toml @@ -1,5 +1,4 @@ version = "0.5.*" -requires = ["numpy"] [tool.stubtest] ignore_missing_stub = false diff --git a/stubs/pycocotools/METADATA.toml b/stubs/pycocotools/METADATA.toml index d7aa2ab6ae9a..424bb5bd8834 100644 --- a/stubs/pycocotools/METADATA.toml +++ b/stubs/pycocotools/METADATA.toml @@ -1,5 +1,4 @@ version = "2.0.*" -requires = ["numpy"] [tool.stubtest] ignore_missing_stub = false From c0aff8141a0c725f50ea99e6ddd6e1ee7de334e0 Mon Sep 17 00:00:00 2001 From: Avasam Date: Wed, 4 Jan 2023 13:37:03 -0500 Subject: [PATCH 3/4] Apply suggestions from code review Co-authored-by: Alex Waygood --- .github/workflows/tests.yml | 1 + CONTRIBUTING.md | 2 +- tests/README.md | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index d5a87ebb977c..d1793dbab6f4 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -76,6 +76,7 @@ jobs: echo "Installing packages: $DEPENDENCIES" pip install $DEPENDENCIES fi + - run: pip freeze --all - run: ./tests/pytype_test.py --print-stderr mypy: diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index a0ea7f42b870..dfbcc30b105e 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -46,7 +46,7 @@ virtual environment. If you're not familiar with what it is and how it works, please refer to this [documentation](https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/). -Some tests require extra setup steps to install the required dependencies. +Note that some tests require extra setup steps to install the required dependencies. ### Linux/Mac OS diff --git a/tests/README.md b/tests/README.md index 161a14a474b0..eb696b846b7e 100644 --- a/tests/README.md +++ b/tests/README.md @@ -20,7 +20,7 @@ To run the tests, follow the [setup instructions](../CONTRIBUTING.md#preparing-t in the `CONTRIBUTING.md` document. In particular, you have to run with Python 3.9+. In order for `pytype_test` and `pyright_test` to work correctly, some third-party stubs -may require dependencies external to typeshed to be installed in your virtual environment +may require extra dependencies external to typeshed to be installed in your virtual environment prior to running the test. You can list or install all of a stubs package's external dependencies using the following script: ```bash From ead2b32134c59c82f993c3d1f37423707d30d0d7 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Wed, 4 Jan 2023 18:41:32 +0000 Subject: [PATCH 4/4] fix yaml syntax --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index d1793dbab6f4..c36b24386d07 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -76,7 +76,7 @@ jobs: echo "Installing packages: $DEPENDENCIES" pip install $DEPENDENCIES fi - - run: pip freeze --all + - run: pip freeze --all - run: ./tests/pytype_test.py --print-stderr mypy: