Skip to content

Commit e24bff4

Browse files
committed
Improve type hints.
1 parent 2106601 commit e24bff4

File tree

5 files changed

+26
-22
lines changed

5 files changed

+26
-22
lines changed

domdf_python_tools/delegators.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
_C = TypeVar("_C", bound="Callable")
4949

5050

51-
def delegate_kwargs(to: Callable, *except_):
51+
def delegate_kwargs(to: Callable, *except_: str) -> Callable[[_C], _C]:
5252
r"""
5353
Decorator to replace ``**kwargs`` in function signatures with the
5454
parameter names from the delegated function.
@@ -61,7 +61,7 @@ def delegate_kwargs(to: Callable, *except_):
6161

6262
# TODO: return annotation
6363

64-
def _f(f: Callable):
64+
def _f(f: _C) -> _C:
6565
to_f, from_f = to, f
6666

6767
to_sig = inspect.signature(to_f)

domdf_python_tools/paths.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ def make_executable(filename: PathLike) -> None:
318318
"""
319319
Make the given file executable.
320320
321-
:param filename: Filename of the file to make executable
321+
:param filename:
322322
"""
323323

324324
if not isinstance(filename, pathlib.Path):
@@ -941,7 +941,7 @@ def traverse_to_file(base_directory: _P, *filename: PathLike, height: int = -1)
941941
raise FileNotFoundError(f"'{filename[0]!s}' not found in {base_directory}")
942942

943943

944-
def matchglob(filename: PathLike, pattern: str, matchcase: bool = True):
944+
def matchglob(filename: PathLike, pattern: str, matchcase: bool = True) -> bool:
945945
"""
946946
Given a filename and a glob pattern, return whether the filename matches the glob.
947947

domdf_python_tools/pretty_print.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
# stdlib
3737
import sys
3838
from io import StringIO
39-
from typing import IO, Any, Callable, Iterator, MutableMapping, Optional, Tuple
39+
from typing import IO, Any, Callable, Iterator, MutableMapping, Optional, Tuple, Type, TypeVar
4040

4141
try: # pragma: no cover
4242

@@ -55,6 +55,8 @@
5555

5656
__all__ = ["FancyPrinter", "simple_repr"]
5757

58+
_T = TypeVar("_T", bound=Type)
59+
5860

5961
class FancyPrinter(PrettyPrinter):
6062
"""
@@ -201,7 +203,7 @@ def __iter__(self) -> Iterator[Tuple[str, Any]]:
201203
def __len__(self) -> int:
202204
return len(self.attributes)
203205

204-
def __repr__(self):
206+
def __repr__(self) -> str:
205207
return f"Attributes{self.attributes}"
206208

207209

@@ -263,7 +265,7 @@ def simple_repr(*attributes: str, show_module: bool = False, **kwargs):
263265
:param \*\*kwargs: Keyword arguments passed on to :class:`pprint.PrettyPrinter`.
264266
"""
265267

266-
def deco(obj):
268+
def deco(obj: _T) -> _T:
267269

268270
def __repr__(self) -> str:
269271
if kwargs:
@@ -279,7 +281,7 @@ def __repr__(self) -> str:
279281
__repr__.__name__ = "__repr__"
280282
__repr__.__module__ = obj.__module__
281283
__repr__.__qualname__ = f"{obj.__module__}.__repr__"
282-
obj.__repr__ = __repr__
284+
obj.__repr__ = __repr__ # type: ignore
283285

284286
return obj
285287

domdf_python_tools/testing/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
]
7575

7676

77-
def is_docker():
77+
def is_docker() -> bool:
7878
"""
7979
Is this current environment running in docker?
8080

domdf_python_tools/versions.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@
2525

2626
# stdlib
2727
import re
28-
from typing import Dict, Generator, Iterable, Sequence, Tuple, Union
28+
from typing import Dict, Generator, Iterable, Sequence, Tuple, Type, TypeVar, Union
2929

3030
# 3rd party
3131
from typing_extensions import final
3232

3333
__all__ = ["Version"]
3434

35+
_V = TypeVar("_V", bound="Version")
36+
3537

3638
@final
3739
class Version(Tuple[int, int, int]):
@@ -84,8 +86,8 @@ def minor(self): # noqa: D102
8486
def patch(self): # noqa: D102
8587
return self[2]
8688

87-
def __new__(cls, major=0, minor=0, patch=0) -> "Version": # noqa: D102
88-
t: "Version" = super().__new__(cls, (int(major), int(minor), int(patch))) # type: ignore
89+
def __new__(cls: Type[_V], major=0, minor=0, patch=0) -> _V: # noqa: D102
90+
t: _V = super().__new__(cls, (int(major), int(minor), int(patch))) # type: ignore
8991

9092
return t
9193

@@ -129,7 +131,7 @@ def __eq__(self, other) -> bool:
129131
"""
130132
Returns whether this version is equal to the other version.
131133
132-
:type other: str, float, Version
134+
:type other: :class:`str`, :class:`float`, :class:`~.Version`
133135
"""
134136

135137
other = _prep_for_eq(other)
@@ -144,7 +146,7 @@ def __gt__(self, other) -> bool:
144146
"""
145147
Returns whether this version is greater than the other version.
146148
147-
:type other: str, float, Version
149+
:type other: :class:`str`, :class:`float`, :class:`~.Version`
148150
"""
149151

150152
other = _prep_for_eq(other)
@@ -158,7 +160,7 @@ def __lt__(self, other) -> bool:
158160
"""
159161
Returns whether this version is less than the other version.
160162
161-
:type other: str, float, Version
163+
:type other: :class:`str`, :class:`float`, :class:`~.Version`
162164
"""
163165

164166
other = _prep_for_eq(other)
@@ -172,7 +174,7 @@ def __ge__(self, other) -> bool:
172174
"""
173175
Returns whether this version is greater than or equal to the other version.
174176
175-
:type other: str, float, Version
177+
:type other: :class:`str`, :class:`float`, :class:`~.Version`
176178
"""
177179

178180
other = _prep_for_eq(other)
@@ -186,7 +188,7 @@ def __le__(self, other) -> bool:
186188
"""
187189
Returns whether this version is less than or equal to the other version.
188190
189-
:type other: str, float, Version
191+
:type other: :class:`str`, :class:`float`, :class:`~.Version`
190192
"""
191193

192194
other = _prep_for_eq(other)
@@ -197,7 +199,7 @@ def __le__(self, other) -> bool:
197199
return tuple(self)[:len(other)] <= other
198200

199201
@classmethod
200-
def from_str(cls, version_string: str) -> "Version":
202+
def from_str(cls: Type[_V], version_string: str) -> _V:
201203
"""
202204
Create a :class:`~.Version` from a :class:`str`.
203205
@@ -209,7 +211,7 @@ def from_str(cls, version_string: str) -> "Version":
209211
return cls(*_iter_string(version_string))
210212

211213
@classmethod
212-
def from_tuple(cls, version_tuple: Tuple[Union[str, int], ...]) -> "Version":
214+
def from_tuple(cls: Type[_V], version_tuple: Tuple[Union[str, int], ...]) -> _V:
213215
"""
214216
Create a :class:`~.Version` from a :class:`tuple`.
215217
@@ -226,7 +228,7 @@ def from_tuple(cls, version_tuple: Tuple[Union[str, int], ...]) -> "Version":
226228
return cls(*(int(x) for x in version_tuple[:3]))
227229

228230
@classmethod
229-
def from_float(cls, version_float: float) -> "Version":
231+
def from_float(cls: Type[_V], version_float: float) -> _V:
230232
"""
231233
Create a :class:`~.Version` from a :class:`float`.
232234
@@ -250,7 +252,7 @@ def _asdict(self) -> Dict[str, int]:
250252
"patch": self.patch,
251253
}
252254

253-
def _replace(self, **kwargs) -> "Version":
255+
def _replace(self: _V, **kwargs) -> _V:
254256
"""
255257
Return a new instance of the named tuple replacing specified fields with new values.
256258
@@ -262,7 +264,7 @@ def _replace(self, **kwargs) -> "Version":
262264
return self.__class__(**{**self._asdict(), **kwargs})
263265

264266
@classmethod
265-
def _make(cls, iterable: Iterable[Union[str, int]]) -> "Version":
267+
def _make(cls: Type[_V], iterable: Iterable[Union[str, int]]) -> _V:
266268
"""
267269
Class method that makes a new instance from an existing sequence or iterable.
268270

0 commit comments

Comments
 (0)