Skip to content

Commit 434589b

Browse files
committed
Further fix lazy imports
1 parent 9ceaf54 commit 434589b

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

coconut/compiler/templates/header.py_template

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ def _coconut_lazy_module(name, on_import=None, attr=None):
88
state[1] = err
99
else:
1010
state[0] = mod
11+
loaded = _coconut.getattr(state[0], attr) if attr is not None else state[0]
12+
for cls in _coconut_type(loaded).__mro__:
13+
for attr_name, attr_val in _coconut.vars(cls).items():
14+
if attr_name.startswith("__") and attr_name.endswith("__") and _coconut.callable(attr_val):
15+
try:
16+
_coconut_type.__setattr__(lazy_obj, attr_name, make_lazy_method(attr_name))
17+
except _coconut.Exception:
18+
pass
1119
if on_import is not None:
1220
on_import(mod)
1321
if state[1] is not None:
@@ -30,16 +38,14 @@ def _coconut_lazy_module(name, on_import=None, attr=None):
3038
return _coconut.isinstance(inst, load())
3139
def __subclasscheck__(cls, sub):
3240
return _coconut.issubclass(sub, load())
33-
def _coconut_make_lazy_dunder(_coconut_n):
34-
def _coconut_lazy_dunder(cls, *args, **kwargs):
35-
return _coconut.getattr(load(), _coconut_n)(*args, **kwargs)
36-
return _coconut_lazy_dunder
37-
for _coconut_dunder in ("__call__", "__hash__", "__repr__", "__str__", "__format__", "__bytes__", "__bool__", "__nonzero__", "__int__", "__float__", "__complex__", "__index__", "__round__", "__eq__", "__ne__", "__lt__", "__le__", "__gt__", "__ge__", "__len__", "__contains__", "__iter__", "__next__", "__reversed__", "__getitem__", "__setitem__", "__delitem__", "__missing__", "__add__", "__radd__", "__iadd__", "__sub__", "__rsub__", "__isub__", "__mul__", "__rmul__", "__imul__", "__floordiv__", "__rfloordiv__", "__ifloordiv__", "__truediv__", "__rtruediv__", "__itruediv__", "__mod__", "__rmod__", "__imod__", "__pow__", "__rpow__", "__ipow__", "__matmul__", "__rmatmul__", "__imatmul__", "__divmod__", "__rdivmod__", "__neg__", "__pos__", "__abs__", "__invert__", "__and__", "__rand__", "__iand__", "__or__", "__ror__", "__ior__", "__xor__", "__rxor__", "__ixor__", "__lshift__", "__rlshift__", "__ilshift__", "__rshift__", "__rrshift__", "__irshift__", "__enter__", "__exit__", "__aiter__", "__anext__", "__await__", "__aenter__", "__aexit__", "__reduce__", "__reduce_ex__", "__copy__", "__deepcopy__"):
38-
_coconut_type.__setattr__(lazy_obj, _coconut_dunder, _coconut_make_lazy_dunder(_coconut_dunder))
39-
if attr is None:
40-
return lazy_obj(_coconut_py_str(name))
41-
else:
42-
return lazy_obj(_coconut_py_str(name), (), {{}})
41+
def make_lazy_method(method_name):
42+
def lazy_method(cls, *args, **kwargs):
43+
loaded = load()
44+
return _coconut.getattr(_coconut_type(loaded), method_name)(loaded, *args, **kwargs)
45+
return lazy_method
46+
for method_name in ("__repr__", "__call__", "__or__", "__ror__", "__reduce__", "__reduce_ex__", "__str__", "__hash__", "__format__", "__eq__", "__lt__", "__le__", "__gt__", "__ge__", "__contains__", "__iter__", "__reversed__", "__getitem__", "__setitem__", "__delitem__", "__missing__", "__add__", "__sub__", "__mul__", "__floordiv__", "__truediv__", "__mod__", "__pow__", "__matmul__", "__divmod__", "__neg__", "__pos__", "__abs__", "__invert__", "__and__", "__xor__", "__lshift__", "__rshift__", "__enter__", "__exit__", "__aiter__", "__await__", "__aenter__", "__aexit__", "__copy__", "__deepcopy__"):
47+
_coconut_type.__setattr__(lazy_obj, method_name, make_lazy_method(method_name))
48+
return lazy_obj(_coconut_py_str(name)) if attr is None else lazy_obj(_coconut_py_str(name), (), {empty_dict})
4349
@_coconut_wraps(_coconut_py_super)
4450
def _coconut_super(type=None, object_or_type=None):
4551
if type is None:

0 commit comments

Comments
 (0)