Skip to content

Commit

Permalink
refactor: rewrite the whole pydantic dataclass logic
Browse files Browse the repository at this point in the history
  • Loading branch information
PrettyWood committed Mar 27, 2021
1 parent 4ec6c52 commit feeee81
Show file tree
Hide file tree
Showing 8 changed files with 630 additions and 184 deletions.
390 changes: 212 additions & 178 deletions pydantic/dataclasses.py

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion pydantic/fields.py
Expand Up @@ -919,12 +919,13 @@ def is_complex(self) -> bool:
"""
Whether the field is "complex" eg. env variables should be parsed as JSON.
"""
from .dataclasses import PydanticDataclass
from .main import BaseModel # noqa: F811

return (
self.shape != SHAPE_SINGLETON
or isinstance(self.type_, PydanticDataclass)
or lenient_issubclass(self.type_, (BaseModel, list, set, frozenset, dict))
or hasattr(self.type_, '__pydantic_model__') # pydantic dataclass
)

def _type_display(self) -> PyObjectStr:
Expand Down
4 changes: 2 additions & 2 deletions pydantic/schema.py
Expand Up @@ -363,13 +363,13 @@ def get_flat_models_from_field(field: ModelField, known_models: TypeModelSet) ->
:param known_models: used to solve circular references
:return: a set with the model used in the declaration for this field, if any, and all its sub-models
"""
from .dataclasses import dataclass, is_builtin_dataclass
from .dataclasses import PydanticDataclass, dataclass
from .main import BaseModel # noqa: F811

flat_models: TypeModelSet = set()

# Handle dataclass-based models
if is_builtin_dataclass(field.type_):
if isinstance(field.type_, PydanticDataclass):
field.type_ = dataclass(field.type_)
field_type = field.type_
if lenient_issubclass(getattr(field_type, '__pydantic_model__', None), BaseModel):
Expand Down

0 comments on commit feeee81

Please sign in to comment.