Skip to content

Commit

Permalink
Remove Unneeded Selectors Class
Browse files Browse the repository at this point in the history
  • Loading branch information
srilman committed Mar 5, 2023
1 parent 236651c commit 8aaad3e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 84 deletions.
17 changes: 0 additions & 17 deletions conda_lock/models/lock_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,12 @@
from conda_lock.virtual_package import FakeRepoData


class Selectors(StrictModel):
platform: Optional[List[str]] = None

def __ior__(self, other: "Selectors") -> "Selectors":
if not isinstance(other, Selectors):
raise TypeError
if other.platform and self.platform:
for p in other.platform:
if p not in self.platform:
self.platform.append(p)
return self

def for_platform(self, platform: str) -> bool:
return self.platform is None or platform in self.platform


class _BaseDependency(StrictModel):
name: str
manager: Literal["conda", "pip"] = "conda"
optional: bool = False
category: str = "main"
extras: List[str] = []
selectors: Selectors = Selectors()


class VersionedDependency(_BaseDependency):
Expand Down
5 changes: 0 additions & 5 deletions conda_lock/src_parser/aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ def aggregate_lock_specs(
lock_spec.dependencies.get(platform, []) for lock_spec in lock_specs
):
key = (dep.manager, dep.name)
if key in unique_deps:
# Override existing, but merge selectors
previous_selectors = unique_deps[key].selectors
previous_selectors |= dep.selectors
dep.selectors = previous_selectors
unique_deps[key] = dep

dependencies[platform] = list(unique_deps.values())
Expand Down
4 changes: 1 addition & 3 deletions conda_lock/src_parser/environment_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ def _parse_environment_file_for_platform(

dependencies: List[Dependency] = []
for spec in specs:
vdep = conda_spec_to_versioned_dep(spec, category)
vdep.selectors.platform = [platform]
dependencies.append(vdep)
dependencies.append(conda_spec_to_versioned_dep(spec, category))

for mapping_spec in mapping_specs:
if "pip" in mapping_spec:
Expand Down
1 change: 0 additions & 1 deletion conda_lock/src_parser/meta_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ def add_spec(spec: str, category: str) -> None:
return

dep = conda_spec_to_versioned_dep(spec, category)
dep.selectors.platform = [platform]
dependencies.append(dep)

def add_requirements_from_recipe_or_output(yaml_data: Dict[str, Any]) -> None:
Expand Down
119 changes: 61 additions & 58 deletions tests/test_conda_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
parse_conda_lock_file,
)
from conda_lock.models.channel import Channel
from conda_lock.models.lock_spec import Selectors, VersionedDependency
from conda_lock.models.lock_spec import VersionedDependency
from conda_lock.pypi_solver import parse_pip_requirement, solve_pypi
from conda_lock.src_parser import (
DEFAULT_PLATFORMS,
Expand Down Expand Up @@ -338,7 +338,7 @@ def test_lock_poetry_ibis(
def test_parse_environment_file(gdal_environment: Path):
res = parse_environment_file(gdal_environment, DEFAULT_PLATFORMS)
assert all(
x in res.dependencies
x in res.dependencies[plat]
for x in [
VersionedDependency(
name="python",
Expand All @@ -351,14 +351,16 @@ def test_parse_environment_file(gdal_environment: Path):
version="",
),
]
for plat in DEFAULT_PLATFORMS
)
assert (
assert all(
VersionedDependency(
name="toolz",
manager="pip",
version="*",
)
in res.dependencies
in res.dependencies[plat]
for plat in DEFAULT_PLATFORMS
)
assert all(
Channel.from_string(x) in res.channels for x in ["conda-forge", "defaults"]
Expand Down Expand Up @@ -387,32 +389,42 @@ def test_parse_env_file_with_filters_no_args(filter_conda_environment: Path):
assert res.channels == [Channel.from_string("conda-forge")]

assert all(
x in res.dependencies
for x in [
VersionedDependency(
name="python",
manager="conda",
version="<3.11",
x in res.dependencies[plat]
for x, platforms in [
(
VersionedDependency(
name="python",
manager="conda",
version="<3.11",
),
platforms,
),
VersionedDependency(
name="clang_osx-arm64",
manager="conda",
version="",
selectors=Selectors(platform=["osx-arm64"]),
(
VersionedDependency(
name="clang_osx-arm64",
manager="conda",
version="",
),
["osx-arm64"],
),
VersionedDependency(
name="clang_osx-64",
manager="conda",
version="",
selectors=Selectors(platform=["osx-64"]),
(
VersionedDependency(
name="clang_osx-64",
manager="conda",
version="",
),
["osx-64"],
),
VersionedDependency(
name="gcc_linux-64",
manager="conda",
version=">=6",
selectors=Selectors(platform=["linux-64"]),
(
VersionedDependency(
name="gcc_linux-64",
manager="conda",
version=">=6",
),
["linux-64"],
),
]
for plat in platforms
)


Expand All @@ -422,26 +434,34 @@ def test_parse_env_file_with_filters_defaults(filter_conda_environment: Path):
assert res.channels == [Channel.from_string("conda-forge")]

assert all(
x in res.dependencies
for x in [
VersionedDependency(
name="python",
manager="conda",
version="<3.11",
x in res.dependencies[plat]
for x, platforms in [
(
VersionedDependency(
name="python",
manager="conda",
version="<3.11",
),
DEFAULT_PLATFORMS,
),
VersionedDependency(
name="clang_osx-64",
manager="conda",
version="",
selectors=Selectors(platform=["osx-64"]),
(
VersionedDependency(
name="clang_osx-64",
manager="conda",
version="",
),
["osx-64"],
),
VersionedDependency(
name="gcc_linux-64",
manager="conda",
version=">=6",
selectors=Selectors(platform=["linux-64"]),
(
VersionedDependency(
name="gcc_linux-64",
manager="conda",
version=">=6",
),
["linux-64"],
),
]
for plat in platforms
)


Expand Down Expand Up @@ -568,13 +588,6 @@ def test_parse_meta_yaml_file(meta_yaml_environment: Path):
for plat in platforms:
specs = {dep.name: dep for dep in res.dependencies[plat]}
assert all(x in specs for x in ["python", "numpy"])
assert all(
dep.selectors
== Selectors(
platform=None
) # Platform will be set to None if all dependencies are the same
for dep in specs.values()
)
# Ensure that this dep specified by a python selector is ignored
assert "enum34" not in specs
# Ensure that this platform specific dep is included
Expand Down Expand Up @@ -1201,16 +1214,6 @@ def _make_spec(name: str, constraint: str = "*"):
)


def _make_dependency_with_platforms(
name: str, platforms: typing.List[str], constraint: str = "*"
):
return VersionedDependency(
name=name,
version=constraint,
selectors=Selectors(platform=platforms),
)


def test_aggregate_lock_specs():
"""Ensure that the way two specs combine when both specify channels is correct"""
base_spec = LockSpecification(
Expand Down

0 comments on commit 8aaad3e

Please sign in to comment.