Skip to content

Commit 9213184

Browse files
committed
Fix Optional when default is some builtins. Fix Python < 3.3 compat.
1 parent 09c00ed commit 9213184

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

schema.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
obtained from config-files, forms, external services or command-line
33
parsing, converted from JSON/YAML (or something else) to Python data-types."""
44

5-
import inspect
65
import re
76

87
try:
@@ -274,10 +273,17 @@ def _priority(s):
274273

275274

276275
def _invoke_with_optional_kwargs(f, **kwargs):
277-
s = inspect.signature(f)
278-
if len(s.parameters) == 0:
279-
return f()
280-
return f(**kwargs)
276+
try:
277+
return f(**kwargs)
278+
except TypeError as e:
279+
if (
280+
e.args[0].startswith("{}() got an unexpected keyword argument".format(f.__name__)) # Python 2/3.
281+
or e.args[0].startswith("{}() takes no arguments".format(f.__name__)) # Python 2 only.
282+
):
283+
return f()
284+
else:
285+
raise
286+
281287

282288

283289
class Schema(object):

0 commit comments

Comments
 (0)