From 35d3f9a501d148daece065ee530521084fa42981 Mon Sep 17 00:00:00 2001 From: Dennis Felsing Date: Mon, 27 Feb 2023 22:28:53 +0100 Subject: [PATCH 1/2] Fix return type of classmethod parse So that calling parse on a derived class will show correct type of derived class --- src/semver/version.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/semver/version.py b/src/semver/version.py index a121d3cf..f4043b16 100644 --- a/src/semver/version.py +++ b/src/semver/version.py @@ -14,6 +14,8 @@ cast, Callable, Collection, + Type, + TypeVar, ) from ._types import ( @@ -53,6 +55,9 @@ def _cmp(a, b): # TODO: type hints return (a > b) - (a < b) +T = TypeVar("T", bound="Version") + + class Version: """ A semver compatible version class. @@ -571,8 +576,8 @@ def match(self, match_expr: str) -> bool: @classmethod def parse( - cls, version: String, optional_minor_and_patch: bool = False - ) -> "Version": + cls: Type[T], version: String, optional_minor_and_patch: bool = False + ) -> T: """ Parse version string to a Version instance. From 38bc78f2b355f0cf0aed8fee633ef64e47794e63 Mon Sep 17 00:00:00 2001 From: Tom Schraitle Date: Tue, 28 Feb 2023 08:00:46 +0100 Subject: [PATCH 2/2] Move T definition some lines up --- src/semver/version.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/semver/version.py b/src/semver/version.py index f4043b16..bf949bb4 100644 --- a/src/semver/version.py +++ b/src/semver/version.py @@ -30,6 +30,8 @@ Comparable = Union["Version", Dict[str, VersionPart], Collection[VersionPart], str] Comparator = Callable[["Version", Comparable], bool] +T = TypeVar("T", bound="Version") + def _comparator(operator: Comparator) -> Comparator: """Wrap a Version binary op method in a type-check.""" @@ -55,9 +57,6 @@ def _cmp(a, b): # TODO: type hints return (a > b) - (a < b) -T = TypeVar("T", bound="Version") - - class Version: """ A semver compatible version class.