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

Support __doc__ argument in create_model() #7863

Merged
merged 4 commits into from Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions pydantic/main.py
Expand Up @@ -1320,6 +1320,7 @@ def create_model(
__model_name: str,
*,
__config__: ConfigDict | None = None,
__doc__: str | None = None,
__base__: None = None,
__module__: str = __name__,
__validators__: dict[str, AnyClassMethod] | None = None,
Expand All @@ -1334,6 +1335,7 @@ def create_model(
__model_name: str,
*,
__config__: ConfigDict | None = None,
__doc__: str | None = None,
__base__: type[Model] | tuple[type[Model], ...],
__module__: str = __name__,
__validators__: dict[str, AnyClassMethod] | None = None,
Expand All @@ -1347,6 +1349,7 @@ def create_model(
__model_name: str,
*,
__config__: ConfigDict | None = None,
__doc__: str | None = None,
__base__: type[Model] | tuple[type[Model], ...] | None = None,
__module__: str = __name__,
__validators__: dict[str, AnyClassMethod] | None = None,
Expand All @@ -1360,6 +1363,7 @@ def create_model(
Args:
__model_name: The name of the newly created model.
__config__: The configuration of the new model.
__doc__: The docstring of the new model.
__base__: The base class for the new model.
__module__: The name of the module that the model belongs to.
__validators__: A dictionary of methods that validate
Expand Down Expand Up @@ -1415,6 +1419,8 @@ def create_model(
fields[f_name] = f_value

namespace: dict[str, Any] = {'__annotations__': annotations, '__module__': __module__}
if __doc__:
namespace.update({'__doc__': __doc__})
if __validators__:
namespace.update(__validators__)
namespace.update(fields)
Expand Down
9 changes: 8 additions & 1 deletion tests/test_create_model.py
Expand Up @@ -254,15 +254,22 @@ def test_create_model_field_and_model_title():


def test_create_model_field_description():
m = create_model('M', a=(str, Field(description='descr')))
m = create_model('M', a=(str, Field(description='descr')), __doc__='Some doc')
assert m.model_json_schema() == {
'properties': {'a': {'description': 'descr', 'title': 'A', 'type': 'string'}},
'required': ['a'],
'title': 'M',
'type': 'object',
'description': 'Some doc',
}


def test_create_model_with_doc():
model = create_model('FooModel', foo=(str, ...), bar=(int, 123), __doc__='The Foo model')
assert model.__name__ == 'FooModel'
assert model.__doc__ == 'The Foo model'


sydney-runkle marked this conversation as resolved.
Show resolved Hide resolved
@pytest.mark.parametrize('base', [ModelPrivateAttr, object])
@pytest.mark.parametrize('use_annotation', [True, False])
def test_private_descriptors(base, use_annotation):
Expand Down