Skip to content

Commit 4d511e0

Browse files
committed
more type annotations
1 parent 4cbccbe commit 4d511e0

File tree

4 files changed

+32
-26
lines changed

4 files changed

+32
-26
lines changed

dephell_specifier/git_specifier.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
class GitSpecifier:
5-
def __contains__(self, release):
5+
def __contains__(self, release: object) -> bool:
66
# check that this is GitRelease without imports
77
return hasattr(release, 'commit')
88

dephell_specifier/range_specifier.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import re
4+
from typing import Iterable
45

56
from packaging.specifiers import InvalidSpecifier
67
from packaging.version import Version, parse
@@ -16,8 +17,9 @@
1617

1718
class RangeSpecifier:
1819
_specs: set
20+
join_type: JoinTypes
1921

20-
def __init__(self, spec=None):
22+
def __init__(self, spec: object | None = None) -> None:
2123
if not spec:
2224
self._specs = set()
2325
self.join_type = JoinTypes.AND
@@ -42,7 +44,7 @@ def __init__(self, spec=None):
4244
return
4345

4446
@classmethod
45-
def _parse(cls, spec) -> set[Specifier]:
47+
def _parse(cls, spec: object) -> set[Specifier]:
4648
spec = cls._split_specifier(spec)
4749
result = set()
4850
for constr in spec:
@@ -74,7 +76,7 @@ def _parse(cls, spec) -> set[Specifier]:
7476
return result
7577

7678
@staticmethod
77-
def _split_specifier(spec) -> list[str]:
79+
def _split_specifier(spec: object) -> list[str]:
7880
if isinstance(spec, (list, tuple)):
7981
return list(spec)
8082
spec = str(spec)
@@ -169,7 +171,7 @@ def _parse_npm(constr: str) -> set[Specifier]:
169171
left += '.' + ''.join(map(str, version.pre))
170172
return {Specifier('>=' + left), Specifier('==' + right)}
171173

172-
def attach_time(self, releases) -> bool:
174+
def attach_time(self, releases: Iterable) -> bool:
173175
"""Attach time to all specifiers if possible
174176
"""
175177
ok = False
@@ -247,27 +249,27 @@ def python_compat(self) -> bool:
247249

248250
# magic methods
249251

250-
def __add__(self, other):
252+
def __add__(self, other: object) -> RangeSpecifier:
251253
new = self.copy()
252254
attached = new._attach(other)
253255
if attached:
254256
return new
255257
return NotImplemented
256258

257-
def __radd__(self, other):
259+
def __radd__(self, other: object) -> RangeSpecifier:
258260
new = self.copy()
259261
attached = new._attach(other)
260262
if attached:
261263
return new
262264
return NotImplemented
263265

264-
def __iadd__(self, other):
266+
def __iadd__(self, other: object) -> RangeSpecifier:
265267
attached = self._attach(other)
266268
if attached:
267269
return self
268270
return NotImplemented
269271

270-
def _attach(self, other) -> bool:
272+
def _attach(self, other: object) -> bool:
271273
if isinstance(other, GitSpecifier):
272274
self._specs.add(other)
273275
return True
@@ -316,34 +318,34 @@ def _attach(self, other) -> bool:
316318
self._specs = new_specs
317319
return True
318320

319-
def __contains__(self, release) -> bool:
321+
def __contains__(self, release: object) -> bool:
320322
rule = all if self.join_type == JoinTypes.AND else any
321323
return rule((release in specifier) for specifier in self._specs)
322324

323-
def __str__(self):
325+
def __str__(self) -> str:
324326
if not self._specs:
325327
return ''
326328
sep = ',' if self.join_type == JoinTypes.AND else ' || '
327329
return sep.join(sorted(map(str, self._specs)))
328330

329-
def __repr__(self):
331+
def __repr__(self) -> str:
330332
return '{name}({spec})'.format(
331333
name=self.__class__.__name__,
332334
spec=str(self),
333335
)
334336

335-
def __bool__(self):
337+
def __bool__(self) -> bool:
336338
return bool(self._specs)
337339

338-
def __lt__(self, other):
340+
def __lt__(self, other: object) -> bool:
339341
if not isinstance(other, type(self)):
340342
return False
341343
return str(self) < str(other)
342344

343-
def __eq__(self, other):
345+
def __eq__(self, other: object) -> bool:
344346
if not isinstance(other, type(self)):
345347
return False
346348
return str(self) == str(other)
347349

348-
def __hash__(self):
350+
def __hash__(self) -> int:
349351
return hash(str(self))

dephell_specifier/specifier.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
from __future__ import annotations
22

33
import operator
4-
from typing import Any, Callable
4+
from typing import Any, Callable, Iterable
55

66
from packaging import specifiers
77
from packaging.version import Version, parse
88

99
from .utils import cached_property
1010

1111

12-
OPERATIONS = {
12+
OPERATIONS: dict[str, Callable[[Any, Any], bool]] = {
1313
'==': operator.eq,
1414
'!=': operator.ne,
1515

@@ -20,7 +20,7 @@
2020
'>': operator.gt,
2121
}
2222

23-
OPERATORS_MERGE = {
23+
OPERATORS_MERGE: dict[frozenset[str], str | None] = {
2424
# different
2525
frozenset({'>', '<'}): None,
2626
frozenset({'>=', '<='}): '==',
@@ -47,21 +47,21 @@
4747
class Specifier:
4848
time = None
4949

50-
def __init__(self, constr):
50+
def __init__(self, constr: object) -> None:
5151
try:
5252
self._spec = specifiers.Specifier(str(constr), prereleases=True)
5353
except specifiers.InvalidSpecifier:
5454
raise specifiers.InvalidSpecifier(constr)
5555

56-
def attach_time(self, releases) -> bool:
56+
def attach_time(self, releases: Iterable) -> bool:
5757
for release in releases:
5858
if release.time.year != 1970:
5959
if str(release.version) == self._spec.version:
6060
self.time = release.time
6161
return True
6262
return False
6363

64-
def _check_version(self, version) -> bool:
64+
def _check_version(self, version: Version | str) -> bool:
6565
"""
6666
https://www.python.org/dev/peps/pep-0440/
6767
"""
@@ -119,7 +119,7 @@ def __repr__(self) -> str:
119119
spec=str(self._spec),
120120
)
121121

122-
def __add__(self, other):
122+
def __add__(self, other: object):
123123
if not isinstance(other, type(self)):
124124
return NotImplemented
125125

@@ -150,10 +150,14 @@ def __add__(self, other):
150150

151151
return NotImplemented
152152

153-
def __lt__(self, other) -> bool:
153+
def __lt__(self, other: object) -> bool:
154+
if not isinstance(other, type(self)):
155+
return NotImplemented
154156
return self.version < other.version
155157

156-
def __eq__(self, other) -> bool:
158+
def __eq__(self, other: object) -> bool:
159+
if not isinstance(other, type(self)):
160+
return NotImplemented
157161
return self.version == other.version and self.operator == other.operator
158162

159163
def __hash__(self) -> int:

dephell_specifier/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class cached_property: # noqa: N801
99
with an ordinary attribute. Deleting the attribute resets the property.
1010
"""
1111

12-
def __init__(self, func):
12+
def __init__(self, func) -> None:
1313
self.__doc__ = func.__doc__
1414
self.func = func
1515

0 commit comments

Comments
 (0)