Base FHIR Models¶
These classes provide base for constructing Fhircraft-compatible Pydantic FHIR models.
base ¶
Modules:
| Name | Description |
|---|---|
mixins |
|
models |
Core FHIRBaseModel — the abstract Pydantic base class for all FHIR types. |
primitives |
|
Classes:
| Name | Description |
|---|---|
FHIRBaseModel |
Abstract base class for all FHIR resource and data-type models. |
FHIRModelKind |
Enumeration of FHIR StructureDefinition kinds. |
FHIRSliceModel |
Base class for representation of FHIR profiled slices as Pydantic objects. |
FHIRList |
Custom list wrapper that maintains parent context on mutations. |
FHIRPrimitiveModel |
Base class for FHIR primitive types. |
StringBase |
A release-independent base metaclass for FHIR string types |
BooleanBase |
A release-independent base metaclass for FHIR boolean types |
IntegerBase |
A release-independent base metaclass for FHIR integer types |
DecimalBase |
A release-independent base metaclass for FHIR decimal types |
DateBase |
A release-independent base metaclass for FHIR date types |
DateTimeBase |
A release-independent base metaclass for FHIR dateTime types |
TimeBase |
A release-independent base metaclass for FHIR time types |
UriBase |
A release-independent base metaclass for FHIR uri types |
UrlBase |
A release-independent base metaclass for FHIR url types |
CodeBase |
A release-independent base metaclass for FHIR code types |
IdBase |
A release-independent base metaclass for FHIR id types |
OidBase |
A release-independent base metaclass for FHIR oid types |
CanonicalBase |
A release-independent base metaclass for FHIR canonical types |
UuidBase |
A release-independent base metaclass for FHIR uuid types |
InstantBase |
A release-independent base metaclass for FHIR instant types |
MarkdownBase |
A release-independent base metaclass for FHIR markdown types |
PositiveIntBase |
A release-independent base metaclass for FHIR positiveInt types |
UnsignedIntBase |
A release-independent base metaclass for FHIR unsignedInt types |
Base64BinaryBase |
A release-independent base metaclass for FHIR base64Binary types |
XhtmlBase |
A release-independent base metaclass for FHIR xhtml types |
Integer64Base |
A release-independent base metaclass for FHIR integer64 types (R5) |
FHIRBaseModel
Path: fhircraft.fhir.resources.base.FHIRBaseModel
Bases: BaseModel, ABC, FHIRContextMixin, FHIRXMLMixin, FHIRPolymorphicMixin, FHIRPathMixin
Abstract base class for all FHIR resource and data-type models.
Extends Pydantic's BaseModel with FHIR-specific behaviour: - Hierarchical parent-context tracking (_parent / _index) - Polymorphic serialization and deserialization - XML serialization and deserialization - Profile-slice construction and introspection - FHIRPath expression evaluation (via FHIRPathMixin)
This class is abstract and may not be instantiated directly. All concrete FHIR types are generated subclasses that define _type.
Methods:
| Name | Description |
|---|---|
model_post_init |
Wire up parent-context tracking after construction. |
model_validate |
Validate |
model_validate_json |
Deserialize json_data and return a validated model instance. |
model_construct |
Construct a model instance without running validation. |
model_copy |
Return a copy of this instance with parent context reset. |
model_post_init ¶
model_post_init(context: Any) -> None
Wire up parent-context tracking after construction.
model_validate
classmethod
¶
model_validate(obj: Any, *, strict: bool | None = None, from_attributes: bool | None = None, context: Any = None, extra: ExtraValues | None = None, by_alias: bool | None = None, by_name: bool | None = None) -> Self
Validate obj and return a model instance.
model_validate_json
classmethod
¶
model_validate_json(json_data: str | bytes | bytearray, *, strict: bool | None = False, context: Any = None, extra: ExtraValues | None = None, by_alias: bool | None = None, by_name: bool | None = None) -> Self
Deserialize json_data and return a validated model instance.
model_construct
classmethod
¶
model_copy ¶
Return a copy of this instance with parent context reset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
update
|
Mapping[str, Any] | None
|
Optional field updates to apply to the copy. |
None
|
deep
|
bool
|
Whether to perform a deep copy. |
False
|
Returns:
| Type | Description |
|---|---|
Self
|
A copied instance with no parent context. |
FHIRSliceModel
Path: fhircraft.fhir.resources.base.FHIRSliceModel
Bases: FHIRBaseModel
Base class for representation of FHIR profiled slices as Pydantic objects.
Expands the FHIRBaseModel class with slice-specific methods.
FHIRList
Path: fhircraft.fhir.resources.base.FHIRList
FHIRList(items: Iterable[Any] | None = None, parent: FHIRBaseModel | None = None)
Bases: list
Custom list wrapper that maintains parent context on mutations.
This list automatically propagates parent, _root_resource, _resource, and _index context to FHIRBaseModel items when they are added via append, extend, insert, or __setitem_.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
Iterable[Any] | None
|
Initial items for the list. |
None
|
parent
|
FHIRBaseModel | None
|
The parent FHIRBaseModel instance that owns this list. |
None
|
Methods:
| Name | Description |
|---|---|
append |
Append item and propagate context. |
extend |
Extend list and propagate context to new items. |
insert |
Insert item and propagate context. |
FHIRPrimitiveModel
Path: fhircraft.fhir.resources.base.FHIRPrimitiveModel
FHIRPrimitiveModel(__value: Any | None = None, **data)
Bases: FHIRBaseModel
Base class for FHIR primitive types.
FHIR primitives are represented as Pydantic models with a single value field that holds the actual primitive value.
This design allows us to attach extensions to primitive values while still treating them as simple types in most contexts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any | None
|
The actual value |
None
|
StringBase
Path: fhircraft.fhir.resources.base.StringBase
StringBase(__value: Any | None = None, **data)
Bases: FHIRPrimitiveModel
A release-independent base metaclass for FHIR string types
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str | None
|
|
None
|
BooleanBase
Path: fhircraft.fhir.resources.base.BooleanBase
BooleanBase(__value: Any | None = None, **data)
Bases: FHIRPrimitiveModel
A release-independent base metaclass for FHIR boolean types
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
bool | None
|
|
None
|
IntegerBase
Path: fhircraft.fhir.resources.base.IntegerBase
IntegerBase(__value: Any | None = None, **data)
Bases: FHIRPrimitiveModel
A release-independent base metaclass for FHIR integer types
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
int | None
|
|
None
|
DecimalBase
Path: fhircraft.fhir.resources.base.DecimalBase
DecimalBase(__value: Any | None = None, **data)
Bases: FHIRPrimitiveModel
A release-independent base metaclass for FHIR decimal types
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
float | None
|
|
None
|
DateBase
Path: fhircraft.fhir.resources.base.DateBase
DateBase(__value: Any | None = None, **data)
Bases: FHIRPrimitiveModel
A release-independent base metaclass for FHIR date types
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str | None
|
|
None
|
DateTimeBase
Path: fhircraft.fhir.resources.base.DateTimeBase
DateTimeBase(__value: Any | None = None, **data)
Bases: FHIRPrimitiveModel
A release-independent base metaclass for FHIR dateTime types
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str | None
|
|
None
|
TimeBase
Path: fhircraft.fhir.resources.base.TimeBase
TimeBase(__value: Any | None = None, **data)
Bases: FHIRPrimitiveModel
A release-independent base metaclass for FHIR time types
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str | None
|
|
None
|
UriBase
Path: fhircraft.fhir.resources.base.UriBase
UriBase(__value: Any | None = None, **data)
Bases: StringBase
A release-independent base metaclass for FHIR uri types
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str | None
|
|
None
|
CodeBase
Path: fhircraft.fhir.resources.base.CodeBase
CodeBase(__value: Any | None = None, **data)
Bases: StringBase
A release-independent base metaclass for FHIR code types
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str | None
|
|
None
|
IdBase
Path: fhircraft.fhir.resources.base.IdBase
IdBase(__value: Any | None = None, **data)
Bases: StringBase
A release-independent base metaclass for FHIR id types
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str | None
|
|
None
|
CanonicalBase
Path: fhircraft.fhir.resources.base.CanonicalBase
CanonicalBase(__value: Any | None = None, **data)
UuidBase
Path: fhircraft.fhir.resources.base.UuidBase
UuidBase(__value: Any | None = None, **data)
InstantBase
Path: fhircraft.fhir.resources.base.InstantBase
InstantBase(__value: Any | None = None, **data)
Bases: FHIRPrimitiveModel
A release-independent base metaclass for FHIR instant types
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str | None
|
|
None
|
MarkdownBase
Path: fhircraft.fhir.resources.base.MarkdownBase
MarkdownBase(__value: Any | None = None, **data)
Bases: StringBase
A release-independent base metaclass for FHIR markdown types
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str | None
|
|
None
|
PositiveIntBase
Path: fhircraft.fhir.resources.base.PositiveIntBase
PositiveIntBase(__value: Any | None = None, **data)
Bases: IntegerBase
A release-independent base metaclass for FHIR positiveInt types
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
int | None
|
|
None
|
UnsignedIntBase
Path: fhircraft.fhir.resources.base.UnsignedIntBase
UnsignedIntBase(__value: Any | None = None, **data)
Bases: IntegerBase
A release-independent base metaclass for FHIR unsignedInt types
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
int | None
|
|
None
|
Base64BinaryBase
Path: fhircraft.fhir.resources.base.Base64BinaryBase
Base64BinaryBase(__value: Any | None = None, **data)
Bases: FHIRPrimitiveModel
A release-independent base metaclass for FHIR base64Binary types
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str | None
|
|
None
|
XhtmlBase
Path: fhircraft.fhir.resources.base.XhtmlBase
XhtmlBase(__value: Any | None = None, **data)
Bases: StringBase
A release-independent base metaclass for FHIR xhtml types
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str | None
|
|
None
|
Integer64Base
Path: fhircraft.fhir.resources.base.Integer64Base
Integer64Base(__value: Any | None = None, **data)
Bases: IntegerBase
A release-independent base metaclass for FHIR integer64 types (R5)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
int | None
|
|
None
|
mixins ¶
Modules:
| Name | Description |
|---|---|
context |
Parent-context tracking mixin for FHIRBaseModel. |
polymorphic |
ContextVar-based recursion guards and polymorphic serialization/deserialization mixin. |
xml |
XML serialization/deserialization mixin for FHIRBaseModel. |
Classes:
| Name | Description |
|---|---|
FHIRContextMixin |
Mixin that provides hierarchical parent-context tracking. |
FHIRXMLMixin |
Mixin providing XML serialization and deserialization for FHIR models. |
FHIRPolymorphicMixin |
Mixin providing polymorphic serialization and deserialization helpers. |
FHIRContextMixin
Path: fhircraft.fhir.resources.base.mixins.FHIRContextMixin
Mixin that provides hierarchical parent-context tracking.
Every FHIR model instance stores a reference to its direct parent (_parent) and its position within a list (_index). The root resource and nearest enclosing resource are resolved lazily by walking the parent chain, avoiding the need to store and update those references.
FHIRXMLMixin
Path: fhircraft.fhir.resources.base.mixins.FHIRXMLMixin
Mixin providing XML serialization and deserialization for FHIR models.
Implements the FHIR XML format as described in the FHIR specification, including correct handling of primitive shadow elements and namespace.
Methods:
| Name | Description |
|---|---|
model_dump_xml |
Serialize the FHIR resource to an XML string. |
model_validate_xml |
Deserialize a FHIR XML string into a model instance. |
model_dump_xml ¶
model_dump_xml(*, indent: int | None = None, ensure_ascii: bool = True, include: IncEx | None = None, exclude: IncEx | None = None, exclude_unset: bool = False, exclude_none: bool = False, exclude_defaults: bool = False) -> str
Serialize the FHIR resource to an XML string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
indent
|
int | None
|
Number of indent levels. |
None
|
ensure_ascii
|
bool
|
When True, non-ASCII characters are escaped. |
True
|
include
|
IncEx | None
|
Fields to include (passed through to model_dump). |
None
|
exclude
|
IncEx | None
|
Fields to exclude (passed through to model_dump). |
None
|
exclude_unset
|
bool
|
Exclude fields that were not explicitly set. |
False
|
exclude_none
|
bool
|
Exclude fields whose value is None. |
False
|
exclude_defaults
|
bool
|
Exclude fields that equal their default value. |
False
|
Returns:
| Type | Description |
|---|---|
str
|
An XML string conforming to the FHIR XML format. |
model_validate_xml
classmethod
¶
Deserialize a FHIR XML string into a model instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xml_data
|
str
|
The XML string to deserialize. |
required |
strict
|
bool | None
|
Whether to validate strictly. |
None
|
context
|
Any
|
Additional context forwarded to model_validate. |
None
|
Returns:
| Type | Description |
|---|---|
T
|
A validated model instance populated from the XML data. |
FHIRPolymorphicMixin
Path: fhircraft.fhir.resources.base.mixins.FHIRPolymorphicMixin
Mixin providing polymorphic serialization and deserialization helpers.
These are helpers called from the Pydantic validators/serializers that remain on FHIRBaseModel; they are not validators themselves and carry no Pydantic decorator magic.