Skip to content

Commit 0545c98

Browse files
committed
Test updates
1 parent 9163951 commit 0545c98

File tree

4 files changed

+43
-44
lines changed

4 files changed

+43
-44
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@ nosetests.xml
3636
.pydevproject
3737

3838
.venv
39+
.mypy_cache
3940
dist
4041

.travis.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ install:
88
script:
99
- python -mpytest ./tests
1010
- python -mpylint ./lazy_load
11-
- python -mmypy ./lazy_load --ignore-missing-imports
11+
- python -mmypy ./lazy_load --ignore-missing-imports --disallow-untyped-defs
1212
- find ./tests -name '*.py' | xargs python -mpylint --disable=missing-docstring,invalid-name,protected-access
13-
- find ./tests -name '*.py' | xargs python -mmypy --ignore-missing-imports
13+
- find ./tests -name '*.py' | xargs python -mmypy --ignore-missing-imports --disallow-untyped-defs
1414

lazy_load/_lazy_load.py

+13-14
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,37 @@
99

1010
from lazy_object_proxy import Proxy as _LazyProxy
1111

12-
_T1 = TypeVar('_T1')
13-
_T2 = TypeVar('_T2')
12+
_T = TypeVar('_T')
1413

1514
_LAZY_FUNC_ORIGINAL_FUNC_ATTRIBUTE = '__wrapped_non_lazy_function__'
1615

1716
def _is_lazy_object(obj: Any) -> bool:
1817
return obj is not None and isinstance(obj, _LazyProxy)
1918

2019
@overload
21-
def lazy(target: Callable[..., _T1], *args: str, **kwargs: str) -> _T1: pass # pylint: disable=missing-docstring,multiple-statements,unused-argument
20+
def lazy(target: Callable[..., _T], *args: Any, **kwargs: Any) -> _T: pass # pylint: disable=missing-docstring,multiple-statements,unused-argument
2221
@overload
23-
def lazy(target: _T1) -> _T1: pass # pylint: disable=missing-docstring,multiple-statements,unused-argument,function-redefined
22+
def lazy(target: _T) -> _T: pass # pylint: disable=missing-docstring,multiple-statements,unused-argument,function-redefined
2423

2524

26-
def lazy(target: Union[_T1, Callable[..., _T1]], *args: Any, **kwargs: Any) -> _T1: # pylint: disable=function-redefined
25+
def lazy(target: Union[_T, Callable[..., _T]], *args: Any, **kwargs: Any) -> _T: # pylint: disable=function-redefined
2726
"""
2827
Create a lazy loading object given a callable object and optional arguments.
2928
The created object will be generated by evaluating the given function
3029
as soon as it is used.
3130
3231
Arguments:
33-
target {Callable[..., _T1]} -- [The function to create the lazy loading object]
32+
target {Callable[..., _T]} -- [The function to create the lazy loading object]
3433
3534
Returns:
3635
_T -- [A proxy object for the lazy loading object.]
3736
"""
3837
if not callable(target):
3938
raise ValueError('Invalid target.')
4039
if args or kwargs or not _is_lazy_object(target):
41-
lazy_object = cast(_T1, _LazyProxy(lambda: target(*args, **kwargs)))
40+
lazy_object = cast(_T, _LazyProxy(lambda: target(*args, **kwargs)))
4241
else:
43-
lazy_object = cast(_T1, target)
42+
lazy_object = cast(_T, target)
4443
return lazy_object
4544

4645
lz: Callable = lazy
@@ -52,7 +51,7 @@ def _is_lazy_function(func: Callable) -> bool:
5251
return callable(func) and hasattr(func, _LAZY_FUNC_ORIGINAL_FUNC_ATTRIBUTE)
5352

5453
class _LazyFunc:
55-
def __call__(self, original_function: Callable[[_T1], _T2]) -> Callable[[_T1], _T2]:
54+
def __call__(self, original_function: Callable[..., _T]) -> Callable[..., _T]:
5655
"""
5756
Wrap a given function in a way that all its evaluations are lazy. This means
5857
a function call to the returned function will only result in an actual function
@@ -62,15 +61,15 @@ def __call__(self, original_function: Callable[[_T1], _T2]) -> Callable[[_T1], _
6261
The lazy evaluation works especially great for pure functions.
6362
6463
Arguments:
65-
original_function {Callable[[_T1], _T2]} -- [The function which should be wrapped.]
64+
original_function {Callable[..., _T]} -- [The function which should be wrapped.]
6665
6766
Returns:
68-
Callable[[_T1], _T2] -- [The lazy evaluated version of the given function]
67+
_T -- [The lazy evaluated version of the given function]
6968
"""
7069
if _is_lazy_function(original_function):
7170
return original_function
7271
@wraps(original_function)
73-
def _lazy_function(*args, **kwargs):
72+
def _lazy_function(*args: Any, **kwargs: Any) -> _T:
7473
return lazy(lambda: original_function(*args, **kwargs))
7574
setattr(_lazy_function, _LAZY_FUNC_ORIGINAL_FUNC_ATTRIBUTE, original_function)
7675
return _lazy_function
@@ -138,7 +137,7 @@ def lazy_class(cls: Type) -> Type:
138137
An alias for lazy_class.
139138
"""
140139

141-
def force_eval(obj: _T1) -> _T1:
140+
def force_eval(obj: _T) -> _T:
142141
"""
143142
Force the evaluation of a lazy object (Proxy). If a lazy function is given, the
144143
wrapped non-lazy version of the function is returned.
@@ -155,7 +154,7 @@ def force_eval(obj: _T1) -> _T1:
155154
return getattr(obj, _LAZY_FUNC_ORIGINAL_FUNC_ATTRIBUTE)
156155
return obj
157156

158-
fe: Callable[[_T1], _T1] = force_eval
157+
fe: Callable[[_T], _T] = force_eval
159158
"""
160159
An alias for force_eval.
161160
"""

tests/test_sample.py

+27-28
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
from lazy_object_proxy import Proxy
22
from lazy_load import lazy, lz, lazy_func, lf, , lazy_class, lc, force_eval, fe
33

4-
5-
def test_aliases():
4+
def test_aliases() -> None:
65
assert lazy is lz
76

87
assert lazy_func is lf
@@ -12,9 +11,9 @@ def test_aliases():
1211

1312
assert force_eval is fe
1413

15-
def test_lazy_simple_expression_evaluation():
14+
def test_lazy_simple_expression_evaluation() -> None:
1615
evaluated = False
17-
def add(x, y):
16+
def add(x: int, y: int) -> int:
1817
nonlocal evaluated
1918
evaluated = True
2019
return x + y
@@ -26,9 +25,9 @@ def add(x, y):
2625
assert evaluated
2726
assert isinstance(res, int)
2827

29-
def test_lazy_function_with_eagle_argument_evaluation():
28+
def test_lazy_function_with_eagle_argument_evaluation() -> None:
3029
evaluated = False
31-
def add(x, y):
30+
def add(x: int, y: int) -> int:
3231
nonlocal evaluated
3332
evaluated = True
3433
return x + y
@@ -40,9 +39,9 @@ def add(x, y):
4039
assert evaluated
4140
assert isinstance(res, int)
4241

43-
def test_lazy_function_evaluation():
42+
def test_lazy_function_evaluation() -> None:
4443
evaluated = False
45-
def add(x, y):
44+
def add(x: int, y: int) -> int:
4645
nonlocal evaluated
4746
evaluated = True
4847
return x + y
@@ -55,11 +54,11 @@ def add(x, y):
5554
assert evaluated
5655
assert isinstance(res, int)
5756

58-
def test_lazy_function_decorator():
57+
def test_lazy_function_decorator() -> None:
5958
evaluated = False
6059

6160
@lazy_func
62-
def lazy_add(x, y):
61+
def lazy_add(x: int, y: int) -> int:
6362
nonlocal evaluated
6463
evaluated = True
6564
return x + y
@@ -71,34 +70,34 @@ def lazy_add(x, y):
7170
assert evaluated
7271
assert isinstance(res, int)
7372

74-
def test_lazy_class():
73+
def test_lazy_class() -> None:
7574
evaluated = False
7675

7776
@lazy_class
7877
class LazyClass:
79-
def __init__(self, n):
78+
def __init__(self, n: int):
8079
self._n = n
8180

82-
def add(self, m) -> int:
81+
def add(self, m: int) -> int:
8382
nonlocal evaluated
8483
evaluated = True
8584
return self._n + m
8685

87-
def sub(self, m) -> None:
86+
def sub(self, m: int) -> None:
8887
# Return type is None: This function should not be lazy evaluated
8988
nonlocal evaluated
9089
evaluated = True
91-
return self._n - m
90+
return self._n - m # type: ignore
9291

9392
@lazy_func
94-
def mul(self, m) -> None:
93+
def mul(self, m: int) -> None:
9594
# Return type is None: This function should not be lazy evaluated;
9695
# BUT there is the decorater: it makes the function lazy
9796
nonlocal evaluated
9897
evaluated = True
99-
return self._n * m
98+
return self._n * m # type: ignore
10099

101-
def _div(self, m) -> int:
100+
def _div(self, m: int) -> int:
102101
# Non-public functions are never made lazy by lazy_class
103102
nonlocal evaluated
104103
evaluated = True
@@ -114,13 +113,13 @@ def _div(self, m) -> int:
114113
assert isinstance(res, int)
115114

116115
evaluated = False
117-
res = lazy_obj.sub(2)
116+
res = lazy_obj.sub(2) # type: ignore
118117
assert not isinstance(res, Proxy)
119118
assert evaluated
120119
assert res == -1
121120

122121
evaluated = False
123-
res = lazy_obj.mul(2)
122+
res = lazy_obj.mul(2) # type: ignore
124123
assert not evaluated
125124
assert isinstance(res, Proxy)
126125
assert res == 2
@@ -133,9 +132,9 @@ def _div(self, m) -> int:
133132
assert evaluated
134133
assert res == 1
135134

136-
def test_force_eval_on_objects():
135+
def test_force_eval_on_objects() -> None:
137136
evaluated = False
138-
def do_something(x, y):
137+
def do_something(x: int, y: int) -> int:
139138
nonlocal evaluated
140139
evaluated = True
141140
return x + y
@@ -145,19 +144,19 @@ def do_something(x, y):
145144
force_eval(res)
146145
assert evaluated
147146

148-
def test_force_eval_on_callables():
147+
def test_force_eval_on_callables() -> None:
149148
evaluated = False
150-
def do_something():
149+
def do_something() -> None:
151150
nonlocal evaluated
152151
evaluated = True
153152
do_something_lazy = [do_something]
154153
assert do_something_lazy is not do_something
155154
assert fe(do_something_lazy) is do_something
156155
assert not evaluated
157156

158-
def test_multiple_lazy_on_expression():
157+
def test_multiple_lazy_on_expression() -> None:
159158
evaluated = False
160-
def add(x, y):
159+
def add(x: int, y: int) -> int:
161160
nonlocal evaluated
162161
evaluated = True
163162
return x + y
@@ -178,9 +177,9 @@ def add(x, y):
178177
assert res == 3
179178
assert evaluated
180179

181-
def test_multiple_lazy_onfunction():
180+
def test_multiple_lazy_onfunction() -> None:
182181
evaluated = False
183-
def add(x, y):
182+
def add(x: int, y: int) -> int:
184183
nonlocal evaluated
185184
evaluated = True
186185
return x + y

0 commit comments

Comments
 (0)