Skip to content

Commit

Permalink
fix: better error messages based on feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
  • Loading branch information
henryiii committed Nov 3, 2024
1 parent 623ca83 commit 7338bf9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
4 changes: 3 additions & 1 deletion cibuildwheel/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,8 @@ def get(


class Options:
pyproject_toml: dict[str, Any] | None

def __init__(
self,
platform: PlatformName,
Expand All @@ -574,7 +576,7 @@ def __init__(
with self.package_dir.joinpath("pyproject.toml").open("rb") as f:
self.pyproject_toml = tomllib.load(f)
except FileNotFoundError:
self.pyproject_toml = {}
self.pyproject_toml = None

@property
def config_file_path(self) -> Path | None:
Expand Down
14 changes: 10 additions & 4 deletions cibuildwheel/projectfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ def setup_py_python_requires(content: str) -> str | None:
return None


def get_requires_python_str(package_dir: Path, pyproject_toml: dict[str, Any]) -> str | None:
def get_requires_python_str(package_dir: Path, pyproject_toml: dict[str, Any] | None) -> str | None:
"""Return the python requires string from the most canonical source available, or None"""

# Read in from pyproject.toml:project.requires-python
with contextlib.suppress(KeyError, IndexError, TypeError):
return str(pyproject_toml["project"]["requires-python"])
return str((pyproject_toml or {})["project"]["requires-python"])

# Read in from setup.cfg:options.python_requires
config = configparser.ConfigParser()
Expand All @@ -106,18 +106,24 @@ def get_requires_python_str(package_dir: Path, pyproject_toml: dict[str, Any]) -
return None


def resolve_dependency_groups(pyproject_toml: dict[str, Any], *groups: str) -> tuple[str, ...]:
def resolve_dependency_groups(
pyproject_toml: dict[str, Any] | None, *groups: str
) -> tuple[str, ...]:
"""
Get the packages in dependency-groups for a package.
"""

if not groups:
return ()

if pyproject_toml is None:
msg = f"Didn't find a pyproject.toml, so can't read [dependency-groups] {groups!r} from it!"
raise FileNotFoundError(msg)

try:
dependency_groups_toml = pyproject_toml["dependency-groups"]
except KeyError:
msg = f"Didn't find [dependency-groups], which are needed to resolve {groups!r}."
msg = f"Didn't find [dependency-groups] in pyproject.toml, which is needed to resolve {groups!r}."
raise KeyError(msg) from None

return dependency_groups.resolve(dependency_groups_toml, *groups)
12 changes: 12 additions & 0 deletions unit_test/projectfiles_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from textwrap import dedent

import pytest

from cibuildwheel._compat import tomllib
from cibuildwheel.projectfiles import (
get_requires_python_str,
Expand Down Expand Up @@ -264,3 +266,13 @@ def test_read_dep_groups():
assert resolve_dependency_groups(pyproject_toml, "group1") == ("pkg1", "pkg2")
assert resolve_dependency_groups(pyproject_toml, "group2") == ("pkg3",)
assert resolve_dependency_groups(pyproject_toml, "group1", "group2") == ("pkg1", "pkg2", "pkg3")


def test_dep_group_no_file_error():
with pytest.raises(FileNotFoundError, match="pyproject.toml"):
resolve_dependency_groups(None, "test")


def test_dep_group_no_section_error():
with pytest.raises(KeyError, match="pyproject.toml"):
resolve_dependency_groups({}, "test")

0 comments on commit 7338bf9

Please sign in to comment.