Skip to content

Commit c63389f

Browse files
committed
Address feedback on MR
This commit addresses feedback on MR by removing unecessary checks and addit __getitem__ method Signed-off-by: Rohan Devasthale <rdevasth@redhat.com>
1 parent 5380773 commit c63389f

3 files changed

Lines changed: 34 additions & 7 deletions

File tree

src/fromager/resolver.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,13 +1001,7 @@ def find_candidates(self, identifier: str) -> Candidates:
10011001
"""
10021002
candidates: list[Candidate] = []
10031003
for version in self.version_map.versions():
1004-
url = self.version_map._content[version]
1005-
if not isinstance(url, str):
1006-
logger.warning(
1007-
f"{identifier}: skipping version {version} with non-string URL: {type(url)}"
1008-
)
1009-
continue
1010-
1004+
url = self.version_map[version]
10111005
candidate = Candidate(
10121006
name=identifier,
10131007
version=version,

src/fromager/versionmap.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ def add(self, key: Version | str, value: typing.Any) -> None:
3030
key = Version(key)
3131
self._content[key] = value
3232

33+
def __getitem__(self, key: Version | str) -> typing.Any:
34+
"""Get the value associated with a version
35+
36+
String keys are converted to Version instances. Raises KeyError if the
37+
version is not found.
38+
"""
39+
if not isinstance(key, Version):
40+
key = Version(key)
41+
return self._content[key]
42+
3343
def versions(self) -> typing.Iterable[Version]:
3444
"""Return the known versions, sorted in descending order."""
3545
return reversed(sorted(self._content.keys()))

tests/test_versionmap.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,26 @@ def test_no_match() -> None:
100100
m.lookup(Requirement("pkg"), Requirement("pkg<1.0"))
101101
with pytest.raises(ValueError):
102102
m.lookup(Requirement("pkg>1.0"), Requirement("pkg<1.0"))
103+
104+
105+
def test_getitem() -> None:
106+
m = VersionMap(
107+
{
108+
"1.2": "value for 1.2",
109+
Version("1.3"): "value for 1.3",
110+
"1.0": "value for 1.0",
111+
}
112+
)
113+
# Access by Version object
114+
assert m[Version("1.2")] == "value for 1.2"
115+
assert m[Version("1.3")] == "value for 1.3"
116+
117+
# Access by string (auto-converted to Version)
118+
assert m["1.2"] == "value for 1.2"
119+
assert m["1.0"] == "value for 1.0"
120+
121+
# Non-existent version raises KeyError
122+
with pytest.raises(KeyError):
123+
m[Version("2.0")]
124+
with pytest.raises(KeyError):
125+
m["2.0"]

0 commit comments

Comments
 (0)