Types#
Module: restalchemy.dm.types
DM types describe the allowed values for properties and how values are converted to/from simple representations (for JSON, OpenAPI, storage, etc.).
All property types inherit from BaseType.
BaseType#
BaseType#
Core interface for all DM types:
validate(value) -> bool: checks whether the value is acceptable.to_simple_type(value): converts a value to a simple Python type (string, number, dict, list…).from_simple_type(value): converts back from a simple type.from_unicode(value): parses a string representation.to_openapi_spec(prop_kwargs): builds OpenAPI schema fragment.
Many concrete types are based on BasePythonType, which wraps a Python type like int or str.
Scalar types#
Boolean#
- Wraps
bool. - Accepts any truthy/falsy value from
from_simple_type()and string representations fromfrom_unicode().
String#
- Wraps
strwith length constraints. - Parameters:
min_length,max_length. to_openapi_spec()addsminLength/maxLength.
Common subclasses:
Email— validates email addresses (with optional deliverability checks).
Integer#
- Wraps
intwithmin_valueandmax_value. Int8,Int16, etc. are specialized variants.
Float#
- Wraps
floatwith bounds.
Decimal#
- Wraps
decimal.Decimalwith optionalmax_decimal_places. - Serializes to/from string to avoid precision loss.
UUID#
- Wraps
uuid.UUID. - Serializes to string form.
Enum#
- Restricts values to a given set of allowed values.
Example:
Use it in properties:
Datetime and time-related types#
UTCDateTimeZ#
- Wraps
datetime.datetimeand enforcestzinfo == datetime.timezone.utc. - Serializes to string in MySQL / RFC3339-like format.
- The naive
UTCDateTime, which took whatever timezone a stored string named, was removed in 16.0.0. It read and wrote the same two formats, so a property declared with it becomesUTCDateTimeZand the stored values are read back unchanged — as UTC, which is what they were assumed to be.
TimeDelta#
- Wraps
datetime.timedelta. - Serializes to seconds as float.
DateTime#
- Legacy timestamp type, serializes to Unix timestamps.
Collection types#
List and TypedList#
Listvalidates that the value is a Python list.TypedList(nested_type)ensures each element is valid fornested_type.
Example:
Dict and structured dicts#
Dictvalidates that the value is adictwith string keys.TypedDict(nested_type)enforces that all values matchnested_type.
Schema-based dicts:
SoftSchemeDict(scheme)— dict with keys that are a subset of the scheme.SchemeDict(scheme)— dict that must match the scheme exactly.
Example:
from restalchemy.dm import types
settings_scheme = {
"retries": types.Integer(min_value=0),
"timeout": types.Float(min_value=0.0),
}
settings_type = types.SoftSchemeDict(settings_scheme)
Use it in a property:
Nullable and wrapper types#
AllowNone(nested_type)#
- Allows either
Noneor a valid value fornested_type. to_simple_type()andfrom_simple_type()propagate throughnested_typewhen notNone.to_openapi_spec()addsnullable: true.
Example:
Regexp and URL-related types#
BaseRegExpType and BaseCompiledRegExpTypeFromAttr#
Low-level base classes for regexp-based types.
Concrete types include:
Uri— validates URI paths ending with a UUID.Mac— validates MAC addresses.Hostname(deprecated) — seetypes_network.Url— HTTP/FTP URL validator.
These types are useful for networking and resource identifiers.
Dynamic and network types#
Additional specialized types live in:
restalchemy.dm.types_dynamicrestalchemy.dm.types_network
Examples include:
- More advanced hostnames, IP networks, CIDR ranges.
- Dynamic structures with runtime-defined schemas.
This reference does not list all of them exhaustively, but the basic usage pattern is always the same:
- Instantiate the type.
- Use it in
properties.property(). - Let DM validation and conversion handle the rest.
Best practices#
- Prefer DM types (
types.String,types.Integer, etc.) to raw Python types; they encode validation and OpenAPI metadata. - Use
AllowNoneinstead of manually allowingNonein your business logic. - Use
Enumfor small closed sets of allowed values. - For complex JSON-like structures, use
SoftSchemeDict,SchemeDictorTypedDictinstead of a bareDict.