Skip to content

Commit 84d89d5

Browse files
committed
repo: force existent key params for __getitem__()
1 parent 746db43 commit 84d89d5

4 files changed

Lines changed: 15 additions & 4 deletions

File tree

src/pkgcraft/repo/base.pyx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,12 @@ cdef class Repo:
105105
return C.pkgcraft_repo_contains_path(self.ptr, str(obj).encode())
106106
return bool(next(self.iter(obj), None))
107107

108-
def __getitem__(self, object obj not None):
109-
if pkgs := list(self.iter(obj)):
108+
def __getitem__(self, object key not None):
109+
if pkgs := list(self.iter(key)):
110110
if len(pkgs) > 1:
111111
return pkgs
112112
return pkgs[0]
113-
raise KeyError(obj)
113+
raise KeyError(key)
114114

115115
def __iter__(self):
116116
return _Iter.create(self)

src/pkgcraft/repo/set.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ cdef class RepoSet:
3939
def __iter__(self):
4040
return _Iter(self)
4141

42-
def __getitem__(self, key):
42+
def __getitem__(self, object key not None):
4343
if isinstance(key, int):
4444
# return a singular Repo for integers
4545
return self.repos[key]

tests/repo/base.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,16 @@ def test_getitem_base(self, repo):
115115
assert pkg2 == repo[Dep(">=cat/pkg-2")]
116116
assert [pkg1, pkg2] == repo["cat/pkg"]
117117

118+
# nonexistent matches
118119
for obj in ("cat/pkg-3", Cpv("cat/pkg-3"), Dep("<cat/pkg-1")):
119120
with pytest.raises(KeyError):
120121
_ = repo[obj]
121122

123+
# invalid key types
124+
for obj in (object(), None):
125+
with pytest.raises(TypeError):
126+
repo[obj]
127+
122128
def test_bool_and_len_base(self, repo):
123129
# empty repo
124130
assert not repo

tests/repo/test_set.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ def test_getitem(self, make_fake_repo):
171171
assert s[Dep("=cat/pkg-1::r2")] == pkg1
172172
assert s["cat/pkg-2"] == pkg2
173173

174+
# invalid key types
175+
for obj in (object(), None):
176+
with pytest.raises(TypeError):
177+
s[obj]
178+
174179
def test_bool_and_len(self, make_fake_repo):
175180
s = self.cls()
176181
assert not s

0 commit comments

Comments
 (0)