Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
apalala committed Nov 12, 2023
2 parents cdd9590 + 84e3246 commit a7a8246
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
24 changes: 23 additions & 1 deletion late/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,20 @@ class _LateBound(NamedTuple):
def late(o: _T | Iterator[_V]) -> _T | _V:
if isinstance(o, int | float | str | bool | bytes | bytearray | frozenset):
return o # type: ignore

actual: Any = None
if isinstance(o, list):
actual = [late(value) for value in o]
elif isinstance(o, dict):
actual = {name: late(value) for name, value in o.items()}
elif isinstance(o, set):
actual = {late(value) for value in o}
else:
return _LateBound(actual=o) # type: ignore
actual = o
return _LateBound(actual=actual) # type: ignore


__ = late


__ = late
Expand All @@ -30,6 +42,16 @@ def _lateargs(func: Callable, **kwargs) -> dict[str, Any]:
def resolve_default(value):
if inspect.isgenerator(value):
return next(value)
if inspect.isfunction(value):
return value()
if isinstance(value, _LateBound):
return resolve_default(value.actual)
if isinstance(value, list):
return [resolve_default(x) for x in value]
if isinstance(value, dict):
return {name: resolve_default(x) for name, x in value.items()}
if isinstance(value, set):
return {resolve_default(x) for x in value}
return copy.copy(value)

lateargs = {
Expand Down
30 changes: 30 additions & 0 deletions test/complex_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import inspect
from typing import Any

from late import latebinding, __, _LateBound


def test_recursion():
@latebinding
def f(x: list[list[Any]] = __([[]])) -> list[list[Any]]:
x[0].append(1)
return x

assert f() == [[1]]
assert f() == [[1]]


def test_function():
t = 0

def a() -> int:
nonlocal t
t += 1
return t

@latebinding
def f(x: int = __(a)) -> int: # type: ignore
return 2 * x

assert f() == 2
assert f() == 4

0 comments on commit a7a8246

Please sign in to comment.