Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional fails when default is some builtins (e.g. dict, set, list) #275

Closed
gschaffner opened this issue Mar 5, 2022 · 1 comment
Closed

Comments

@gschaffner
Copy link

The documentation provides the example

schema/README.rst

Lines 271 to 277 in 09c00ed

default can also be a callable:
.. code:: python
>>> from schema import Schema, Optional
>>> Schema({Optional('data', default=dict): {}}).validate({}) == {'data': {}}
True

This does not work, however:

>>> from schema import Schema, Optional 
>>> Schema({Optional('data', default=dict): {}}).validate({})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/tmp/tmp.27EtQAmlOm/.venv/lib/python3.10/site-packages/schema.py", line 431, in validate
    new[default.key] = _invoke_with_optional_kwargs(default.default, **kwargs) if callable(default.default) else default.default
  File "/tmp/tmp.27EtQAmlOm/.venv/lib/python3.10/site-packages/schema.py", line 277, in _invoke_with_optional_kwargs
    s = inspect.signature(f)
  File "/usr/lib/python3.10/inspect.py", line 3247, in signature
    return Signature.from_callable(obj, follow_wrapped=follow_wrapped,
  File "/usr/lib/python3.10/inspect.py", line 2995, in from_callable
    return _signature_from_callable(obj, sigcls=cls,
  File "/usr/lib/python3.10/inspect.py", line 2529, in _signature_from_callable
    raise ValueError(
ValueError: no signature found for builtin type <class 'dict'>

I've tested and found the above error to occur with schema v0.7.5 on CPython 3.10.2, CPython 3.7.12, and PyPy3.8-7.3.7.

It looks like this regression was introduced in 958e1ad. Arguably this is due to a bug in Python, though.

Curiously: inspect.signature(list) works fine in CPython 3.6.15, but raises in 3.7.0.

For users wanting to work around this, some working options include e.g. Optional(..., default=lambda: dict()) and Optional(..., default=dict.__call__).

@gschaffner
Copy link
Author

Duplicate of #272.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant