Skip to content

Commit ba54362

Browse files
committed
bullet-proof against UnknownArity
1 parent 7987b7c commit ba54362

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

unpythonic/let.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,17 +249,23 @@ def _let(mode, body, **bindings):
249249
env = _envcls()
250250
for k, v in bindings.items():
251251
if mode == "letrec" and callable(v):
252-
if not arity_includes(v, 1):
253-
raise ValueError("Arity mismatch; callable value must allow arity 1, to take in the environment.")
252+
try:
253+
if not arity_includes(v, 1):
254+
raise ValueError("Arity mismatch; callable value must allow arity 1, to take in the environment.")
255+
except UnknownArity: # well, we tried!
256+
pass
254257
v = v(env)
255258
env[k] = v
256259
# decorators need just the final env; else run body now
257260
env.finalize()
258261
if body:
259262
if not callable(body):
260263
raise TypeError("Expected callable body, got '{}' with value '{}'".format(type(body), body))
261-
if not arity_includes(body, 1):
262-
raise ValueError("Arity mismatch; body must allow arity 1, to take in the environment.")
264+
try:
265+
if not arity_includes(body, 1):
266+
raise ValueError("Arity mismatch; body must allow arity 1, to take in the environment.")
267+
except UnknownArity: # well, we tried!
268+
pass
263269
return env if body is None else body(env)
264270

265271
# decorator factory: almost as fun as macros?

unpythonic/lispylet.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,15 +231,21 @@ def _let(bindings, body, *, env=None, mode="let"):
231231
if body:
232232
if not callable(body):
233233
raise TypeError("Expected callable body, got '{}' with value '{}'".format(type(body), body))
234-
if not arity_includes(body, 1):
235-
raise ValueError("Arity mismatch; body must allow arity 1, to take in the environment.")
234+
try:
235+
if not arity_includes(body, 1):
236+
raise ValueError("Arity mismatch; body must allow arity 1, to take in the environment.")
237+
except UnknownArity: # well, we tried!
238+
pass
236239
# decorators need just the final env; else run body now
237240
return env if body is None else body(env)
238241

239242
(k, v), *more = bindings
240243
if mode == "letrec" and callable(v):
241-
if not arity_includes(v, 1):
242-
raise ValueError("Arity mismatch; callable value must allow arity 1, to take in the environment.")
244+
try:
245+
if not arity_includes(v, 1):
246+
raise ValueError("Arity mismatch; callable value must allow arity 1, to take in the environment.")
247+
except UnknownArity: # well, we tried!
248+
pass
243249
v = v(env)
244250
env[k] = v
245251
return _let(more, body, env=env, mode=mode) # FP loop (without TCO)

unpythonic/seq.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from unpythonic.env import env
1111

1212
# evil inspect dependency, used only to provide informative error messages.
13-
from unpythonic.arity import arity_includes
13+
from unpythonic.arity import arity_includes, UnknownArity
1414

1515
# sequence side effects in a lambda
1616
def begin(*vals):
@@ -310,8 +310,11 @@ def do(*items):
310310
e = env()
311311
def maybe_call(v):
312312
if callable(v):
313-
if not arity_includes(v, 1):
314-
raise ValueError("Arity mismatch; callable value must allow arity 1, to take in the environment.")
313+
try:
314+
if not arity_includes(v, 1):
315+
raise ValueError("Arity mismatch; callable value must allow arity 1, to take in the environment.")
316+
except UnknownArity: # well, we tried!
317+
pass
315318
return v(e)
316319
else:
317320
return v

0 commit comments

Comments
 (0)