sqlmesh.utils.pydantic API documentation

Edit on GitHub

# sqlmesh.utils.pydantic

View Source

     1import typing as t
     2
     3from pydantic import BaseModel
     4from sqlglot import exp
     5
     6DEFAULT_ARGS = {"exclude_none": True, "by_alias": True}
     7
     8
     9class PydanticModel(BaseModel):
    10    class Config:
    11        arbitrary_types_allowed = True
    12        extra = "forbid"
    13        json_encoders = {exp.Expression: lambda e: e.sql()}
    14        underscore_attrs_are_private = True
    15        smart_union = True
    16
    17    def dict(
    18        self,
    19        **kwargs: t.Any,
    20    ) -> t.Dict[str, t.Any]:
    21        return super().dict(**{**DEFAULT_ARGS, **kwargs})  # type: ignore
    22
    23    def json(
    24        self,
    25        **kwargs: t.Any,
    26    ) -> str:
    27        return super().json(**{**DEFAULT_ARGS, **kwargs})  # type: ignore
    28
    29    @classmethod
    30    def missing_required_fields(
    31        cls: t.Type["PydanticModel"], provided_fields: t.Set[str]
    32    ) -> t.Set[str]:
    33        return cls.required_fields() - provided_fields
    34
    35    @classmethod
    36    def extra_fields(cls: t.Type["PydanticModel"], provided_fields: t.Set[str]) -> t.Set[str]:
    37        return provided_fields - cls.all_fields()
    38
    39    @classmethod
    40    def all_fields(cls: t.Type["PydanticModel"]) -> t.Set[str]:
    41        return cls._fields()
    42
    43    @classmethod
    44    def required_fields(cls: t.Type["PydanticModel"]) -> t.Set[str]:
    45        return cls._fields(lambda field: field.required)
    46
    47    @classmethod
    48    def _fields(
    49        cls: t.Type["PydanticModel"],
    50        predicate: t.Callable[[t.Any], bool] = lambda _: True,
    51    ) -> t.Set[str]:
    52        return {
    53            field.alias if field.alias else field.name
    54            for field in cls.__fields__.values()
    55            if predicate(field)
    56        }

class PydanticModel(pydantic.main.BaseModel): View Source

    10class PydanticModel(BaseModel):
    11    class Config:
    12        arbitrary_types_allowed = True
    13        extra = "forbid"
    14        json_encoders = {exp.Expression: lambda e: e.sql()}
    15        underscore_attrs_are_private = True
    16        smart_union = True
    17
    18    def dict(
    19        self,
    20        **kwargs: t.Any,
    21    ) -> t.Dict[str, t.Any]:
    22        return super().dict(**{**DEFAULT_ARGS, **kwargs})  # type: ignore
    23
    24    def json(
    25        self,
    26        **kwargs: t.Any,
    27    ) -> str:
    28        return super().json(**{**DEFAULT_ARGS, **kwargs})  # type: ignore
    29
    30    @classmethod
    31    def missing_required_fields(
    32        cls: t.Type["PydanticModel"], provided_fields: t.Set[str]
    33    ) -> t.Set[str]:
    34        return cls.required_fields() - provided_fields
    35
    36    @classmethod
    37    def extra_fields(cls: t.Type["PydanticModel"], provided_fields: t.Set[str]) -> t.Set[str]:
    38        return provided_fields - cls.all_fields()
    39
    40    @classmethod
    41    def all_fields(cls: t.Type["PydanticModel"]) -> t.Set[str]:
    42        return cls._fields()
    43
    44    @classmethod
    45    def required_fields(cls: t.Type["PydanticModel"]) -> t.Set[str]:
    46        return cls._fields(lambda field: field.required)
    47
    48    @classmethod
    49    def _fields(
    50        cls: t.Type["PydanticModel"],
    51        predicate: t.Callable[[t.Any], bool] = lambda _: True,
    52    ) -> t.Set[str]:
    53        return {
    54            field.alias if field.alias else field.name
    55            for field in cls.__fields__.values()
    56            if predicate(field)
    57        }

def dict(self, **kwargs: Any) -> Dict[str, Any]: View Source

    18    def dict(
    19        self,
    20        **kwargs: t.Any,
    21    ) -> t.Dict[str, t.Any]:
    22        return super().dict(**{**DEFAULT_ARGS, **kwargs})  # type: ignore

Generate a dictionary representation of the model, optionally specifying which fields to include or exclude.

def json(self, **kwargs: Any) -> str: View Source

    24    def json(
    25        self,
    26        **kwargs: t.Any,
    27    ) -> str:
    28        return super().json(**{**DEFAULT_ARGS, **kwargs})  # type: ignore

Generate a JSON representation of the model, `include` and `exclude` arguments as per `dict()`.

`encoder` is an optional function to supply as `default` to json.dumps(), other arguments as per `json.dumps()`.

@classmethod
def missing_required_fields( cls: Type[sqlmesh.utils.pydantic.PydanticModel], provided_fields: Set[str]) -> Set[str]: View Source

    30    @classmethod
    31    def missing_required_fields(
    32        cls: t.Type["PydanticModel"], provided_fields: t.Set[str]
    33    ) -> t.Set[str]:
    34        return cls.required_fields() - provided_fields

@classmethod
def extra_fields( cls: Type[sqlmesh.utils.pydantic.PydanticModel], provided_fields: Set[str]) -> Set[str]: View Source

    36    @classmethod
    37    def extra_fields(cls: t.Type["PydanticModel"], provided_fields: t.Set[str]) -> t.Set[str]:
    38        return provided_fields - cls.all_fields()

@classmethod
def all_fields(cls: Type[sqlmesh.utils.pydantic.PydanticModel]) -> Set[str]: View Source

    40    @classmethod
    41    def all_fields(cls: t.Type["PydanticModel"]) -> t.Set[str]:
    42        return cls._fields()

@classmethod
def required_fields(cls: Type[sqlmesh.utils.pydantic.PydanticModel]) -> Set[str]: View Source

    44    @classmethod
    45    def required_fields(cls: t.Type["PydanticModel"]) -> t.Set[str]:
    46        return cls._fields(lambda field: field.required)

##### Inherited Members

pydantic.main.BaseModel
  BaseModel
  parse_obj
  parse_raw
  parse_file
  from_orm
  construct
  copy
  schema
  schema_json
  validate
  update_forward_refs

class PydanticModel.Config: View Source

    11    class Config:
    12        arbitrary_types_allowed = True
    13        extra = "forbid"
    14        json_encoders = {exp.Expression: lambda e: e.sql()}
    15        underscore_attrs_are_private = True
    16        smart_union = True

PydanticModel.Config()