Skip to content

Commit 5e96b5b

Browse files
committed
repo: return list of pkgs for multiple __getitem__() matches
1 parent 8147648 commit 5e96b5b

4 files changed

Lines changed: 21 additions & 17 deletions

File tree

src/pkgcraft/repo/base.pyx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,11 @@ cdef class Repo:
106106
return bool(next(self.iter(obj), None))
107107

108108
def __getitem__(self, object obj not None):
109-
try:
110-
return next(self.iter(obj))
111-
except StopIteration:
112-
raise KeyError(obj)
109+
if pkgs := list(self.iter(obj)):
110+
if len(pkgs) > 1:
111+
return pkgs
112+
return pkgs[0]
113+
raise KeyError(obj)
113114

114115
def __iter__(self):
115116
return _Iter.create(self)

src/pkgcraft/repo/set.pyx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,12 @@ cdef class RepoSet:
5252
if repo := next(matching_repos, None):
5353
return repo
5454

55-
# try to return the first pkg match from a restriction
56-
try:
57-
return next(self.iter(key))
58-
except StopIteration:
59-
raise KeyError(key)
55+
# try to return the pkgs matching a restriction
56+
if pkgs := list(self.iter(key)):
57+
if len(pkgs) > 1:
58+
return pkgs
59+
return pkgs[0]
60+
raise KeyError(key)
6061

6162
def iter(self, restrict=None):
6263
"""Iterate over a repo set's packages, optionally applying a restriction."""

tests/repo/base.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,15 @@ def test_contains_base(self, make_repo):
107107
assert obj in r1
108108

109109
def test_getitem_base(self, repo):
110-
pkg = repo.create_pkg("cat/pkg-1")
111-
assert pkg == repo["cat/pkg-1"]
112-
assert pkg == repo[Cpv("cat/pkg-1")]
113-
assert pkg == repo[Dep("=cat/pkg-1")]
114-
assert pkg == repo[Dep(">=cat/pkg-1")]
110+
pkg1 = repo.create_pkg("cat/pkg-1")
111+
pkg2 = repo.create_pkg("cat/pkg-2")
112+
assert pkg1 == repo["cat/pkg-1"]
113+
assert pkg1 == repo[Cpv("cat/pkg-1")]
114+
assert pkg1 == repo[Dep("=cat/pkg-1")]
115+
assert pkg2 == repo[Dep(">=cat/pkg-2")]
116+
assert [pkg1, pkg2] == repo["cat/pkg"]
115117

116-
for obj in ("cat/pkg-2", Cpv("cat/pkg-3"), Dep("<cat/pkg-1")):
118+
for obj in ("cat/pkg-3", Cpv("cat/pkg-3"), Dep("<cat/pkg-1")):
117119
with pytest.raises(KeyError):
118120
_ = repo[obj]
119121

tests/repo/test_set.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ def test_getitem(self, make_fake_repo):
167167
assert s[:] == s
168168
assert s[-1] == r2
169169
assert s["r2"] == r2
170-
assert s["cat/pkg"] == pkg
171-
assert s["cat/pkg-1"] == pkg
170+
assert s["cat/pkg"] == [pkg, pkg1, pkg2]
171+
assert s["cat/pkg-1"] == [pkg, pkg1]
172172
assert s["=cat/pkg-1::r2"] == pkg1
173173
assert s[Dep("=cat/pkg-1::r2")] == pkg1
174174
assert s["cat/pkg-2"] == pkg2

0 commit comments

Comments
 (0)