Skip to content

Commit b7bc689

Browse files
committed
Fix linters
Signed-off-by: Jorge J. Perez <jjperez@ekumenlabs.com>
1 parent a241cb5 commit b7bc689

2 files changed

Lines changed: 39 additions & 26 deletions

File tree

pallet_patcher/solver.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
# Copyright 2025 Open Source Robotics Foundation, Inc.
2+
# Licensed under the Apache License, Version 2.0
3+
14
from packaging.version import Version
25
from packaging.specifiers import SpecifierSet
36

7+
48
# I heavily relied on AI to help with this one.
5-
# I added each case by iterating on the ones that throw me errors while debugging
6-
# We can add tests to make sure it handles most common Rust cases
7-
# (or at least the ones used right now
9+
# I added each case by iterating on the ones that throw me errors while
10+
# debugging. We can add tests to make sure it handles most common Rust
11+
# cases (or at least the ones used right now
812
def _parse_rust_specifier(spec_str: str) -> SpecifierSet:
913
clean_spec = spec_str.strip()
1014

@@ -46,7 +50,7 @@ def _parse_rust_specifier(spec_str: str) -> SpecifierSet:
4650
if major == 0:
4751
# Case B.1: Single digit (^0) -> Allow 0.x.x
4852
if len(parts) == 1:
49-
return SpecifierSet(f">={clean_spec},<1.0.0")
53+
return SpecifierSet(f">={clean_spec},<1.0.0")
5054

5155
minor = int(parts[1])
5256

@@ -65,7 +69,7 @@ def _parse_rust_specifier(spec_str: str) -> SpecifierSet:
6569
return SpecifierSet(f">={clean_spec},<0.1.0")
6670

6771
except ValueError:
68-
pass # Fallback to standard handling if parsing fails
72+
pass # Fallback to standard handling if parsing fails
6973

7074
# Fallback for standard Python specifiers (>=1.2, etc.)
7175
return SpecifierSet(clean_spec)
@@ -82,15 +86,15 @@ def solve_dependency(version_specifier, available_versions):
8286
:param available_versions: List of versions available.
8387
:type available_versions: str
8488
85-
:returns: matched version string, or None if available versions don't match the spec
89+
:returns: matched version string, or None if available ver don't match
8690
:rtype: dict
8791
"""
8892
spec = _parse_rust_specifier(version_specifier)
8993

9094
# We sort them first to prioritize higher versions for the packages
9195
sorted_versions = sorted(
92-
available_versions,
93-
key=lambda x: [int(part) for part in x.split('.')],
96+
available_versions,
97+
key=lambda x: [int(part) for part in x.split('.')],
9498
reverse=True
9599
)
96100

@@ -101,4 +105,4 @@ def solve_dependency(version_specifier, available_versions):
101105
if v in spec:
102106
return str(v)
103107

104-
return None
108+
return None

test/test_solver.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
# Copyright 2025 Open Source Robotics Foundation, Inc.
2+
# Licensed under the Apache License, Version 2.0
3+
14
import pytest
25
from pallet_patcher.solver import _parse_rust_specifier, solve_dependency
36

7+
48
# Test code generated with LLM help
5-
@pytest.mark.parametrize("rust_input, expected_matches, expected_non_matches", [
9+
@pytest.mark.parametrize("input, expected_matches, expected_non_matches", [
610
# 1. Explicit Equals (=)
711
("=1.2.3", ["1.2.3"], ["1.2.4", "1.2.2"]),
812
@@ -38,20 +42,22 @@
3842
("^0.0", ["0.0.0", "0.0.5"], ["0.1.0"]),
3943
("0.0", ["0.0.1"], ["0.1.0"]),
4044
])
41-
def test_rust_specifier_logic(rust_input, expected_matches, expected_non_matches):
45+
def test_rust_specifier_logic(input, expected_matches, expected_non_matches):
4246
"""
4347
Tests that the converted Python SpecifierSet correctly matches
4448
and excludes the versions dictated by Rust SemVer logic.
4549
"""
46-
spec_set = _parse_rust_specifier(rust_input)
50+
spec_set = _parse_rust_specifier(input)
4751

4852
for version in expected_matches:
4953
assert version in spec_set, \
50-
f"Rust spec '{rust_input}' should MATCH {version}, but Python spec {spec_set} did not."
54+
f"""Rust spec '{input}' should MATCH {version},
55+
but Python spec {spec_set} did not."""
5156

5257
for version in expected_non_matches:
5358
assert version not in spec_set, \
54-
f"Rust spec '{rust_input}' should NOT match {version}, but Python spec {spec_set} did."
59+
f"""Rust spec '{input}' should NOT match {version},
60+
but Python spec {spec_set} did."""
5561

5662

5763
@pytest.mark.parametrize("input_str, expected_str_repr", [
@@ -62,34 +68,36 @@ def test_rust_specifier_logic(rust_input, expected_matches, expected_non_matches
6268
])
6369
def test_standard_python_fallback(input_str, expected_str_repr):
6470
"""
65-
Tests that standard Python specifiers or other strings
71+
Tests that standard Python specifiers or other strings
6672
are passed through to SpecifierSet mostly unchanged.
6773
"""
6874
spec = _parse_rust_specifier(input_str)
69-
# Note: SpecifierSet normalization might change string spacing,
75+
# Note: SpecifierSet normalization might change string spacing,
7076
# but the logic checks if it parses without crashing.
7177
assert str(spec) == expected_str_repr or not input_str
7278

79+
7380
def test_invalid_version_strings():
7481
"""
75-
Ensure the function handles malformed strings gracefully
76-
(falling back to SpecifierSet which might raise its own error or handle it).
82+
Ensure the function handles malformed strings gracefully
83+
(falling back to SpecifierSet wich might raise or handle it).
7784
"""
7885
# This falls through to "Bare" logic, fails try/except, hits fallback
79-
# SpecifierSet("invalid") is technically valid in packaging but matches nothing usually
80-
# or raises InvalidSpecifier depending on version.
86+
# SpecifierSet("invalid") is technically valid in packaging but matches
87+
# nothing or raises InvalidSpecifier depending on version.
8188
# Here we just want to ensure your code doesn't crash internally.
8289
try:
8390
_parse_rust_specifier("invalid_string_with_char")
8491
except Exception as e:
85-
# It is acceptable if SpecifierSet raises, but your parsing logic shouldn't
92+
# It is acceptable if SpecifierSet raises, but your parsing logic
93+
# shouldn't
8694
assert "Invalid specifier" in str(e) or isinstance(e, ValueError)
8795

8896

8997
@pytest.mark.parametrize("spec, available, expected", [
9098
# 1. Basic Caret Priority
9199
# ^1.2.0 allows >=1.2.0, <2.0.0.
92-
# It should pick 1.9.9 (highest), ignore 2.0.0 (too high) and 1.1.0 (too low).
100+
# It should pick 1.9.9 (highest), ignore 2.0.0 (high) and 1.1.0 (low).
93101
("^1.2.0", ["1.1.0", "1.2.0", "1.2.5", "1.9.9", "2.0.0"], "1.9.9"),
94102
95103
# 2. Basic Tilde Priority
@@ -98,7 +106,7 @@ def test_invalid_version_strings():
98106
("~1.2.0", ["1.2.0", "1.2.5", "1.2.9", "1.3.0", "1.4.0"], "1.2.9"),
99107
100108
# 3. Rust "0.x.y" Semantics (Major 0 breaking changes)
101-
# ^0.2.0 allows >=0.2.0, <0.3.0.
109+
# ^0.2.0 allows >=0.2.0, <0.3.0.
102110
# Should ignore 0.3.0 even though it's higher.
103111
("^0.2.0", ["0.1.9", "0.2.0", "0.2.5", "0.3.0"], "0.2.5"),
104112
@@ -127,7 +135,8 @@ def test_solve_dependency_logic(spec, available, expected):
127135
"""
128136
result = solve_dependency(spec, available)
129137
assert result == expected, \
130-
f"For spec '{spec}' and versions {available}, expected '{expected}' but got '{result}'"
138+
f"""For spec '{spec}' and versions {available}, expected '{expected}'
139+
but got '{result}'"""
131140

132141

133142
def test_solve_dependency_sorting_correctness():
@@ -146,7 +155,7 @@ def test_solve_dependency_sorting_correctness():
146155

147156
def test_solve_dependency_invalid_input_handling():
148157
"""
149-
Test how the function handles non-integer version strings
158+
Test how the function handles non-integer version strings
150159
given the lambda sorting logic provided.
151160
"""
152161
# NOTE: The provided function uses `int(part)` which will crash on "beta".
@@ -159,4 +168,4 @@ def test_solve_dependency_invalid_input_handling():
159168
solve_dependency(spec, available)
160169

161170
# Confirm it failed where we expected (in the sort lambda)
162-
assert "invalid literal for int()" in str(excinfo.value)
171+
assert "invalid literal for int()" in str(excinfo.value)

0 commit comments

Comments
 (0)