nat.data_models.common#

Attributes#

Classes#

HashableBaseModel

Subclass of a Pydantic BaseModel that is hashable. Use in objects that need to be hashed for caching purposes.

BaseModelRegistryTag

TypedBaseModel

Subclass of Pydantic BaseModel that allows for specifying the object type. Use in Pydantic discriminated unions.

Functions#

subclass_depth(→ int)

Compute a class' subclass depth.

_get_origin_or_base(→ type)

Get the origin of a type or the base class if it is not a generic.

get_secret_value(→ str | None)

Extract the secret value from a SecretStr or return None.

set_secret_from_env(model, field_name, env_var)

Set a SecretStr field in a Pydantic model from an environment variable, but only if the environment variable is set.

Module Contents#

_LT#
class HashableBaseModel(/, **data: Any)#

Bases: pydantic.BaseModel

Subclass of a Pydantic BaseModel that is hashable. Use in objects that need to be hashed for caching purposes.

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

classmethod generate_json_schema() dict[str, Any]#
classmethod write_json_schema(schema_path: str) None#
subclass_depth(cls: type) int#

Compute a class’ subclass depth.

_get_origin_or_base(cls: type) type#

Get the origin of a type or the base class if it is not a generic.

class BaseModelRegistryTag#
class TypedBaseModel(/, **data: Any)#

Bases: pydantic.BaseModel

Subclass of Pydantic BaseModel that allows for specifying the object type. Use in Pydantic discriminated unions.

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

type: str = None#
full_type: ClassVar[str]#
_typed_model_name: ClassVar[str | None] = None#
model_post_init(__context)#

Set the type field to the correct value after instance creation.

classmethod model_json_schema(
by_alias: bool = True,
ref_template: str = '#/$defs/{model}',
schema_generator: type[GenerateJsonSchema] = GenerateJsonSchema,
mode: pydantic.json_schema.JsonSchemaMode = 'validation',
) dict#

Override to provide correct default for type field in schema.

classmethod static_type()#
classmethod static_full_type()#
static discriminator(v: Any) str | None#
TypedBaseModelT#
get_secret_value(v: pydantic.SecretStr | None) str | None#

Extract the secret value from a SecretStr or return None.

Parameters#

v: SecretStr or None.

A field defined as OptionalSecretStr, which is either a SecretStr or None.

Returns#

str | None

The secret value as a plain string, or None if v is None.

set_secret_from_env(
model: pydantic.BaseModel,
field_name: str,
env_var: str,
)#

Set a SecretStr field in a Pydantic model from an environment variable, but only if the environment variable is set.

Parameters#

model: BaseModel

The Pydantic model instance containing the field to set.

field_name: str

The name of the field in the model to set.

env_var: str

The name of the environment variable to read the secret value from.

SerializableSecretStr#
OptionalSecretStr#