Skip to content

Commit 8fc4ffa

Browse files
committed
thoroughly fixed all typing issues for both mypy and pyright and removed all type ignores
1 parent 7ef8b42 commit 8fc4ffa

File tree

18 files changed

+338
-150
lines changed

18 files changed

+338
-150
lines changed

_python_utils_tests/test_aio.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88

99
@pytest.mark.asyncio
10-
async def test_acount(monkeypatch: pytest.MonkeyPatch):
10+
async def test_acount(monkeypatch: pytest.MonkeyPatch) -> None:
1111
sleeps: types.List[float] = []
1212

13-
async def mock_sleep(delay: float):
13+
async def mock_sleep(delay: float) -> None:
1414
sleeps.append(delay)
1515

1616
monkeypatch.setattr(asyncio, 'sleep', mock_sleep)
@@ -23,13 +23,13 @@ async def mock_sleep(delay: float):
2323

2424

2525
@pytest.mark.asyncio
26-
async def test_acontainer():
27-
async def async_gen():
26+
async def test_acontainer() -> None:
27+
async def async_gen() -> types.AsyncIterable[int]:
2828
yield 1
2929
yield 2
3030
yield 3
3131

32-
async def empty_gen():
32+
async def empty_gen() -> types.AsyncIterable[int]:
3333
if False:
3434
yield 1
3535

@@ -52,13 +52,13 @@ async def empty_gen():
5252

5353

5454
@pytest.mark.asyncio
55-
async def test_adict():
56-
async def async_gen():
55+
async def test_adict() -> None:
56+
async def async_gen() -> types.AsyncIterable[types.Tuple[int, int]]:
5757
yield 1, 2
5858
yield 3, 4
5959
yield 5, 6
6060

61-
async def empty_gen():
61+
async def empty_gen() -> types.AsyncIterable[types.Tuple[int, int]]:
6262
if False:
6363
yield 1, 2
6464

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import typing
12
from unittest.mock import MagicMock
23

34
import pytest
45

56
from python_utils.decorators import sample, wraps_classmethod
67

8+
T = typing.TypeVar('T')
9+
710

811
@pytest.fixture
912
def random(monkeypatch: pytest.MonkeyPatch) -> MagicMock:
@@ -14,7 +17,7 @@ def random(monkeypatch: pytest.MonkeyPatch) -> MagicMock:
1417
return mock
1518

1619

17-
def test_sample_called(random: MagicMock):
20+
def test_sample_called(random: MagicMock) -> None:
1821
demo_function = MagicMock()
1922
decorated = sample(0.5)(demo_function)
2023
random.return_value = 0.4
@@ -28,7 +31,7 @@ def test_sample_called(random: MagicMock):
2831
assert demo_function.call_count == 3
2932

3033

31-
def test_sample_not_called(random: MagicMock):
34+
def test_sample_not_called(random: MagicMock) -> None:
3235
demo_function = MagicMock()
3336
decorated = sample(0.5)(demo_function)
3437
random.return_value = 0.5
@@ -40,31 +43,31 @@ def test_sample_not_called(random: MagicMock):
4043

4144
class SomeClass:
4245
@classmethod
43-
def some_classmethod(cls, arg): # type: ignore
44-
return arg # type: ignore
46+
def some_classmethod(cls, arg: T) -> T:
47+
return arg
4548

4649
@classmethod
4750
def some_annotated_classmethod(cls, arg: int) -> int:
4851
return arg
4952

5053

51-
def test_wraps_classmethod(): # type: ignore
54+
def test_wraps_classmethod() -> None:
5255
some_class = SomeClass()
53-
some_class.some_classmethod = MagicMock()
54-
wrapped_method = wraps_classmethod( # type: ignore
55-
SomeClass.some_classmethod # type: ignore
56-
)( # type: ignore
57-
some_class.some_classmethod # type: ignore
56+
some_class.some_classmethod = MagicMock() # type: ignore[method-assign]
57+
wrapped_method = wraps_classmethod(
58+
SomeClass.some_classmethod
59+
)(
60+
some_class.some_classmethod
5861
)
5962
wrapped_method(123)
60-
some_class.some_classmethod.assert_called_with(123) # type: ignore
63+
some_class.some_classmethod.assert_called_with(123)
6164

6265

63-
def test_wraps_annotated_classmethod(): # type: ignore
66+
def test_wraps_annotated_classmethod() -> None:
6467
some_class = SomeClass()
65-
some_class.some_annotated_classmethod = MagicMock()
68+
some_class.some_annotated_classmethod = MagicMock() # type: ignore[method-assign]
6669
wrapped_method = wraps_classmethod(SomeClass.some_annotated_classmethod)(
6770
some_class.some_annotated_classmethod
6871
)
69-
wrapped_method(123) # type: ignore
72+
wrapped_method(123)
7073
some_class.some_annotated_classmethod.assert_called_with(123)

_python_utils_tests/test_generators.py

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

88

99
@pytest.mark.asyncio
10-
async def test_abatcher():
10+
async def test_abatcher() -> None:
1111
async for batch in python_utils.abatcher(python_utils.acount(stop=9), 3):
1212
assert len(batch) == 3
1313

@@ -28,8 +28,8 @@ async def test_abatcher_timed() -> None:
2828

2929

3030
@pytest.mark.asyncio
31-
async def test_abatcher_timed_with_timeout():
32-
async def generator():
31+
async def test_abatcher_timed_with_timeout() -> None:
32+
async def generator() -> types.AsyncIterator[int]:
3333
# Test if the timeout is respected
3434
yield 0
3535
yield 1
@@ -57,7 +57,7 @@ async def generator():
5757
await batcher.__anext__()
5858

5959

60-
def test_batcher():
60+
def test_batcher() -> None:
6161
batch = []
6262
for batch in python_utils.batcher(range(9), 3):
6363
assert len(batch) == 3

_python_utils_tests/test_import.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
from python_utils import import_, types
22

33

4-
def test_import_globals_relative_import():
4+
def test_import_globals_relative_import() -> None:
55
for i in range(-1, 5):
66
relative_import(i)
77

88

9-
def relative_import(level: int):
9+
def relative_import(level: int) -> None:
1010
locals_: types.Dict[str, types.Any] = {}
1111
globals_ = {'__name__': 'python_utils.import_'}
1212
import_.import_global('.formatters', locals_=locals_, globals_=globals_)
1313
assert 'camel_to_underscore' in globals_
1414

1515

16-
def test_import_globals_without_inspection():
16+
def test_import_globals_without_inspection() -> None:
1717
locals_: types.Dict[str, types.Any] = {}
1818
globals_: types.Dict[str, types.Any] = {'__name__': __name__}
1919
import_.import_global(
@@ -22,7 +22,7 @@ def test_import_globals_without_inspection():
2222
assert 'camel_to_underscore' in globals_
2323

2424

25-
def test_import_globals_single_method():
25+
def test_import_globals_single_method() -> None:
2626
locals_: types.Dict[str, types.Any] = {}
2727
globals_: types.Dict[str, types.Any] = {'__name__': __name__}
2828
import_.import_global(
@@ -34,19 +34,19 @@ def test_import_globals_single_method():
3434
assert 'camel_to_underscore' in globals_
3535

3636

37-
def test_import_globals_with_inspection():
37+
def test_import_globals_with_inspection() -> None:
3838
import_.import_global('python_utils.formatters')
3939
assert 'camel_to_underscore' in globals()
4040

4141

42-
def test_import_globals_missing_module():
42+
def test_import_globals_missing_module() -> None:
4343
import_.import_global(
4444
'python_utils.spam', exceptions=ImportError, locals_=locals()
4545
)
4646
assert 'camel_to_underscore' in globals()
4747

4848

49-
def test_import_locals_missing_module():
49+
def test_import_locals_missing_module() -> None:
5050
import_.import_global(
5151
'python_utils.spam', exceptions=ImportError, globals_=globals()
5252
)

_python_utils_tests/test_logger.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
# mypy: disable-error-code=misc
12
import pytest
23

34
from python_utils.loguru import Logurud
45

56
loguru = pytest.importorskip('loguru')
67

78

8-
def test_logurud():
9+
def test_logurud() -> None:
910
class MyClass(Logurud):
1011
pass
1112

_python_utils_tests/test_python_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from python_utils import __about__
22

33

4-
def test_definitions():
4+
def test_definitions() -> None:
55
# The setup.py requires this so we better make sure they exist :)
66
assert __about__.__version__
77
assert __about__.__author__

_python_utils_tests/test_time.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ async def test_aio_timeout_generator(
3232
maximum_interval: float,
3333
iterable: types.AsyncIterable[types.Any],
3434
result: int,
35-
):
35+
) -> None:
3636
i = None
3737
async for i in python_utils.aio_timeout_generator(
3838
timeout, interval, iterable, maximum_interval=maximum_interval
@@ -46,15 +46,15 @@ async def test_aio_timeout_generator(
4646
'timeout,interval,interval_multiplier,maximum_interval,iterable,result',
4747
[
4848
(0.01, 0.006, 0.5, 0.01, 'abc', 'c'),
49-
(0.01, 0.006, 0.5, 0.01, itertools.count, 2), # type: ignore
49+
(0.01, 0.006, 0.5, 0.01, itertools.count, 2),
5050
(0.01, 0.006, 0.5, 0.01, itertools.count(), 2),
5151
(0.01, 0.006, 1.0, None, 'abc', 'c'),
5252
(
5353
timedelta(seconds=0.01),
5454
timedelta(seconds=0.006),
5555
2.0,
5656
timedelta(seconds=0.01),
57-
itertools.count, # type: ignore
57+
itertools.count,
5858
2,
5959
),
6060
],
@@ -70,7 +70,7 @@ def test_timeout_generator(
7070
types.Callable[..., types.Iterable[types.Any]],
7171
],
7272
result: int,
73-
):
73+
) -> None:
7474
i = None
7575
for i in python_utils.timeout_generator(
7676
timeout=timeout,
@@ -85,11 +85,11 @@ def test_timeout_generator(
8585

8686

8787
@pytest.mark.asyncio
88-
async def test_aio_generator_timeout_detector():
88+
async def test_aio_generator_timeout_detector() -> None:
8989
# Make pyright happy
9090
i = None
9191

92-
async def generator():
92+
async def generator() -> types.AsyncGenerator[int, None]:
9393
for i in range(10):
9494
await asyncio.sleep(i / 100.0)
9595
yield i
@@ -123,10 +123,10 @@ async def generator():
123123

124124

125125
@pytest.mark.asyncio
126-
async def test_aio_generator_timeout_detector_decorator_reraise():
126+
async def test_aio_generator_timeout_detector_decorator_reraise() -> None:
127127
# Test regular timeout with reraise
128128
@python_utils.aio_generator_timeout_detector_decorator(timeout=0.05)
129-
async def generator_timeout():
129+
async def generator_timeout() -> types.AsyncGenerator[int, None]:
130130
for i in range(10):
131131
await asyncio.sleep(i / 100.0)
132132
yield i
@@ -137,15 +137,15 @@ async def generator_timeout():
137137

138138

139139
@pytest.mark.asyncio
140-
async def test_aio_generator_timeout_detector_decorator_clean_exit():
140+
async def test_aio_generator_timeout_detector_decorator_clean_exit() -> None:
141141
# Make pyright happy
142142
i = None
143143

144144
# Test regular timeout with clean exit
145145
@python_utils.aio_generator_timeout_detector_decorator(
146146
timeout=0.05, on_timeout=None
147147
)
148-
async def generator_clean():
148+
async def generator_clean() -> types.AsyncGenerator[int, None]:
149149
for i in range(10):
150150
await asyncio.sleep(i / 100.0)
151151
yield i
@@ -157,10 +157,10 @@ async def generator_clean():
157157

158158

159159
@pytest.mark.asyncio
160-
async def test_aio_generator_timeout_detector_decorator_reraise_total():
160+
async def test_aio_generator_timeout_detector_decorator_reraise_total() -> None:
161161
# Test total timeout with reraise
162162
@python_utils.aio_generator_timeout_detector_decorator(total_timeout=0.1)
163-
async def generator_reraise():
163+
async def generator_reraise() -> types.AsyncGenerator[int, None]:
164164
for i in range(10):
165165
await asyncio.sleep(i / 100.0)
166166
yield i
@@ -171,15 +171,15 @@ async def generator_reraise():
171171

172172

173173
@pytest.mark.asyncio
174-
async def test_aio_generator_timeout_detector_decorator_clean_total():
174+
async def test_aio_generator_timeout_detector_decorator_clean_total() -> None:
175175
# Make pyright happy
176176
i = None
177177

178178
# Test total timeout with clean exit
179179
@python_utils.aio_generator_timeout_detector_decorator(
180180
total_timeout=0.1, on_timeout=None
181181
)
182-
async def generator_clean_total():
182+
async def generator_clean_total() -> types.AsyncGenerator[int, None]:
183183
for i in range(10):
184184
await asyncio.sleep(i / 100.0)
185185
yield i

pyproject.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,9 @@ strict = ['python_utils', '_python_utils_tests', 'setup.py']
1111
ignore = ['python_utils/terminal.py']
1212
pythonVersion = '3.9'
1313

14+
[tool.mypy]
15+
strict = true
16+
check_untyped_defs = true
17+
18+
[[tool.mypy.overrides]]
19+
module = '_python_utils_tests.*'

python_utils/aio.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ async def acontainer(
6262
types.AsyncIterable[_T],
6363
types.Callable[..., types.AsyncIterable[_T]],
6464
],
65-
container: types.Callable[[types.Iterable[_T]], types.Iterable[_T]] = list,
66-
) -> types.Iterable[_T]:
65+
container: types.Callable[[types.Iterable[_T]], types.Collection[_T]] =
66+
list,
67+
) -> types.Collection[_T]:
6768
"""
6869
Asyncio version of list()/set()/tuple()/etc() using an async for loop.
6970

0 commit comments

Comments
 (0)