mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 19:57:03 -04:00
Merge requirement extras and fail on conflicting pins in gen_requirements_all (#186710)
Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude
parent
ff2c6d4649
commit
77888cb2be
@@ -11,6 +11,9 @@ import sys
|
||||
import tomllib
|
||||
from typing import Any
|
||||
|
||||
from packaging.requirements import Requirement
|
||||
from packaging.utils import canonicalize_name
|
||||
|
||||
from homeassistant.util.yaml.loader import load_yaml
|
||||
from script.hassfest.model import Config, Integration
|
||||
|
||||
@@ -379,6 +382,9 @@ def gather_modules() -> dict[str, list[str]] | None:
|
||||
for value in reqs.values():
|
||||
value = sorted(value, key=lambda name: (len(name.split(".")), name))
|
||||
|
||||
if not errors:
|
||||
reqs = merge_requirement_extras(errors, reqs)
|
||||
|
||||
if errors:
|
||||
print("******* ERROR")
|
||||
print("Errors while importing: ", ", ".join(errors))
|
||||
@@ -387,6 +393,35 @@ def gather_modules() -> dict[str, list[str]] | None:
|
||||
return reqs
|
||||
|
||||
|
||||
def merge_requirement_extras(
|
||||
errors: list[str], reqs: dict[str, list[str]]
|
||||
) -> dict[str, list[str]]:
|
||||
"""Merge the requirements of a package into one requirement."""
|
||||
grouped: dict[str, list[str]] = {}
|
||||
for req in reqs:
|
||||
grouped.setdefault(canonicalize_name(Requirement(req).name), []).append(req)
|
||||
|
||||
merged: dict[str, list[str]] = {}
|
||||
for name, group in grouped.items():
|
||||
if len(group) == 1:
|
||||
merged[group[0]] = reqs[group[0]]
|
||||
continue
|
||||
requirements = [Requirement(req) for req in group]
|
||||
if len({(str(req.specifier), str(req.marker)) for req in requirements}) > 1:
|
||||
conflicts = ", ".join(
|
||||
f"{req} ({', '.join(sorted(reqs[req]))})" for req in sorted(group)
|
||||
)
|
||||
errors.append(f"{name}[Conflicting requirements: {conflicts}]")
|
||||
continue
|
||||
requirement = requirements[0]
|
||||
for other in requirements[1:]:
|
||||
requirement.extras |= other.extras
|
||||
merged[str(requirement)] = sorted(
|
||||
{package for req in group for package in reqs[req]}
|
||||
)
|
||||
return merged
|
||||
|
||||
|
||||
def gather_entity_platform_requirements() -> set[str]:
|
||||
"""Gather all of the requirements from manifests for entity platforms."""
|
||||
config = _get_hassfest_config()
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from script import gen_requirements_all
|
||||
|
||||
|
||||
@@ -49,3 +51,50 @@ def test_requirement_override_markers() -> None:
|
||||
gen_requirements_all.process_action_requirement("other==1.0", "pytest")
|
||||
== "other==1.0"
|
||||
)
|
||||
|
||||
|
||||
def test_merge_requirement_extras() -> None:
|
||||
"""Test requirements that only differ in extras are merged."""
|
||||
reqs = {
|
||||
"lorawan-connection[chirpstack]==1.0.0": ["homeassistant.components.a"],
|
||||
"lorawan-connection==1.0.0": ["homeassistant.components.b"],
|
||||
"lorawan_connection[ttn]==1.0.0": ["homeassistant.components.c"],
|
||||
"other[b,a]==2.0.0": ["homeassistant.components.a"],
|
||||
}
|
||||
errors: list[str] = []
|
||||
assert gen_requirements_all.merge_requirement_extras(errors, reqs) == {
|
||||
"lorawan-connection[chirpstack,ttn]==1.0.0": [
|
||||
"homeassistant.components.a",
|
||||
"homeassistant.components.b",
|
||||
"homeassistant.components.c",
|
||||
],
|
||||
# Requirements without a merge keep their original form
|
||||
"other[b,a]==2.0.0": ["homeassistant.components.a"],
|
||||
}
|
||||
assert errors == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"conflicting_req",
|
||||
[
|
||||
pytest.param("pinned==2.0.0", id="version"),
|
||||
pytest.param("pinned[extra]==2.0.0", id="version_with_extra"),
|
||||
pytest.param("pinned==1.0.0;sys_platform=='linux'", id="marker"),
|
||||
],
|
||||
)
|
||||
def test_merge_requirement_extras_conflict(conflicting_req: str) -> None:
|
||||
"""Test requirements of one package with different versions or markers fail."""
|
||||
reqs = {
|
||||
"pinned==1.0.0": ["homeassistant.components.a"],
|
||||
conflicting_req: ["homeassistant.components.b"],
|
||||
"other==1.0.0": ["homeassistant.components.c"],
|
||||
}
|
||||
errors: list[str] = []
|
||||
assert gen_requirements_all.merge_requirement_extras(errors, reqs) == {
|
||||
"other==1.0.0": ["homeassistant.components.c"],
|
||||
}
|
||||
assert errors == [
|
||||
"pinned[Conflicting requirements: "
|
||||
"pinned==1.0.0 (homeassistant.components.a), "
|
||||
f"{conflicting_req} (homeassistant.components.b)]"
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user