Core Utilities
Configuration, utilities, and helper functions.
FhircraftConfig
Path: fhircraft.config.FhircraftConfig
dataclass
FhircraftConfig(disable_validation_warnings: bool = False, disable_fhir_warnings: bool = False, disable_fhir_errors: bool = False, disabled_fhir_constraints: FrozenSet[str] = frozenset(), mode: Literal['strict', 'lenient', 'skip'] = 'strict')
Global configuration for FHIRcraft.
Attributes:
| Name | Type | Description |
|---|
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
disable_validation_warnings
|
bool
|
|
False
|
disable_fhir_warnings
|
bool
|
|
False
|
disable_fhir_errors
|
bool
|
|
False
|
disabled_fhir_constraints
|
FrozenSet[str]
|
Build an immutable unordered collection of unique elements. |
<dynamic>
|
mode
|
Literal['strict', 'lenient', 'skip']
|
|
'strict'
|
configure
configure(*, disable_validation_warnings: bool | None = None, disable_fhir_warnings: bool | None = None, disable_fhir_errors: bool | None = None, disabled_fhir_constraints: Container[str] | None = None, validation_mode: Literal['strict', 'lenient', 'skip'] | None = None) -> None
Configure FHIRcraft settings.
All parameters are optional; only the ones provided will be changed. Unspecified parameters retain their current values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
disable_validation_warnings
|
bool | None
|
Disable all validation warnings globally. |
None
|
disable_fhir_warnings
|
bool | None
|
Disable only FHIR warning-severity issues, keep errors. |
None
|
disable_fhir_errors
|
bool | None
|
Disable error-level constraints (use with extreme caution). |
None
|
disabled_fhir_constraints
|
Container[str] | None
|
Set of constraint keys to disable (e.g., {'dom-6'}). |
None
|
validation_mode
|
Literal['strict', 'lenient', 'skip'] | None
|
Validation mode - 'strict', 'lenient', or 'skip'. |
None
|
Source code in fhircraft/config.py
disable_constraint
disable_constraint(*constraint_keys: str) -> None
Disable specific validation constraints by their keys.
Creates a new configuration with the given keys added to
disabled_fhir_constraints; the change is visible to the current context
only and does not escape a surrounding override_config block.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*constraint_keys
|
str
|
One or more constraint keys to disable (e.g., 'dom-6'). |
()
|
Source code in fhircraft/config.py
enable_constraint
enable_constraint(*constraint_keys: str) -> None
Re-enable specific validation constraints by their keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*constraint_keys
|
str
|
One or more constraint keys to re-enable. |
()
|
Source code in fhircraft/config.py
get_config
get_config() -> FhircraftConfig
Get the current FHIRcraft configuration.
Returns:
| Name | Type | Description |
|---|---|---|
FhircraftConfig |
FhircraftConfig
|
The current configuration instance. |
load_config_from_env
Load configuration from environment variables.
Supported environment variables
- FHIRCRAFT_DISABLE_WARNINGS: 'true' to disable all validation warnings
- FHIRCRAFT_VALIDATION_MODE: 'strict', 'lenient', or 'skip'
- FHIRCRAFT_DISABLED_CONSTRAINTS: Comma-separated constraint keys
Source code in fhircraft/config.py
override_config
override_config(*, disable_validation_warnings: bool | None = None, disable_fhir_warnings: bool | None = None, disable_fhir_errors: bool | None = None, disabled_fhir_constraints: Container[str] | None = None, validation_mode: Literal['strict', 'lenient', 'skip'] | None = None)
Context manager for temporary configuration changes.
All parameters are optional; only the ones provided will be changed within the context block. Previous configuration is automatically restored on exit, even if an exception occurs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
disable_validation_warnings
|
bool | None
|
Disable all validation warnings globally. |
None
|
disable_fhir_warnings
|
bool | None
|
Disable only FHIR warning-severity issues, keep errors. |
None
|
disable_fhir_errors
|
bool | None
|
Disable error-level constraints (use with extreme caution). |
None
|
disabled_fhir_constraints
|
Container[str] | None
|
Set of constraint keys to disable (e.g., {'dom-6'}). |
None
|
validation_mode
|
Literal['strict', 'lenient', 'skip'] | None
|
Validation mode - 'strict', 'lenient', or 'skip'. |
None
|
Yields:
| Name | Type | Description |
|---|---|---|
FhircraftConfig |
The temporary configuration. |
Source code in fhircraft/config.py
reset_config
Reset configuration to default values.
This is useful for testing or when you want to clear all configuration changes.
capitalize
contains_list_type
Recursively check if List is anywhere in the variable's typing.
Source code in fhircraft/utils.py
contains_only_none
Check if the input contains only None values recursively.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
Any
|
The input dictionary or list to check for only None values. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
result |
bool
|
True if the input contains only None values, False otherwise. |
Source code in fhircraft/utils.py
ensure_list
Ensure that the input variable is converted into a list if it is not already an iterable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
variable
|
any
|
The input variable that needs to be converted into a list if it is not already an iterable. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
variable |
list
|
The input variable converted into a list, or the input variable itself if it was already an iterable. |
Source code in fhircraft/utils.py
get_FHIR_release_from_version
Source code in fhircraft/utils.py
get_all_models_from_field
get_all_models_from_field(field: FieldInfo, issubclass_of: type[T_] = BaseModel) -> Generator[Type[T_], None, None]
Source code in fhircraft/utils.py
get_dict_paths
get_dict_paths(nested_dict: Union[Dict[str, Any], List[Dict[str, Any]]], prefix: str = '') -> Dict[str, Any]
Get all paths in a nested dictionary with their corresponding values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nested_dict
|
Union[Dict[str, Any], List[Dict[str, Any]]]
|
The nested dictionary or list of dictionaries to extract paths from. |
required |
prefix
|
str
|
The prefix to be added to the paths (default is ''). |
''
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dict[str, Any]: A dictionary containing all paths in the nested dictionary with their corresponding values. |
Source code in fhircraft/utils.py
get_fhir_model_from_field
get_module_name
Returns the name of the module to which the given object belongs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The object whose module name is to be retrieved. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The name of the module containing the object. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the object does not belong to any module. |
Source code in fhircraft/utils.py
is_dict_subset
Return True if all keys/values in subset are present in superset.
Source code in fhircraft/utils.py
is_list_field
is_list_field(field) -> bool
Determines if a given Pydantic field or FieldInfo is a list type, including Optional[List[T]] and Union[List[T], ...].
Source code in fhircraft/utils.py
is_url
load_env_variables
Loads environment variables from a .env file into a dictionary without changing the global environment variables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
Optional[str]
|
Optional path to the .env file. If not provided, it looks for a .env file in the current directory. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
vars |
dict
|
A dictionary containing the environment variables from the .env file. |
Source code in fhircraft/utils.py
load_file
Load data from a file based on its extension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
The path to the file to load. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
data |
dict
|
The data loaded from the file as a dictionary. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the file content is not a dictionary (for YAML files). |
Source code in fhircraft/utils.py
load_url
Load content from a URL and parse it based on the content type (YAML or JSON).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The URL to load content from. |
required |
Returns:
| Type | Description |
|---|---|
Dict
|
Union[Dict, List, Any]: Parsed content from the URL. Can be a dictionary, list, or any other valid JSON/YAML data type. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the URL format is invalid or the content type is not supported. |
Source code in fhircraft/utils.py
model_rebuild_all
Call model_rebuild() on all Pydantic models defined in the module where this function is called. This is useful when models have forward references or need to be re-evaluated after all classes have been defined.
Source code in fhircraft/utils.py
remove_none_dicts
remove_none_dicts(d: Union[Dict[str, Any], List[Any], Any]) -> Union[Dict[str, Any], List[Any], Any]
Remove any dictionaries with all values being None from the input dictionary recursively.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
Union[Dict[str, Any], List[Any], Any]
|
The input dictionary or list to remove None values from. |
required |
Returns:
| Type | Description |
|---|---|
Union[Dict[str, Any], List[Any], Any]
|
Union[Dict[str, Any], List[Any], Any]: The dictionary or list with None values removed. |
Source code in fhircraft/utils.py
replace_nth
Replace the nth occurrence of a substring in a string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
string
|
str
|
The original string. |
required |
sub
|
str
|
The substring to be replaced. |
required |
wanted
|
str
|
The new substring to replace with. |
required |
n
|
int
|
The occurrence number of the substring to replace. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
string |
str
|
The updated string after replacing the nth occurrence of the substring. |
Source code in fhircraft/utils.py
to_snake_case
Convert a given string from CamelCase to snake_case.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The input string in CamelCase format. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The converted string in snake_case format. |