Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Improve type hints to fix TODOs #439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/semver/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
Comparator = Callable[["Version", Comparable], bool]

T = TypeVar("T", bound="Version")
T_cmp = TypeVar("T_cmp", tuple, str, int)


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


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

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

@classmethod
def _nat_cmp(cls, a, b): # TODO: type hints
def _nat_cmp(cls, a: Optional[str], b: Optional[str]) -> int:
def cmp_prerelease_tag(a, b):
if isinstance(a, int) and isinstance(b, int):
return _cmp(a, b)
Expand All @@ -150,9 +151,10 @@ def cmp_prerelease_tag(a, b):

a, b = a or "", b or ""
a_parts, b_parts = a.split("."), b.split(".")
a_parts = [int(x) if re.match(r"^\d+$", x) else x for x in a_parts]
b_parts = [int(x) if re.match(r"^\d+$", x) else x for x in b_parts]
for sub_a, sub_b in zip(a_parts, b_parts):
re_digits = re.compile(r"^\d+$")
parts_a = [int(x) if re_digits.match(x) else x for x in a_parts]
parts_b = [int(x) if re_digits.match(x) else x for x in b_parts]
for sub_a, sub_b in zip(parts_a, parts_b):
cmp_result = cmp_prerelease_tag(sub_a, sub_b)
if cmp_result != 0:
return cmp_result
Expand Down
Loading