From 921318466ba64a9534e4041cf0c5cee7c91542b9 Mon Sep 17 00:00:00 2001 From: Ganden Schaffner Date: Sat, 5 Mar 2022 00:00:00 -0800 Subject: [PATCH] Fix `Optional` when `default` is some builtins. Fix Python < 3.3 compat. --- schema.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/schema.py b/schema.py index 37b0fb1..1de61eb 100644 --- a/schema.py +++ b/schema.py @@ -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: @@ -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):