Skip to content

Commit b8a5864

Browse files
committed
Fixing type hints.
1 parent a897677 commit b8a5864

File tree

7 files changed

+38
-24
lines changed

7 files changed

+38
-24
lines changed

domdf_python_tools/bases.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def __eq__(self, other) -> bool:
9797

9898

9999
_T = TypeVar("_T")
100-
_S = TypeVar('_S')
100+
_S = TypeVar('_S', bound="UserList")
101101

102102

103103
@prettify_docstrings
@@ -197,7 +197,7 @@ def __setitem__(self, i: slice, o: Iterable[_T]) -> None:
197197
... # pragma: no cover
198198

199199
def __setitem__(self, i: Union[int, slice], item: Union[_T, Iterable[_T]]) -> None:
200-
self.data[i] = item
200+
self.data[i] = item # type: ignore
201201

202202
def __delitem__(self, i: Union[int, slice]):
203203
del self.data[i]

domdf_python_tools/delegators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def _f(f: Callable):
8181
if param in to_params:
8282
del to_params[param]
8383

84-
f.__signature__ = from_sig.replace(
84+
f.__signature__ = from_sig.replace( # type: ignore
8585
parameters=[*from_params.values(), *to_params.values()]
8686
) # type: ignore
8787
f.__annotations__ = {**to_annotations, **from_annotations}

tests/list_tests.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
from functools import cmp_to_key
1515

1616
# 3rd party
17+
from typing import List, no_type_check
18+
1719
import pytest
1820

1921
# this package
@@ -43,18 +45,20 @@ def test_init(self):
4345
self.assertNotEqual(id(a), id(b))
4446
self.assertEqual(a, b)
4547

48+
@no_type_check
4649
def test_getitem_error(self):
4750
a = []
4851
with pytest.raises(TypeError, match="list indices must be integers or slices"):
4952
a['a']
5053

54+
@no_type_check
5155
def test_setitem_error(self):
5256
a = []
5357
with pytest.raises(TypeError, match="list indices must be integers or slices"):
5458
a['a'] = "python"
5559

5660
def test_repr(self):
57-
l0 = []
61+
l0: List = []
5862
l2 = [0, 1, 2]
5963
a0 = self.type2test(l0)
6064
a2 = self.type2test(l2)

tests/seq_tests.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
from itertools import chain
1515

1616
# 3rd party
17+
from typing import List
18+
1719
import pytest
1820

1921
# this package
@@ -173,7 +175,7 @@ def __iter__(self):
173175

174176
class CommonTest:
175177
# The type to be tested
176-
type2test = None
178+
type2test: type
177179

178180
def assertEqual(self, left, right):
179181
assert left == right
@@ -186,7 +188,7 @@ def assertRaises(self, what, func, *args):
186188
func(*args)
187189

188190
def test_constructors(self):
189-
l0 = []
191+
l0: List = []
190192
l1 = [0]
191193
l2 = [0, 1]
192194

@@ -370,7 +372,7 @@ def test_addmul(self):
370372
self.assertEqual(u2 + u2 + u2, u2 * 3)
371373
self.assertEqual(u2 + u2 + u2, 3 * u2)
372374

373-
class subclass(self.type2test):
375+
class subclass(self.type2test): # type: ignore
374376
pass
375377

376378
u3 = subclass([0, 1])
@@ -399,7 +401,7 @@ def test_imul(self):
399401

400402
def test_getitemoverwriteiter(self):
401403
# Verify that __getitem__ overrides are not recognized by __iter__
402-
class T(self.type2test):
404+
class T(self.type2test): # type: ignore
403405

404406
def __getitem__(self, key):
405407
return str(key) + '!!!'

tests/test_namedlist.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
# stdlib
1212
import sys
13-
from typing import Callable
13+
from typing import Callable, Type, Union
1414

1515
# this package
1616
from domdf_python_tools.bases import NamedList, UserList, namedlist
@@ -19,7 +19,7 @@
1919

2020

2121
class TestNamedList(TestList):
22-
type2test = NamedList
22+
type2test: Type[NamedList] = NamedList
2323

2424
def test_add_specials(self):
2525
u = NamedList("spam")
@@ -58,7 +58,7 @@ class TestShoppingList(TestNamedList):
5858
Test a subclass of NamedList.
5959
"""
6060

61-
type2test = ShoppingList
61+
type2test: Type[ShoppingList] = ShoppingList
6262

6363
def test_repr(self):
6464
a0 = self.type2test([])
@@ -77,10 +77,11 @@ def test_repr(self):
7777

7878

7979
class NamedListTest:
80-
shopping_list: Callable
80+
shopping_list: Union[NamedList[str], Callable]
8181

8282
repr_out = "['egg and bacon', 'egg sausage and bacon', 'egg and spam', 'egg bacon and spam']"
8383
str_out = "ShoppingList['egg and bacon', 'egg sausage and bacon', 'egg and spam', 'egg bacon and spam']"
84+
cls_str: str
8485

8586
def test(self, capsys):
8687
assert isinstance(self.shopping_list, UserList)

tests/test_pretty_print.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
import types
1515

1616
# 3rd party
17+
from typing import no_type_check
18+
1719
import pytest
18-
from pytest_regressions.file_regression import FileRegressionFixture
20+
from pytest_regressions.file_regression import FileRegressionFixture # type: ignore
1921

2022
# this package
2123
from domdf_python_tools.pretty_print import FancyPrinter, simple_repr
@@ -140,6 +142,7 @@ def __hash__(self):
140142

141143
class TestFancyPrinter:
142144

145+
@no_type_check
143146
def test_init(self):
144147
FancyPrinter()
145148
FancyPrinter(indent=4, width=40, depth=5, stream=io.StringIO(), compact=True)
@@ -182,6 +185,7 @@ def test_basic(self, safe):
182185
assert not pp.isrecursive(safe), f"expected not isrecursive for {safe!r}"
183186
assert pp.isreadable(safe), f"expected isreadable for {safe!r}"
184187

188+
@no_type_check
185189
def test_knotted(self):
186190
a = list(range(100))
187191
b = list(range(200))
@@ -278,6 +282,7 @@ def test_knotted(self):
278282
# self.assertEqual(FancyPrinter().pformat(cont), expected)
279283
# self.assertEqual(FancyPrinter(width=1, indent=0).pformat(cont), expected)
280284

285+
@no_type_check
281286
def test_basic_line_wrap(self):
282287
# verify basic line-wrapping operation
283288
o = {
@@ -449,7 +454,7 @@ def test_sorted_dict(self):
449454
(): {}}) == r"{5: [[]], 'xy\tab\n': (3,), (): {}}"
450455

451456
def test_ordered_dict(self, file_regression: FileRegressionFixture):
452-
d = collections.OrderedDict()
457+
d: collections.OrderedDict = collections.OrderedDict()
453458
assert FancyPrinter(width=1).pformat(d) == 'OrderedDict()'
454459
d = collections.OrderedDict([])
455460
assert FancyPrinter(width=1).pformat(d) == 'OrderedDict()'
@@ -564,7 +569,7 @@ def test_sort_unorderable_values(self):
564569
assert FancyPrinter().pformat({Unorderable: 0, 1: 0}) == '{1: 0, ' + repr(Unorderable) + ': 0}'
565570

566571
# Issue 14998: TypeError on tuples with NoneTypes as dict keys.
567-
keys = [(1, ), (None, )]
572+
keys = [(1, ), (None, )] # type: ignore
568573
assert FancyPrinter().pformat(dict.fromkeys(keys, 0)) == '{%r: 0, %r: 0}' % tuple(sorted(keys, key=id))
569574

570575
def test_sort_orderable_and_unorderable_values(self):
@@ -694,7 +699,7 @@ def test_compact_width(self):
694699
number = 10
695700
o = [0] * number
696701
for i in range(levels - 1):
697-
o = [o]
702+
o = [o] # type: ignore
698703
for w in range(levels * 2 + 1, levels + 3 * number - 1):
699704
lines = FancyPrinter(width=w, compact=True).pformat(o, ).splitlines()
700705
maxwidth = max(map(len, lines))
@@ -811,20 +816,20 @@ def test_bytearray_wrap(self, value, width, file_regression: FileRegressionFixtu
811816
self.check_file_regression(FancyPrinter(width=width).pformat(value), file_regression)
812817

813818
def test_default_dict(self, file_regression: FileRegressionFixture):
814-
d = collections.defaultdict(int)
819+
d: collections.defaultdict = collections.defaultdict(int)
815820
assert FancyPrinter(width=1).pformat(d) == "defaultdict(<class 'int'>, {})"
816821
words = 'the quick brown fox jumped over a lazy dog'.split()
817822
d = collections.defaultdict(int, zip(words, itertools.count()))
818823
self.check_file_regression(FancyPrinter().pformat(d), file_regression)
819824

820825
def test_counter(self, file_regression: FileRegressionFixture):
821-
d = collections.Counter()
826+
d: collections.Counter = collections.Counter()
822827
assert FancyPrinter(width=1).pformat(d) == "Counter()"
823828
d = collections.Counter('senselessness')
824829
self.check_file_regression(FancyPrinter(width=40).pformat(d), file_regression)
825830

826831
def test_chainmap(self, file_regression: FileRegressionFixture):
827-
d = collections.ChainMap()
832+
d: collections.ChainMap = collections.ChainMap()
828833
assert FancyPrinter(width=1).pformat(d) == "ChainMap({})"
829834
words = 'the quick brown fox jumped over a lazy dog'.split()
830835
items = list(zip(words, itertools.count()))
@@ -838,7 +843,7 @@ def test_chainmap_nested(self, file_regression: FileRegressionFixture):
838843
self.check_file_regression(FancyPrinter().pformat(d), file_regression)
839844

840845
def test_deque(self):
841-
d = collections.deque()
846+
d: collections.deque = collections.deque()
842847
assert FancyPrinter(width=1).pformat(d) == "deque([])"
843848
d = collections.deque(maxlen=7)
844849
assert FancyPrinter(width=1).pformat(d) == "deque([], maxlen=7)"
@@ -873,14 +878,14 @@ def check_file_regression(self, data, file_regression: FileRegressionFixture):
873878
file_regression.check(data, extension=".txt", encoding="UTF-8")
874879

875880
def test_user_dict(self, file_regression: FileRegressionFixture):
876-
d = collections.UserDict()
881+
d: collections.UserDict = collections.UserDict()
877882
assert FancyPrinter(width=1).pformat(d) == "{}"
878883
words = 'the quick brown fox jumped over a lazy dog'.split()
879-
d = collections.UserDict(zip(words, itertools.count()))
884+
d = collections.UserDict(zip(words, itertools.count())) # type: ignore
880885
self.check_file_regression(FancyPrinter().pformat(d), file_regression)
881886

882887
def test_user_list(self, file_regression: FileRegressionFixture):
883-
d = collections.UserList()
888+
d: collections.UserList = collections.UserList()
884889
assert FancyPrinter(width=1).pformat(d) == "[]"
885890
words = 'the quick brown fox jumped over a lazy dog'.split()
886891
d = collections.UserList(zip(words, itertools.count()))

tests/test_userlist.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
# Check every path through every method of UserList
1010

1111
# this package
12+
from typing import Type
13+
1214
from domdf_python_tools.bases import UserList
1315
from domdf_python_tools.testing import not_pypy
1416
from tests import list_tests
@@ -59,7 +61,7 @@ def test_userlist_copy(self):
5961

6062

6163
class TestUserList(TestList):
62-
type2test = UserList
64+
type2test: Type[UserList] = UserList
6365

6466
def test_add_specials(self):
6567
u = UserList("spam")

0 commit comments

Comments
 (0)