Skip to content

Commit

Permalink
Support __doc__ argument in create_model() (#7863)
Browse files Browse the repository at this point in the history
Co-authored-by: sydney-runkle <sydneymarierunkle@gmail.com>
  • Loading branch information
chris-spann and sydney-runkle committed Oct 20, 2023
1 parent 37c3efd commit 6a4b329
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
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'


@pytest.mark.parametrize('base', [ModelPrivateAttr, object])
@pytest.mark.parametrize('use_annotation', [True, False])
def test_private_descriptors(base, use_annotation):
Expand Down

0 comments on commit 6a4b329

Please sign in to comment.