Skip to content

Commit 453e380

Browse files
committed
[latebinding] relaz requirements from generator to iterator
1 parent 3cb8cec commit 453e380

File tree

6 files changed

+25
-9
lines changed

6 files changed

+25
-9
lines changed

Diff for: Makefile

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
test: pytest mypy
1+
test: lint pytest mypy
2+
3+
4+
lint:
5+
ruff late test
26

37
pytest:
48
pytest -v

Diff for: README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ assert d.x is not c.x
116116
```
117117

118118

119-
### Working with generators
119+
### Working with iterators
120120

121-
**Late** allows passing a generator as a default argument value,
121+
**Late** allows passing an iterator as a default argument value,
122122
and it will provide the next value on each function call. The usefulness of
123123
this feature is unknown, but it's something that came up during the discussions
124124
about default arguments, so **Late** implements it.
@@ -144,7 +144,7 @@ about default arguments, so **Late** implements it.
144144
assert f() == 5
145145
```
146146

147-
This is a possible use for the generators feature. Imagine a function that requires a unique ID, and
147+
This is a possible use for the iterator feature. Imagine a function that requires a unique ID, and
148148
will generate one if none is provided. Without **Late** the declaration would be:
149149

150150
```python
@@ -191,7 +191,7 @@ For values of immutable types, ``__()`` will return the same value. For all othe
191191
will wrap the value in a special ``namedtuple(actual=value)``. At function invocation time, this it what happens:
192192

193193
* if the argument name is already in ``kwargs``, nothing is done
194-
* if the wrapped value is a generator, then ``next(actual)`` is used
194+
* if the wrapped value is an iterator, then ``next(actual)`` is used
195195
* if the wrapped value is a function, then ``actual()`` is used
196196
* in all other cases ``copy.deepcopy(actual)`` is used
197197

Diff for: late/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def late(o: _T | Iterator[_V] | Callable[[], _R]) -> _T | _V | _R:
3232
def _lateargs(func: Callable, **kwargs) -> dict[str, Any]:
3333

3434
def resolve_default(value):
35-
if inspect.isgenerator(value):
35+
if isinstance(value, Iterator):
3636
return next(value)
3737
if inspect.isfunction(value):
3838
return value()

Diff for: test/complex_test.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import inspect
21
from typing import Any
32

4-
from late import latebinding, __, _LateBound
3+
from late import latebinding, __
54

65

76
def test_recursion():

Diff for: test/generator_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from typing import Iterator
2-
from late import latebinding, __, _LateBound
2+
from late import latebinding, __
33

44

55
def test_generator():

Diff for: test/iterator_test.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from itertools import count
2+
3+
from late import latebinding, __
4+
5+
6+
def test_iterator():
7+
8+
@latebinding
9+
def f(x: int = __(count(start=7))):
10+
return x
11+
12+
assert f() == 7
13+
assert f() == 8

0 commit comments

Comments
 (0)