Skip to content

Commit 5a1e798

Browse files
committed
dep: add inverted pkg intersects support for Dep and Cpv objects
1 parent 29696cb commit 5a1e798

4 files changed

Lines changed: 25 additions & 8 deletions

File tree

src/pkgcraft/dep/cpv.pyx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ cimport cython
22

33
from .. cimport C
44
from .._misc cimport cstring_to_str
5+
from ..pkg cimport Pkg
56
from ..restrict cimport Restrict
67
from .cpn cimport Cpn
78
from .pkg cimport Dep
@@ -284,7 +285,7 @@ cdef class Cpv:
284285
"""
285286
return C.pkgcraft_cpv_restrict_matches(self.ptr, r.ptr)
286287

287-
def intersects(self, other: Cpv | Dep):
288+
def intersects(self, other):
288289
"""Determine intersection between two Cpv or Dep objects.
289290
290291
Args:
@@ -306,6 +307,8 @@ cdef class Cpv:
306307
return C.pkgcraft_cpv_intersects(self.ptr, (<Cpv>other).ptr)
307308
elif isinstance(other, Dep):
308309
return C.pkgcraft_cpv_intersects_dep(self.ptr, (<Dep>other).ptr)
310+
elif isinstance(other, Pkg):
311+
return C.pkgcraft_pkg_intersects_cpv((<Pkg>other).ptr, self.ptr)
309312
raise TypeError(f"{other.__class__.__name__!r} unsupported type")
310313

311314
def __lt__(self, other):

src/pkgcraft/dep/pkg.pyx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ from cpython.mem cimport PyMem_Free, PyMem_Malloc
55
from .. cimport C
66
from .._misc cimport SENTINEL, cstring_to_str
77
from ..eapi cimport Eapi
8+
from ..pkg cimport Pkg
89
from ..restrict cimport Restrict
910
from ..types cimport OrderedFrozenSet
1011
from .cpn cimport Cpn
@@ -636,7 +637,7 @@ cdef class Dep:
636637
"""
637638
return C.pkgcraft_dep_restrict_matches(self.ptr, r.ptr)
638639

639-
def intersects(self, other: Cpv | Dep):
640+
def intersects(self, other):
640641
"""Determine intersection between two Cpv or Dep objects.
641642
642643
Args:
@@ -658,6 +659,8 @@ cdef class Dep:
658659
return C.pkgcraft_dep_intersects(self.ptr, (<Dep>other).ptr)
659660
elif isinstance(other, Cpv):
660661
return C.pkgcraft_dep_intersects_cpv(self.ptr, (<Cpv>other).ptr)
662+
elif isinstance(other, Pkg):
663+
return C.pkgcraft_pkg_intersects_dep((<Pkg>other).ptr, self.ptr)
661664
raise TypeError(f"{other.__class__.__name__!r} unsupported type")
662665

663666
def __lt__(self, other):

tests/dep/test_cpv.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def test_cmp(self):
8989
with pytest.raises(TypeError):
9090
assert cpv2 > obj
9191

92-
def test_intersects(self):
92+
def test_intersects(self, fake_repo):
9393
cpv1 = Cpv("cat/pkg-1")
9494
cpv2 = Cpv("cat/pkg-2")
9595

@@ -103,9 +103,15 @@ def test_intersects(self):
103103
# unversioned Dep
104104
assert cpv1.intersects(Dep("cat/pkg"))
105105

106-
# invalid type
107-
with pytest.raises(TypeError):
108-
cpv1.intersects(object())
106+
# packages
107+
pkg = fake_repo.create_pkg("cat/pkg-1")
108+
assert cpv1.intersects(pkg)
109+
assert not cpv2.intersects(pkg)
110+
111+
# invalid types
112+
for obj in (object(), None):
113+
with pytest.raises(TypeError):
114+
cpv1.intersects(obj)
109115

110116
def test_hash(self):
111117
for d in TEST_DATA.toml("version.toml")["hashing"]:

tests/dep/test_pkg.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ def test_cmp(self):
324324
with pytest.raises(TypeError):
325325
op_func(obj, None)
326326

327-
def test_intersects(self):
327+
def test_intersects(self, fake_repo):
328328
for d in TEST_DATA.toml("dep.toml")["intersects"]:
329329
# test intersections between all pairs of distinct values
330330
for s1, s2 in permutations(d["vals"], 2):
@@ -340,8 +340,13 @@ def test_intersects(self):
340340
else:
341341
assert not obj1.intersects(obj2)
342342

343+
# packages
344+
pkg = fake_repo.create_pkg("cat/pkg-1")
345+
assert Dep("=cat/pkg-1").intersects(pkg)
346+
assert not Dep(">cat/pkg-1").intersects(pkg)
347+
343348
# invalid types
344-
for obj in [None, object()]:
349+
for obj in (object(), None):
345350
with pytest.raises(TypeError):
346351
Dep("cat/pkg").intersects(obj)
347352

0 commit comments

Comments
 (0)