Skip to content

Commit

Permalink
Allow merging requirement specs
Browse files Browse the repository at this point in the history
When a package defines requirements based on self version constraints,
allow those definitions to overlap and the result is a sum of all
requirements matching all constraints.
  • Loading branch information
elprans committed Jan 9, 2025
1 parent 509ea55 commit 6a3956b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 24 deletions.
2 changes: 2 additions & 0 deletions metapkg/packages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
PkgConfigMeta,
canonicalize_name,
get_bundled_pkg,
merge_requirements,
pep440_to_semver,
semver_pre_tag,
)
Expand Down Expand Up @@ -49,6 +50,7 @@
"PkgConfigMeta",
"canonicalize_name",
"get_bundled_pkg",
"merge_requirements",
"pep440_to_semver",
"semver_pre_tag",
"BaseSource",
Expand Down
63 changes: 39 additions & 24 deletions metapkg/packages/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,22 @@ class PkgConfigMeta:

BundledPackage_T = TypeVar("BundledPackage_T", bound="BundledPackage")

RequirementsSpec = Union[
list[str | poetry_dep.Dependency],
Mapping[
str,
list[str],
],
Mapping[
str,
list[str | poetry_dep.Dependency],
],
Mapping[
poetry_constr.VersionConstraint,
list[str | poetry_dep.Dependency],
],
]


class BundledPackage(BasePackage):
ident: ClassVar[str]
Expand All @@ -450,21 +466,8 @@ class BundledPackage(BasePackage):

source_version: str

artifact_requirements: Union[
list[str | poetry_dep.Dependency],
dict[
str | poetry_constr.VersionConstraint,
list[str | poetry_dep.Dependency],
],
] = []
artifact_build_requirements: Union[
list[str | poetry_dep.Dependency],
dict[
str | poetry_constr.VersionConstraint,
list[str | poetry_dep.Dependency],
],
] = []

artifact_requirements: RequirementsSpec = []
artifact_build_requirements: RequirementsSpec = []
build_requires: list[poetry_dep.Dependency]

options: dict[str, Any]
Expand Down Expand Up @@ -907,15 +910,14 @@ def _get_requirements(
if isinstance(ver, str):
ver = poetry_constr.parse_constraint(ver)
if ver.allows(self.version):
req_spec = ver_reqs
break
else:
if spec:
raise RuntimeError(
f"{prop} for {self.name!r} are not "
f"empty, but don't match the requested version "
f"{self.version}"
)
req_spec.extend(ver_reqs)

if not req_spec and spec:
raise RuntimeError(
f"{prop} for {self.name!r} are not "
f"empty, but don't match the requested version "
f"{self.version}"
)
else:
req_spec = spec

Expand Down Expand Up @@ -1218,6 +1220,19 @@ def set_metadata_tags(self, tags: Mapping[str, str]) -> None:
self.metadata_tags = dict(tags)


def merge_requirements(
*specs: RequirementsSpec,
) -> RequirementsSpec:
result = collections.defaultdict(list)
for spec in specs:
if isinstance(spec, list):
result["*"].extend(spec)
else:
for k, v in spec.items():
result[k].extend(v)
return dict(result)


@functools.cache
def _get_bundled_pkg_config_meta(name: str) -> PkgConfigMeta:
package = _get_pkg_in_bundle_repo(poetry_dep.Dependency(name, "*"))
Expand Down

0 comments on commit 6a3956b

Please sign in to comment.