Skip to content

Commit 0c29ad6

Browse files
committed
refactored and added some type annotations, less mypy errors
1 parent 0c11cc3 commit 0c29ad6

2 files changed

Lines changed: 40 additions & 25 deletions

File tree

test/examples.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def _remove_version_suffixes(path: pathlib.Path) -> t.Optional[pathlib.Path]:
6969

7070
KWARG_NAMES = ('major', 'minor', 'patch', 'pre_release', 'local')
7171

72-
INIT_CASES = {
72+
INIT_CASES: t.Dict[str, t.Tuple[tuple, dict]] = {
7373
'1': ((1,), {}),
7474
'1.0': ((1, 0), {}),
7575
'1.0.0': ((1, 0, 0), {}),
@@ -79,7 +79,7 @@ def _remove_version_suffixes(path: pathlib.Path) -> t.Optional[pathlib.Path]:
7979
'1.0.0.rc3+local': ((1, 0, 0, ('.', 'rc', 3), 'local'), {}),
8080
'1.0.0.rc4+local': ((1, 0, 0, ('.', 'rc', 4), ('local',)), {})}
8181

82-
BAD_INIT_CASES = {
82+
BAD_INIT_CASES: t.Dict[t.Tuple[tuple, tuple], t.Type[Exception]] = {
8383
(('spam',), ()): TypeError,
8484
((-1,), ()): ValueError,
8585
((1, 'ham'), ()): TypeError,

version_query/version.py

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,20 @@ class Version(collections.abc.Hashable): # pylint: disable = too-many-public-me
5151
@classmethod
5252
def _parse_release_str(cls, release: str) -> tuple:
5353
match = cls._pattern_release.fullmatch(release)
54-
major = match.group('major')
55-
major = int(major)
56-
minor = match.group('minor')
57-
if minor is not None:
58-
minor = int(minor)
59-
patch = match.group('patch')
60-
if patch is not None:
61-
patch = int(patch)
54+
assert match is not None
55+
major_match = match.group('major')
56+
assert major_match is not None
57+
major = int(major_match)
58+
minor_match = match.group('minor')
59+
if minor_match is not None:
60+
minor = int(minor_match)
61+
else:
62+
minor = None
63+
patch_match = match.group('patch')
64+
if patch_match is not None:
65+
patch = int(patch_match)
66+
else:
67+
patch = None
6268
return major, minor, patch
6369

6470
_re_pre_separator = rf'(?P<preseparator>{_re_sep})'
@@ -71,16 +77,20 @@ def _parse_release_str(cls, release: str) -> tuple:
7177
_pattern_pre_release_check = re.compile(rf'(?:{_re_pre_release_parts})+')
7278

7379
@classmethod
74-
def _parse_pre_release_str(cls, pre_release: str) -> tuple:
80+
def _parse_pre_release_str(cls, pre_release: str) -> t.Sequence[
81+
t.Tuple[t.Optional[str], t.Optional[str], t.Optional[int]]]:
7582
parts = cls._pattern_pre_release.findall(pre_release)
7683
_LOG.debug('parsed pre-release string %s into %s',
7784
repr(pre_release), parts)
7885
tuples = []
7986
for part in parts:
8087
match = cls._pattern_pre_release_part.fullmatch(part)
81-
pre_patch = match.group('prepatch')
82-
if pre_patch is not None:
83-
pre_patch = int(pre_patch)
88+
assert match is not None
89+
pre_patch_match = match.group('prepatch')
90+
if pre_patch_match is not None:
91+
pre_patch = int(pre_patch_match)
92+
else:
93+
pre_patch = None
8494
tuples.append((match.group('preseparator'), match.group('pretype'), pre_patch))
8595
return tuples
8696

@@ -92,6 +102,7 @@ def _parse_pre_release_str(cls, pre_release: str) -> tuple:
92102
@classmethod
93103
def _parse_local_str(cls, local: str) -> tuple:
94104
match = cls._pattern_local.fullmatch(local)
105+
assert match is not None
95106
return tuple([_ for _ in match.groups() if _ is not None])
96107

97108
_re_release = r'(?P<release>{n}(?:\.{n})?(?:\.{n})?)'.format(n=_re_number)
@@ -105,7 +116,7 @@ def _parse_local_str(cls, local: str) -> tuple:
105116
@classmethod
106117
def from_str(cls, version_str: str):
107118
"""Create version from string."""
108-
match = cls._pattern_version.fullmatch(version_str) # type: t.Match[str]
119+
match = cls._pattern_version.fullmatch(version_str) # type: t.Optional[t.Match[str]]
109120
if match is None:
110121
raise ValueError('version string {} is invalid'.format(repr(version_str)))
111122
_LOG.debug('version_query parsed version string %s into %s: %s %s',
@@ -137,8 +148,10 @@ def from_py_version(cls, py_version: packaging.version.Version):
137148
ver = py_version._version
138149
major, minor, patch = [ver.release[i] if len(ver.release) > i
139150
else None for i in range(3)]
140-
pre_release = None
151+
pre_release: t.Optional[t.Sequence[
152+
t.Tuple[t.Optional[str], t.Optional[str], t.Optional[int]]]] = None
141153
local = None
154+
pre_ver: t.Optional[t.Tuple[None, int]]
142155
if len(ver.release) == 4:
143156
pre_ver = (None, ver.release[3])
144157
elif len(ver.release) > 4:
@@ -155,8 +168,8 @@ def from_py_version(cls, py_version: packaging.version.Version):
155168
elif ver.pre:
156169
pre_ver = ver.pre
157170
if pre_ver:
158-
pre_release = [('.',) + tuple(pre_ver[i] if pre_ver and len(pre_ver) > i else None
159-
for i in range(2))]
171+
pre_release = [('.', pre_ver[0] if len(pre_ver) > 0 else None,
172+
pre_ver[1] if len(pre_ver) > 1 else None)]
160173
if ver.local:
161174
local = tuple(itertools.chain.from_iterable(
162175
(dot, str(_)) for dot, _ in zip('.' * len(ver.local), ver.local)))[1:]
@@ -190,9 +203,9 @@ def __init__(
190203
pre_release: t.Sequence[
191204
t.Tuple[t.Optional[str], t.Optional[str], t.Optional[int]]] = None,
192205
local: t.Union[str, tuple] = None):
193-
self._major = None
194-
self._minor = None
195-
self._patch = None
206+
self._major: t.Optional[int] = None
207+
self._minor: t.Optional[int] = None
208+
self._patch: t.Optional[int] = None
196209
self._pre_release = None
197210
self._local = None
198211

@@ -225,8 +238,9 @@ def __init__(
225238
local = (local,)
226239
self.local = local
227240

228-
def _get_pre_release_from_args(self, args) -> t.Tuple[list, int]:
229-
pre_release = []
241+
def _get_pre_release_from_args(self, args) -> t.Tuple[
242+
t.Sequence[t.Tuple[t.Optional[str], t.Optional[str], t.Optional[int]]], int]:
243+
pre_release: t.List[t.Tuple[t.Optional[str], t.Optional[str], t.Optional[int]]] = []
230244
consumed_args = 0
231245
if args and isinstance(args[0], tuple):
232246
for i, arg in enumerate(args):
@@ -242,7 +256,7 @@ def _get_pre_release_from_args(self, args) -> t.Tuple[list, int]:
242256
' must be a 3-tuple'
243257
.format(arg, i, args, repr(self)))
244258
else:
245-
accumulated = []
259+
accumulated: t.List[t.Union[int, str]] = []
246260
for i, arg in enumerate(args):
247261
if not accumulated:
248262
if arg in (None, '.', '-'):
@@ -261,6 +275,7 @@ def _get_pre_release_from_args(self, args) -> t.Tuple[list, int]:
261275

262276
@property
263277
def release(self) -> t.Tuple[int, t.Optional[int], t.Optional[int]]:
278+
assert self._major is not None
264279
return self._major, self._minor, self._patch
265280

266281
@release.setter
@@ -301,7 +316,7 @@ def release(self, release: t.Tuple[int, t.Optional[int], t.Optional[int]]):
301316

302317
@property
303318
def pre_release(self) -> t.Optional[
304-
t.List[t.Tuple[t.Optional[str], t.Optional[str], t.Optional[int]]]]:
319+
t.Sequence[t.Tuple[t.Optional[str], t.Optional[str], t.Optional[int]]]]:
305320
"""Pre-release version component."""
306321
if self._pre_release is None:
307322
return None

0 commit comments

Comments
 (0)