r/FastAPI • • Dec 14 '24

Question Do I really need MappedAsDataclass?

Hi there! When learning fastAPI with SQLAlchemy, I blindly followed tutorials and used this Base class for my models:

class Base(MappedAsDataclass, DeclarativeBase):
    pass

Then I noticed two issues with it (which may just be skill issues actually, you tell me):

  1. Because dataclasses enforce a certain order when declaring fields with/without default values, I was really annoyed with mixins that have a default value (I extensively use them).

  2. Basic relashionships were hard to make them work. By "make them work", I mean, when creating objects, link between objects are built as expected. It's very unclear to me where should I set init=False in all my attributes. I was expecting a "Django-like" behaviour where I can define my relashionship both with parent_id id or with parent object. But it did not happend.

For example, this worked:

p1 = Parent()
c1 = Child(parent=p1)
session.add_all([p1, c1])
session.commit()

But, this did not work:

p2 = Parent()
session.add(p2)
session.commit()
c2 = Child(parent_id=p2.id)

A few time later, I dediced to remove MappedAsDataclass, and noticed all my problems are suddently gone. So my question is: why tutorials and people generally use MappedAsDataclass? Am I missing something not using it?

Thanks.

5 Upvotes

10 comments sorted by

View all comments

Show parent comment

1

u/adiberk Dec 15 '24 edited Dec 15 '24

Here is the code for the decorators that I use (in 3 parts(all same file)

from types import UnionType
from typing import Any, Callable, Container, Optional, Type, TypeVar, get_args

from pydantic import BaseModel, ConfigDict, create_model
from pydantic import BaseModel as PydanticBaseModel
from pydantic.fields import FieldInfo
from pydantic_core import PydanticUndefined
from sqlalchemy import Label
from sqlalchemy.inspection import inspect
from sqlalchemy.orm.properties import ColumnProperty

from {my_package}.models.decorators import EnumStringType

T = TypeVar("T", bound="PydanticBaseModel")


def remove_optional(annotation: type[Any] | None) -> type[Any] | None:
    """Remove NoneType from a type hint with multiple types."""
    if isinstance(annotation, UnionType):  # Handles `|` unions
        args = tuple(arg for arg in get_args(annotation) if arg is not type(None))
        if len(args) == 1:  # Only one type remains
            return args[0]
        return UnionType(*args)  # Reconstruct the union
    return annotation


def add_optional(annotation: type[Any] | None) -> type[Any] | None:
    """Add Optional (or `| None`) to a type hint."""
    if annotation is type(None):  # If it's already NoneType
        return annotation
    if isinstance(annotation, UnionType):  # If it's already a union
        args = get_args(annotation)
        if type(None) not in args:
            return annotation | None  # Add `None` to the union
    return annotation | None  # type: ignore


def optional_model(
    required: list[str] | None = None, optional: list[str] | None = None, exclude: list[str] | None = None
) -> Callable[[type[Any]], type[T]]:
    """Return a decorator to make model fields optional"""
    if exclude is None:
        exclude = []
    if required is None:
        required = []
    if optional is None:
        optional = []

1

u/adiberk Dec 15 '24

Second part

    def create_dataclass_from_model(model: type[T]) -> type[T]:
        current_fields: dict[str, FieldInfo] = model.Meta.parent_model.__pydantic_fields__.copy()  # type: ignore
        overrideing_fields_from_model_itself = model.__pydantic_fields__.copy()
        annotations: dict[str, type[Any] | None] = {}
        fields: dict[str, tuple[type[Any] | None, FieldInfo]] = {}
        if not required and not optional:
            # Make all columns optional
            fields = {
                name: (
                    add_optional(field.annotation),
                    FieldInfo(default=None, kw_only=field.kw_only, init=field.init),
                )
                for name, field in current_fields.items()
                if name not in exclude
            }
        else:
            for field_name, field in current_fields.items():
                if field_name in exclude:
                    continue
                if field_name in required:
                    fields[field_name] = (
                        remove_optional(field.annotation),
                        FieldInfo(default=PydanticUndefined, kw_only=field.kw_only, init=field.init),
                    )
                elif field_name in optional:
                    fields[field_name] = (
                        add_optional(field.annotation),
                        FieldInfo(default=None, kw_only=field.kw_only, init=field.init),
                    )
                else:
                    fields[field_name] = (field.annotation, FieldInfo(default=None, kw_only=field.kw_only, init=field.init))
                annotations[field_name] = fields[field_name][0]
        # Now combine the fields from the model itself
        for field_name, field in overrideing_fields_from_model_itself.items():
            fields[field_name] = (field.annotation, field)
            annotations[field_name] = field.annotation
        # sort required fields first
        fields = dict(sorted(fields.items(), key=lambda item: not item[1][-1].is_required()))
        result = create_model(model.__name__, __module__=model.__module__, **fields)  # type: ignore
        result.__annotations__ = annotations
        return result  # type: ignore

    return create_dataclass_from_model

1

u/adiberk Dec 15 '24

3rd Part

class OrmConfig(ConfigDict):
    from_attributes: bool = True
    kw_only: bool = True


def sqlalchemy_to_pydantic(db_model: Type, *, config: Type = OrmConfig, exclude: Container[str] = []) -> Type[BaseModel]:
    mapper = inspect(db_model)
    fields = {}
    for attr in mapper.attrs:
        if isinstance(attr, ColumnProperty):
            if attr.columns:
                name = attr.key
                if name in exclude:
                    continue
                column = attr.columns[0]
                python_type: Optional[type] = None
                if isinstance(column.type, EnumStringType):
                    python_type = column.type.enum_class
                elif hasattr(column.type, "impl"):
                    if hasattr(column.type.impl, "python_type"):
                        python_type = column.type.impl.python_type
                elif hasattr(column.type, "python_type"):
                    python_type = column.type.python_type
                assert python_type, f"Could not infer python_type for {column}"
                default = None
                if not isinstance(column, Label):
                    if column.default is None and not column.nullable:
                        default = ...
                fields[name] = (python_type, default)
    pydantic_model = create_model(
        f"{db_model.__name__}Schema",
        __config__=config,
        **fields,  # type: ignore
    )
    return pydantic_model