Skip to content

Commit

Permalink
Fix Optional when default is some builtins. Fix Python < 3.3 compat.
Browse files Browse the repository at this point in the history
  • Loading branch information
gschaffner committed Mar 5, 2022
1 parent 09c00ed commit 9213184
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
obtained from config-files, forms, external services or command-line
parsing, converted from JSON/YAML (or something else) to Python data-types."""

import inspect
import re

try:
Expand Down Expand Up @@ -274,10 +273,17 @@ def _priority(s):


def _invoke_with_optional_kwargs(f, **kwargs):
s = inspect.signature(f)
if len(s.parameters) == 0:
return f()
return f(**kwargs)
try:
return f(**kwargs)
except TypeError as e:
if (
e.args[0].startswith("{}() got an unexpected keyword argument".format(f.__name__)) # Python 2/3.
or e.args[0].startswith("{}() takes no arguments".format(f.__name__)) # Python 2 only.
):
return f()
else:
raise



class Schema(object):
Expand Down

0 comments on commit 9213184

Please sign in to comment.