bedrock.model.wrappers.calculated_type

 1from sqlalchemy.orm import column_property  # pragma: unit
 2
 3
 4def calculated_type(sa_type, *args, **kwargs):  # pragma: unit
 5    """
 6    Creates a `column_property` annotated with an explicit SQLAlchemy type for OpenAPI spec generation.
 7
 8    Use this instead of `column_property()` for calculated/computed columns whose type
 9    cannot be reliably inferred from the underlying SQL expression.
10
11    Accepts both SQLAlchemy type classes (`DateTime`) and instances (`DateTime(timezone=True)`).
12
13    Example:
14    ```python
15    from bedrock.model.wrappers.calculated_type import calculated_type
16
17    Officer.sia_licence_expiry = calculated_type(DateTime,
18        select(Evidence.expiry_date)
19        .where(Evidence.officer_uuid == Officer.uuid)
20        .correlate(Officer)
21        .scalar_subquery()
22    )
23    ```
24    See [`column_property`](https://docs.sqlalchemy.org/en/21/orm/mapped_sql_expr.html#using-column-property).
25
26    :param sa_type: A SQLAlchemy type class or instance (e.g. `DateTime`, `Integer`, `String(255)`).
27    :param args: Positional arguments forwarded to `column_property`.
28    :param kwargs: Keyword arguments forwarded to `column_property`.
29    """
30    prop = column_property(*args, **kwargs)
31    prop._calculated_type = sa_type() if isinstance(sa_type, type) else sa_type
32    return prop
def calculated_type(sa_type, *args, **kwargs):
 5def calculated_type(sa_type, *args, **kwargs):  # pragma: unit
 6    """
 7    Creates a `column_property` annotated with an explicit SQLAlchemy type for OpenAPI spec generation.
 8
 9    Use this instead of `column_property()` for calculated/computed columns whose type
10    cannot be reliably inferred from the underlying SQL expression.
11
12    Accepts both SQLAlchemy type classes (`DateTime`) and instances (`DateTime(timezone=True)`).
13
14    Example:
15    ```python
16    from bedrock.model.wrappers.calculated_type import calculated_type
17
18    Officer.sia_licence_expiry = calculated_type(DateTime,
19        select(Evidence.expiry_date)
20        .where(Evidence.officer_uuid == Officer.uuid)
21        .correlate(Officer)
22        .scalar_subquery()
23    )
24    ```
25    See [`column_property`](https://docs.sqlalchemy.org/en/21/orm/mapped_sql_expr.html#using-column-property).
26
27    :param sa_type: A SQLAlchemy type class or instance (e.g. `DateTime`, `Integer`, `String(255)`).
28    :param args: Positional arguments forwarded to `column_property`.
29    :param kwargs: Keyword arguments forwarded to `column_property`.
30    """
31    prop = column_property(*args, **kwargs)
32    prop._calculated_type = sa_type() if isinstance(sa_type, type) else sa_type
33    return prop

Creates a column_property annotated with an explicit SQLAlchemy type for OpenAPI spec generation.

Use this instead of column_property() for calculated/computed columns whose type cannot be reliably inferred from the underlying SQL expression.

Accepts both SQLAlchemy type classes (DateTime) and instances (DateTime(timezone=True)).

Example:

from bedrock.model.wrappers.calculated_type import calculated_type

Officer.sia_licence_expiry = calculated_type(DateTime,
    select(Evidence.expiry_date)
    .where(Evidence.officer_uuid == Officer.uuid)
    .correlate(Officer)
    .scalar_subquery()
)

See column_property.

Parameters
  • sa_type: A SQLAlchemy type class or instance (e.g. DateTime, Integer, String(255)).
  • args: Positional arguments forwarded to column_property.
  • kwargs: Keyword arguments forwarded to column_property.