From 6bb9b748808838726b22a03b4443388f02e7663f Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Fri, 19 Jan 2018 11:07:56 -0800 Subject: [PATCH 1/5] Refine types for distutils.version Note: these type signatures were derived mainly by looking at the docstrings inside distutils.version. For reference: - py3 impl: https://github.com/python/cpython/blob/master/Lib/distutils/version.py - py2 impl: https://github.com/python/cpython/blob/2.7/Lib/distutils/version.py --- stdlib/2and3/distutils/version.pyi | 49 +++++++++++++++++------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/stdlib/2and3/distutils/version.pyi b/stdlib/2and3/distutils/version.pyi index d44a6f2ee3a9..fe407af3219c 100644 --- a/stdlib/2and3/distutils/version.pyi +++ b/stdlib/2and3/distutils/version.pyi @@ -1,31 +1,38 @@ import sys -from typing import Any +from typing import Any, Optional, TypeVar, Union, Pattern, Tuple + +T = TypeVar('T', bound='Version') class Version: - def __init__(self, vstring=None): ... + def __init__(self, vstring: Optional[str] = None) -> None: ... + def __repr__(self) -> str: ... if sys.version_info >= (3,): - def __eq__(self, other): ... - def __lt__(self, other): ... - def __le__(self, other): ... - def __gt__(self, other): ... - def __ge__(self, other): ... + def __eq__(self: T, other: Union[T, str]) -> bool: ... + def __lt__(self: T, other: Union[T, str]) -> bool: ... + def __le__(self: T, other: Union[T, str]) -> bool: ... + def __gt__(self: T, other: Union[T, str]) -> bool: ... + def __ge__(self: T, other: Union[T, str]) -> bool: ... + + # Methods part of the 'Version' interface, but not implemented + # within the class itself + + def parse(self: T, vstring: str) -> T: ... + def __str__(self) -> str: ... + if sys.version_info >= (3,): + def _cmp(self: T, other: Union[T, str]) -> bool: ... + else: + def __cmp__(self: T, other: Union[T, str]) -> bool: ... + class StrictVersion(Version): - version_re = ... # type: Any - version = ... # type: Any - prerelease = ... # type: Any - def parse(self, vstring): ... + version_re: Pattern[str] + version: Tuple[int, int, int] + prerelease: Optional[Tuple[str, int]] - if sys.version_info < (3,): - def __cmp__(self, other): ... class LooseVersion(Version): - component_re = ... # type: Any - def __init__(self, vstring=None): ... - vstring = ... # type: Any - version = ... # type: Any - def parse(self, vstring): ... - - if sys.version_info < (3,): - def __cmp__(self, other): ... + component_re: Pattern[str] + vstring: str + version: Tuple[Union[str, int], ...] + From 99a64a4fbc065794839c8c09649a04adcaea1c09 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Fri, 19 Jan 2018 11:17:13 -0800 Subject: [PATCH 2/5] Make __eq__ checks comply with 'object' superclass --- stdlib/2and3/distutils/version.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/2and3/distutils/version.pyi b/stdlib/2and3/distutils/version.pyi index fe407af3219c..659d87926245 100644 --- a/stdlib/2and3/distutils/version.pyi +++ b/stdlib/2and3/distutils/version.pyi @@ -8,7 +8,7 @@ class Version: def __repr__(self) -> str: ... if sys.version_info >= (3,): - def __eq__(self: T, other: Union[T, str]) -> bool: ... + def __eq__(self, other: object) -> bool: ... def __lt__(self: T, other: Union[T, str]) -> bool: ... def __le__(self: T, other: Union[T, str]) -> bool: ... def __gt__(self: T, other: Union[T, str]) -> bool: ... From 0925ab437ac8898ba55b722412f8b2d083b6be9c Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Fri, 19 Jan 2018 11:28:05 -0800 Subject: [PATCH 3/5] Fix flake8 errors --- stdlib/2and3/distutils/version.pyi | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/stdlib/2and3/distutils/version.pyi b/stdlib/2and3/distutils/version.pyi index 659d87926245..8381e4df1f5a 100644 --- a/stdlib/2and3/distutils/version.pyi +++ b/stdlib/2and3/distutils/version.pyi @@ -1,7 +1,7 @@ import sys from typing import Any, Optional, TypeVar, Union, Pattern, Tuple -T = TypeVar('T', bound='Version') +_T = TypeVar('_T', bound='Version') class Version: def __init__(self, vstring: Optional[str] = None) -> None: ... @@ -9,30 +9,27 @@ class Version: if sys.version_info >= (3,): def __eq__(self, other: object) -> bool: ... - def __lt__(self: T, other: Union[T, str]) -> bool: ... - def __le__(self: T, other: Union[T, str]) -> bool: ... - def __gt__(self: T, other: Union[T, str]) -> bool: ... - def __ge__(self: T, other: Union[T, str]) -> bool: ... + def __lt__(self: _T, other: Union[_T, str]) -> bool: ... + def __le__(self: _T, other: Union[_T, str]) -> bool: ... + def __gt__(self: _T, other: Union[_T, str]) -> bool: ... + def __ge__(self: _T, other: Union[_T, str]) -> bool: ... # Methods part of the 'Version' interface, but not implemented # within the class itself - def parse(self: T, vstring: str) -> T: ... + def parse(self: _T, vstring: str) -> _T: ... def __str__(self) -> str: ... if sys.version_info >= (3,): - def _cmp(self: T, other: Union[T, str]) -> bool: ... + def _cmp(self: _T, other: Union[_T, str]) -> bool: ... else: - def __cmp__(self: T, other: Union[T, str]) -> bool: ... - + def __cmp__(self: _T, other: Union[_T, str]) -> bool: ... class StrictVersion(Version): version_re: Pattern[str] version: Tuple[int, int, int] prerelease: Optional[Tuple[str, int]] - class LooseVersion(Version): component_re: Pattern[str] vstring: str version: Tuple[Union[str, int], ...] - From b981e158957830affc720b24c786ceafe4968a51 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Fri, 19 Jan 2018 12:16:10 -0800 Subject: [PATCH 4/5] Respond to code review by adding 'abstractmethod' decorators --- stdlib/2and3/distutils/version.pyi | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/stdlib/2and3/distutils/version.pyi b/stdlib/2and3/distutils/version.pyi index 8381e4df1f5a..a5c9e084b461 100644 --- a/stdlib/2and3/distutils/version.pyi +++ b/stdlib/2and3/distutils/version.pyi @@ -1,10 +1,10 @@ import sys +from abc import abstractmethod from typing import Any, Optional, TypeVar, Union, Pattern, Tuple _T = TypeVar('_T', bound='Version') class Version: - def __init__(self, vstring: Optional[str] = None) -> None: ... def __repr__(self) -> str: ... if sys.version_info >= (3,): @@ -14,14 +14,17 @@ class Version: def __gt__(self: _T, other: Union[_T, str]) -> bool: ... def __ge__(self: _T, other: Union[_T, str]) -> bool: ... - # Methods part of the 'Version' interface, but not implemented - # within the class itself - + @abstractmethod + def __init__(self, vstring: Optional[str] = None) -> None: ... + @abstractmethod def parse(self: _T, vstring: str) -> _T: ... + @abstractmethod def __str__(self) -> str: ... if sys.version_info >= (3,): + @abstractmethod def _cmp(self: _T, other: Union[_T, str]) -> bool: ... else: + @abstractmethod def __cmp__(self: _T, other: Union[_T, str]) -> bool: ... class StrictVersion(Version): @@ -29,7 +32,23 @@ class StrictVersion(Version): version: Tuple[int, int, int] prerelease: Optional[Tuple[str, int]] + def __init__(self, vstring: Optional[str] = None) -> None: ... + def parse(self: _T, vstring: str) -> _T: ... + def __str__(self) -> str: ... + if sys.version_info >= (3,): + def _cmp(self: _T, other: Union[_T, str]) -> bool: ... + else: + def __cmp__(self: _T, other: Union[_T, str]) -> bool: ... + class LooseVersion(Version): component_re: Pattern[str] vstring: str version: Tuple[Union[str, int], ...] + + def __init__(self, vstring: Optional[str] = None) -> None: ... + def parse(self: _T, vstring: str) -> _T: ... + def __str__(self) -> str: ... + if sys.version_info >= (3,): + def _cmp(self: _T, other: Union[_T, str]) -> bool: ... + else: + def __cmp__(self: _T, other: Union[_T, str]) -> bool: ... From 7ad9452f851b0b5eb0b8c7b2d5ebee6aff4a6809 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Fri, 19 Jan 2018 14:17:02 -0800 Subject: [PATCH 5/5] Change default of 'None' to '...' --- stdlib/2and3/distutils/version.pyi | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/stdlib/2and3/distutils/version.pyi b/stdlib/2and3/distutils/version.pyi index a5c9e084b461..956273542e6f 100644 --- a/stdlib/2and3/distutils/version.pyi +++ b/stdlib/2and3/distutils/version.pyi @@ -15,7 +15,7 @@ class Version: def __ge__(self: _T, other: Union[_T, str]) -> bool: ... @abstractmethod - def __init__(self, vstring: Optional[str] = None) -> None: ... + def __init__(self, vstring: Optional[str] = ...) -> None: ... @abstractmethod def parse(self: _T, vstring: str) -> _T: ... @abstractmethod @@ -32,7 +32,7 @@ class StrictVersion(Version): version: Tuple[int, int, int] prerelease: Optional[Tuple[str, int]] - def __init__(self, vstring: Optional[str] = None) -> None: ... + def __init__(self, vstring: Optional[str] = ...) -> None: ... def parse(self: _T, vstring: str) -> _T: ... def __str__(self) -> str: ... if sys.version_info >= (3,): @@ -45,7 +45,7 @@ class LooseVersion(Version): vstring: str version: Tuple[Union[str, int], ...] - def __init__(self, vstring: Optional[str] = None) -> None: ... + def __init__(self, vstring: Optional[str] = ...) -> None: ... def parse(self: _T, vstring: str) -> _T: ... def __str__(self) -> str: ... if sys.version_info >= (3,):