-
Notifications
You must be signed in to change notification settings - Fork 0
Add versions command to extract supported Python versions from pyproject.toml #194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
15
commits into
main
Choose a base branch
from
copilot/add-versions-command
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+546
−1
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
840d0a0
Initial plan
Copilot ec8894a
Add versions command to extract supported Python versions from pyproj…
Copilot 0027fac
Update CLI documentation with versions command
Copilot 6e5a649
Fix linting issues in versions command and tests
Copilot 7b147db
Merge branch 'main' into copilot/add-versions-command
tschm 1a91e03
Apply suggestion from @Copilot
tschm a064f98
Apply suggestion from @Copilot
tschm 842d56b
Address PR review feedback: Add missing tests, update exports, and cl…
Copilot 4d75bc3
Update src/rhiza/cli.py
tschm b3ac4bc
Improve docstring clarity and add security note to CLI example
Copilot cda436a
Replace custom exceptions with built-in types for consistency
tschm aaaf512
Add missing test coverage for satisfies operators and malformed TOML
tschm 1efc248
Fix linting and type checking issues
tschm db897c2
Add tests for 100% coverage on versions command
tschm 46befd0
Fix ruff PT011 linting error in test
tschm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| """Command for extracting supported Python versions from pyproject.toml. | ||
|
|
||
| This module provides functionality to read a pyproject.toml file and determine | ||
| which Python versions are supported based on the requires-python field. | ||
| """ | ||
|
|
||
| import json | ||
| import re | ||
| import tomllib | ||
| from collections.abc import Callable | ||
| from pathlib import Path | ||
|
|
||
| from loguru import logger | ||
|
|
||
| CANDIDATES = ["3.11", "3.12", "3.13", "3.14"] # extend as needed | ||
tschm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
tschm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| def parse_version(v: str) -> tuple[int, ...]: | ||
| """Parse a version string into a tuple of integers. | ||
|
|
||
| This is intentionally simple and only supports numeric components. | ||
| If a component contains non-numeric suffixes (e.g. '3.11.0rc1'), | ||
| the leading numeric portion will be used (e.g. '0rc1' -> 0). If a | ||
| component has no leading digits at all, a ValueError is raised. | ||
|
|
||
| Args: | ||
| v: Version string to parse (e.g., "3.11", "3.11.0rc1"). | ||
|
|
||
| Returns: | ||
| Tuple of integers representing the version. | ||
|
|
||
| Raises: | ||
| ValueError: If a version component has no numeric prefix. | ||
| """ | ||
| parts: list[int] = [] | ||
| for part in v.split("."): | ||
| match = re.match(r"\d+", part) | ||
| if not match: | ||
| msg = f"Invalid version component {part!r} in version {v!r}; expected a numeric prefix." | ||
| raise ValueError(msg) | ||
| parts.append(int(match.group(0))) | ||
| return tuple(parts) | ||
|
|
||
|
|
||
| def _check_operator(version_tuple: tuple[int, ...], op: str, spec_v_tuple: tuple[int, ...]) -> bool: | ||
| """Check if a version tuple satisfies an operator constraint.""" | ||
| operators: dict[str, Callable[[tuple[int, ...], tuple[int, ...]], bool]] = { | ||
| ">=": lambda v, s: v >= s, | ||
| "<=": lambda v, s: v <= s, | ||
| ">": lambda v, s: v > s, | ||
| "<": lambda v, s: v < s, | ||
| "==": lambda v, s: v == s, | ||
| "!=": lambda v, s: v != s, | ||
| } | ||
| return operators[op](version_tuple, spec_v_tuple) | ||
|
|
||
|
|
||
| def satisfies(version: str, specifier: str) -> bool: | ||
| """Check if a version satisfies a comma-separated list of specifiers. | ||
|
|
||
| This is a simplified version of packaging.specifiers.SpecifierSet. | ||
| Supported operators: >=, <=, >, <, ==, != | ||
|
|
||
| Args: | ||
| version: Version string to check (e.g., "3.11"). | ||
| specifier: Comma-separated specifier string (e.g., ">=3.11,<3.14"). | ||
|
|
||
| Returns: | ||
| True if the version satisfies all specifiers, False otherwise. | ||
|
|
||
| Raises: | ||
| ValueError: If the specifier format is invalid. | ||
| """ | ||
| version_tuple = parse_version(version) | ||
|
|
||
| # Split by comma for multiple constraints | ||
| for spec in specifier.split(","): | ||
| spec = spec.strip() | ||
| # Match operator and version part; require a fully-formed version like '3', '3.11', '3.11.1' | ||
| match = re.fullmatch(r"(>=|<=|>|<|==|!=)\s*(\d+(?:\.\d+)*)", spec) | ||
| if not match: | ||
| # If no operator, assume bare version equality like '3.11' | ||
| if re.fullmatch(r"\d+(?:\.\d+)*", spec): | ||
| if version_tuple != parse_version(spec): | ||
| return False | ||
| continue | ||
| msg = f"Invalid specifier {spec!r}; expected format like '>=3.11' or '3.11'" | ||
| raise ValueError(msg) | ||
|
|
||
| op, spec_v = match.groups() | ||
| spec_v_tuple = parse_version(spec_v) | ||
|
|
||
| if not _check_operator(version_tuple, op, spec_v_tuple): | ||
| return False | ||
|
|
||
| return True | ||
|
|
||
|
|
||
| def supported_versions(pyproject_path: Path) -> list[str]: | ||
| """Return all supported Python versions declared in pyproject.toml. | ||
|
|
||
| Reads project.requires-python, evaluates candidate versions against the | ||
| specifier, and returns the subset that satisfy the constraint, in ascending order. | ||
|
|
||
| Args: | ||
| pyproject_path: Path to the pyproject.toml file. | ||
|
|
||
| Returns: | ||
| list[str]: The supported versions (e.g., ["3.11", "3.12"]). | ||
|
|
||
| Raises: | ||
| RuntimeError: If requires-python is missing or no candidates match. | ||
| FileNotFoundError: If pyproject.toml does not exist. | ||
| """ | ||
| if not pyproject_path.exists(): | ||
| raise FileNotFoundError(f"pyproject.toml not found at: {pyproject_path}") # noqa: TRY003 | ||
|
|
||
| # Load pyproject.toml using the tomllib standard library (Python 3.11+) | ||
| with pyproject_path.open("rb") as f: | ||
| data = tomllib.load(f) | ||
|
|
||
| # Extract the requires-python field from project metadata | ||
| # This specifies the Python version constraint (e.g., ">=3.11") | ||
| spec_str = data.get("project", {}).get("requires-python") | ||
| if not spec_str: | ||
| msg = "pyproject.toml: missing 'project.requires-python'" | ||
| raise RuntimeError(msg) | ||
|
|
||
| # Filter candidate versions to find which ones satisfy the constraint | ||
| versions: list[str] = [] | ||
| for v in CANDIDATES: | ||
| if satisfies(v, spec_str): | ||
| versions.append(v) | ||
|
|
||
| if not versions: | ||
| msg = f"pyproject.toml: no supported Python versions match '{spec_str}'. Evaluated candidates: {CANDIDATES}" | ||
| raise RuntimeError(msg) | ||
|
|
||
| return versions | ||
|
|
||
|
|
||
| def versions(target: Path) -> None: | ||
tschm marked this conversation as resolved.
Show resolved
Hide resolved
tschm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Extract and print supported Python versions from pyproject.toml. | ||
|
|
||
| Args: | ||
| target: Path to pyproject.toml file or directory containing it. | ||
| """ | ||
| target = target.resolve() | ||
|
|
||
| # Determine the pyproject.toml path | ||
| if target.is_file() and target.name == "pyproject.toml": | ||
| pyproject_path = target | ||
| elif target.is_dir(): | ||
| pyproject_path = target / "pyproject.toml" | ||
| else: | ||
| logger.error(f"Invalid target: {target}") | ||
| logger.error("Target must be a directory or pyproject.toml file") | ||
| raise ValueError(f"Invalid target: {target}") # noqa: TRY003 | ||
|
|
||
| logger.info(f"Reading Python version requirements from: {pyproject_path}") | ||
|
|
||
| try: | ||
| versions_list = supported_versions(pyproject_path) | ||
| logger.success(f"Supported Python versions: {versions_list}") | ||
| print(json.dumps(versions_list)) | ||
| except FileNotFoundError as e: | ||
| logger.error(str(e)) | ||
| logger.error("Ensure pyproject.toml exists in the target location") | ||
| raise | ||
| except RuntimeError as e: | ||
| logger.error(str(e)) | ||
| raise | ||
| except ValueError as e: | ||
| logger.error(f"Invalid version specifier: {e}") | ||
| raise | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.