Skip to content

Commit

Permalink
Support environment markers in requires fields (#12711)
Browse files Browse the repository at this point in the history
  • Loading branch information
srittau authored Oct 2, 2024
1 parent 213ca9e commit 6ba6589
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 13 deletions.
18 changes: 11 additions & 7 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ jobs:
- run: uv pip install -r requirements-tests.txt --system
- name: Install external dependencies for 3rd-party stubs
run: |
DEPENDENCIES=$(python tests/get_external_stub_requirements.py)
mapfile -t DEPENDENCIES < <( python tests/get_external_stub_requirements.py )
if [ -n "$DEPENDENCIES" ]; then
echo "Installing packages: $DEPENDENCIES"
uv pip install $DEPENDENCIES --system
echo "Installing packages:"
for DEP in "${DEPENDENCIES[@]}"; do echo " ${DEP}"; done
uv pip install "${DEPENDENCIES[@]}" --system
fi
- run: uv pip freeze
- run: ./tests/pytype_test.py --print-stderr
Expand Down Expand Up @@ -129,11 +130,14 @@ jobs:
run: uv venv .venv
- name: Install 3rd-party stub dependencies
run: |
DEPENDENCIES=$(python tests/get_external_stub_requirements.py)
mapfile -t DEPENDENCIES < <( python tests/get_external_stub_requirements.py )
if [ -n "$DEPENDENCIES" ]; then
source .venv/bin/activate
echo "Installing packages: $DEPENDENCIES"
uv pip install $DEPENDENCIES
echo "Installing packages:"
for DEP in "${DEPENDENCIES[@]}"; do echo " ${DEP}"; done
# TODO: We need to specify the platform here, but the platforms
# strings supported by uv are different from the ones supported by
# pyright.
uv pip install --python-version ${{ matrix.python-version }} "${DEPENDENCIES[@]}"
fi
- name: Activate the isolated venv for the rest of the job
run: echo "$PWD/.venv/bin" >> $GITHUB_PATH
Expand Down
3 changes: 3 additions & 0 deletions pyrightconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"stubs",
],
"exclude": [
// Stubs that don't work in all Python versions
"stubs/seaborn",
"stubs/shapely",
// test cases use a custom config file
"**/@tests/test_cases",
],
Expand Down
10 changes: 9 additions & 1 deletion stubs/seaborn/METADATA.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ version = "0.13.2"
# Requires a version of numpy and matplotlib with a `py.typed` file
# see https://github.com/python/typeshed/issues/12551
# on why we need the upper bound for numpy
requires = ["matplotlib>=3.8", "numpy>=1.20,<2.1.0", "pandas-stubs"]
#
# TODO: Specifying the python-version for matplotlib should not be necessary,
# because of the requires_python field. However, this needs changes to
# get_typeshed_stub_version.py (see there).
requires = [
"matplotlib>=3.8; python_version>='3.9'",
"numpy>=1.20,<2.1.0",
"pandas-stubs",
]
# matplotlib>=3.8 requires Python >=3.9
requires_python = ">=3.9"
upstream_repository = "https://github.com/mwaskom/seaborn"
4 changes: 2 additions & 2 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ You can list or install all of a stubs package's external dependencies using the
(.venv3)$ python tests/get_external_stub_requirements.py <third_party_stub1> <third_party_stub2> # List external dependencies for <third_party_stub1> and <third_party_stub2>
(.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
(.venv3)$ mapfile -t DEPENDENCIES < <( python tests/get_external_stub_requirements.py )
(.venv3)$ if [ -n "$DEPENDENCIES" ]; then pip install "${DEPENDENCIES[@]}"; fi
```

## Run all tests for a specific stub
Expand Down
2 changes: 0 additions & 2 deletions tests/_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,6 @@ def read_metadata(distribution: str) -> StubMetadata:

def parse_requires(distribution: str, req: object) -> Requirement:
assert isinstance(req, str), f"Invalid requirement {req!r} for {distribution!r}"
for space in " \t\n":
assert space not in req, f"For consistency, requirement should not have whitespace: {req!r}"
return Requirement(req)


Expand Down
4 changes: 4 additions & 0 deletions tests/get_external_stub_requirements.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/usr/bin/env python3

# 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
# and platform.

from __future__ import annotations

import os
Expand Down
7 changes: 6 additions & 1 deletion tests/pytype_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,12 @@ def _is_supported_stdlib_version(module_versions: SupportedVersionsDict, filenam


def _get_pkgs_associated_with_requirement(req_name: str) -> list[str]:
dist = importlib.metadata.distribution(req_name)
try:
dist = importlib.metadata.distribution(req_name)
except importlib.metadata.PackageNotFoundError:
# The package wasn't installed, probably because an environment
# marker excluded it.
return []
toplevel_txt_contents = dist.read_text("top_level.txt")
if toplevel_txt_contents is None:
if dist.files is None:
Expand Down

0 comments on commit 6ba6589

Please sign in to comment.