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

Update type annotation for create_model function #2071

Merged
merged 2 commits into from Nov 29, 2020
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
1 change: 1 addition & 0 deletions changes/2071-uriyyo.md
@@ -0,0 +1 @@
Updated `create_model` return type annotation to return type which inherits from `__base__` argument.
9 changes: 5 additions & 4 deletions pydantic/main.py
Expand Up @@ -870,11 +870,11 @@ def create_model(
__model_name: str,
*,
__config__: Type[BaseConfig] = None,
__base__: Type[BaseModel] = None,
__base__: Type['Model'] = None,
__module__: str = __name__,
__validators__: Dict[str, classmethod] = None,
**field_definitions: Any,
) -> Type[BaseModel]:
) -> Type['Model']:
"""
Dynamically create a model.
:param __model_name: name of the created model
Expand All @@ -887,11 +887,12 @@ def create_model(
`foobar=(str, ...)` or `foobar=123`, or, for complex use-cases, in the format
`<name>=<FieldInfo>`, e.g. `foo=Field(default_factory=datetime.utcnow, alias='bar')`
"""
if __base__:

if __base__ is not None:
if __config__ is not None:
raise ConfigError('to avoid confusion __config__ and __base__ cannot be used together')
else:
__base__ = BaseModel
__base__ = cast(Type['Model'], BaseModel)

fields = {}
annotations = {}
Expand Down
8 changes: 7 additions & 1 deletion tests/mypy/modules/plugin_success.py
@@ -1,6 +1,6 @@
from typing import ClassVar, Optional, Union

from pydantic import BaseModel, Field
from pydantic import BaseModel, Field, create_model
from pydantic.dataclasses import dataclass


Expand Down Expand Up @@ -133,3 +133,9 @@ class Model(BaseModel):


_ = NestedModel.Model


DynamicModel = create_model('DynamicModel', __base__=Model)

dynamic_model = DynamicModel(x=1, y='y')
dynamic_model.x = 2