-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Closed
Labels
Description
Feature Request
It would be great if pydantic supported callables as types.
Use-case: a user can specify a path to the class to be used dynamically or to provide a callable object directly (instead of the path as a string). We wanted to use pre validator to transform a string to the callable object.
from typing import Type, Callable, Any
from pydantic import BaseModel
def useless(x: Any) -> Any:
return x
class DemoModel(BaseModel):
t: Callable[..., Any]
print(DemoModel(t=useless))
...-> % python main.py
Traceback (most recent call last):
File "main.py", line 8, in <module>
class DemoModel(BaseModel):
File ".../.venv/lib/python3.7/site-packages/pydantic/main.py", line 124, in __new__
config=config,
File ".../.venv/lib/python3.7/site-packages/pydantic/fields.py", line 107, in infer
schema=schema,
File ".../.venv/lib/python3.7/site-packages/pydantic/fields.py", line 87, in __init__
self.prepare()
File ".../.venv/lib/python3.7/site-packages/pydantic/fields.py", line 135, in prepare
self._populate_sub_fields()
File ".../.venv/lib/python3.7/site-packages/pydantic/fields.py", line 240, in _populate_sub_fields
assert issubclass(origin, Mapping)
AssertionError