Skip to content

Commit 4815626

Browse files
authored
Merge pull request #439 from waketzheng/master
Improve type hints to fix TODOs
2 parents 7a950aa + 9593ad0 commit 4815626

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/semver/version.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
Comparator = Callable[["Version", Comparable], bool]
3333

3434
T = TypeVar("T", bound="Version")
35+
T_cmp = TypeVar("T_cmp", tuple, str, int)
3536

3637

3738
def _comparator(operator: Comparator) -> Comparator:
@@ -53,7 +54,7 @@ def wrapper(self: "Version", other: Comparable) -> bool:
5354
return wrapper
5455

5556

56-
def _cmp(a, b): # TODO: type hints
57+
def _cmp(a: T_cmp, b: T_cmp) -> int:
5758
"""Return negative if a<b, zero if a==b, positive if a>b."""
5859
return (a > b) - (a < b)
5960

@@ -137,7 +138,7 @@ def __init__(
137138
self._build = None if build is None else str(build)
138139

139140
@classmethod
140-
def _nat_cmp(cls, a, b): # TODO: type hints
141+
def _nat_cmp(cls, a: Optional[str], b: Optional[str]) -> int:
141142
def cmp_prerelease_tag(a, b):
142143
if isinstance(a, int) and isinstance(b, int):
143144
return _cmp(a, b)
@@ -150,9 +151,10 @@ def cmp_prerelease_tag(a, b):
150151

151152
a, b = a or "", b or ""
152153
a_parts, b_parts = a.split("."), b.split(".")
153-
a_parts = [int(x) if re.match(r"^\d+$", x) else x for x in a_parts]
154-
b_parts = [int(x) if re.match(r"^\d+$", x) else x for x in b_parts]
155-
for sub_a, sub_b in zip(a_parts, b_parts):
154+
re_digits = re.compile(r"^\d+$")
155+
parts_a = [int(x) if re_digits.match(x) else x for x in a_parts]
156+
parts_b = [int(x) if re_digits.match(x) else x for x in b_parts]
157+
for sub_a, sub_b in zip(parts_a, parts_b):
156158
cmp_result = cmp_prerelease_tag(sub_a, sub_b)
157159
if cmp_result != 0:
158160
return cmp_result

0 commit comments

Comments
 (0)