Skip to content
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

feat: Add to constraints only packages from requested extras #13

Merged
merged 2 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ repos:
- id: black
pass_filenames: true
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.4.9
rev: v0.5.3
hooks:
- id: ruff
exclude: _vendor|vendored
- repo: https://github.com/pre-commit/mirrors-mypy
rev: 'v1.10.0' # Use the sha / tag you want to point at
rev: 'v1.10.1' # Use the sha / tag you want to point at
hooks:
- id: mypy
- repo: https://github.com/pre-commit/pre-commit-hooks
Expand Down
29 changes: 19 additions & 10 deletions tests/test_parsing_files.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from tox_min_req._parse_dependencies import (
Expand All @@ -12,65 +14,72 @@
import pytest


def test_setup_cfg_parse(data_dir: "Path", monkeypatch: "pytest.MonkeyPatch"):
def test_setup_cfg_parse(data_dir: Path, monkeypatch: pytest.MonkeyPatch):
monkeypatch.setattr("sys.platform", "linux")
setup_file = data_dir / "setup.cfg"

constrains = {
"pytest": "7.0.0",
"pytest-cov": "2.5",
"sphinx": "3.0.0",
"scipy": "1.2.0",
}

assert parse_setup_cfg(
setup_file, python_version="3.7", python_full_version="3.7.1"
setup_file, python_version="3.7", python_full_version="3.7.1", extras=("tests",)
) == {
"numpy": "1.16.0",
**constrains,
}
assert parse_setup_cfg(
setup_file, python_version="3.8", python_full_version="3.8.3"
setup_file, python_version="3.8", python_full_version="3.8.3", extras=("tests",)
) == {
"numpy": "1.18.0",
**constrains,
}
monkeypatch.setattr("sys.platform", "win32")
assert parse_setup_cfg(
setup_file, python_version="3.8", python_full_version="3.8.3"
setup_file, python_version="3.8", python_full_version="3.8.3", extras=("tests",)
) == {
"numpy": "1.18.0",
"pandas": "0.25.0",
**constrains,
}


def test_pyproject_toml_parse(data_dir: "Path", monkeypatch: "pytest.MonkeyPatch"):
def test_pyproject_toml_parse(data_dir: Path, monkeypatch: pytest.MonkeyPatch):
monkeypatch.setattr("sys.platform", "linux")
pyproject_file = data_dir / "pyproject.toml"

constrains = {
"pytest": "7.0.0",
"pytest-cov": "2.5",
"sphinx": "3.0.0",
"scipy": "1.2.0",
}

assert parse_pyproject_toml(
pyproject_file, python_version="3.7", python_full_version="3.7.1"
pyproject_file,
python_version="3.7",
python_full_version="3.7.1",
extras=("tests",),
) == {
"numpy": "1.16.0",
**constrains,
}
assert parse_pyproject_toml(
pyproject_file, python_version="3.8", python_full_version="3.8.3"
pyproject_file,
python_version="3.8",
python_full_version="3.8.3",
extras=("tests",),
) == {
"numpy": "1.18.0",
**constrains,
}
monkeypatch.setattr("sys.platform", "win32")
assert parse_pyproject_toml(
pyproject_file, python_version="3.8", python_full_version="3.8.3"
pyproject_file,
python_version="3.8",
python_full_version="3.8.3",
extras=("tests",),
) == {
"numpy": "1.18.0",
"pandas": "0.25.0",
Expand Down
15 changes: 13 additions & 2 deletions tox_min_req/_parse_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import re
import sys
from collections.abc import Sequence
from configparser import ConfigParser
from pathlib import Path

Expand Down Expand Up @@ -65,7 +66,10 @@ def _parse_setup_cfg_section(


def parse_setup_cfg(
path: str | Path, python_version: str, python_full_version: str
path: str | Path,
python_version: str,
python_full_version: str,
extras: Sequence[str] = (),
) -> dict[str, str]:
"""
Parse the setup.cfg file and return a dict of the dependencies and their lower version constraints.
Expand All @@ -83,6 +87,8 @@ def parse_setup_cfg(
python_full_version,
)
for extra in config["options.extras_require"]:
if extra not in extras:
continue
base_constrains.update(
_parse_setup_cfg_section(
config["options.extras_require"][extra],
Expand All @@ -95,7 +101,10 @@ def parse_setup_cfg(


def parse_pyproject_toml(
path: str | Path, python_version: str, python_full_version: str
path: str | Path,
python_version: str,
python_full_version: str,
extras: Sequence[str] = (),
) -> dict[str, str]:
"""
Parse the pyproject.toml file and return a dict of the dependencies and their lower version constraints.
Expand All @@ -113,6 +122,8 @@ def parse_pyproject_toml(
parse_single_requirement(line, python_version, python_full_version)
)
for extra in data["project"]["optional-dependencies"]:
if extra not in extras:
continue
for line in data["project"]["optional-dependencies"][extra]:
base_constrains.update(
parse_single_requirement(line, python_version, python_full_version)
Expand Down
5 changes: 3 additions & 2 deletions tox_min_req/_tox_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,14 @@ def tox_on_install(tox_env: ToxEnv, arguments: Any, section: str, of_type: str)
project_path = tox_env.core["package_root"]
python_version = ".".join(str(x) for x in tox_env.base_python.version_info[:2])
python_full_version = ".".join(str(x) for x in tox_env.base_python.version_info[:3])
extras = tox_env.conf["extras"]
if (project_path / "setup.cfg").exists():
dependencies = parse_setup_cfg(
project_path / "setup.cfg", python_version, python_full_version
project_path / "setup.cfg", python_version, python_full_version, extras
)
elif (project_path / "pyproject.toml").exists():
dependencies = parse_pyproject_toml(
project_path / "pyproject.toml", python_version, python_full_version
project_path / "pyproject.toml", python_version, python_full_version, extras
)
else: # pragma: no cover
return
Expand Down
Loading