Skip to content

models

field_origins

field_origins(
    m: Type[BaseModel], name: str
) -> Iterator[Type[BaseModel]]

Return sequence of bases where the field type hint was defined / overridden.

Source code in src/metador_core/util/models.py
 9
10
11
12
13
def field_origins(m: Type[BaseModel], name: str) -> Iterator[Type[BaseModel]]:
    """Return sequence of bases where the field type hint was defined / overridden."""
    return (
        b for b in m.__mro__ if issubclass(b, BaseModel) and name in get_annotations(b)
    )

updated_fields

updated_fields(m: Type[BaseModel]) -> Set[str]

Return subset of fields that are added or overridden by a new type hint.

Source code in src/metador_core/util/models.py
16
17
18
def updated_fields(m: Type[BaseModel]) -> Set[str]:
    """Return subset of fields that are added or overridden by a new type hint."""
    return {n for n in m.__fields__.keys() if next(field_origins(m, n)) is m}

field_atomic_types

field_atomic_types(
    mf: ModelField, *, bound=object
) -> Iterator[Type]

Return sequence of nested atomic types in the hint of given field.

Source code in src/metador_core/util/models.py
26
27
28
def field_atomic_types(mf: ModelField, *, bound=object) -> Iterator[Type]:
    """Return sequence of nested atomic types in the hint of given field."""
    return filter(is_subclass_of(bound), traverse_typehint(mf.type_))

atomic_types

atomic_types(
    m: BaseModel, *, bound=object
) -> Dict[str, Set[Type]]

Return dict from field name to model classes referenced in the field definition.

Parameters:

Name Type Description Default
m BaseModel

Pydantic model

required
bound object

If provided, will be used to filter results to contain only subclasses of the bound.

object
Source code in src/metador_core/util/models.py
31
32
33
34
35
36
37
38
39
def atomic_types(m: BaseModel, *, bound=object) -> Dict[str, Set[Type]]:
    """Return dict from field name to model classes referenced in the field definition.

    Args:
        m: Pydantic model
        bound (object): If provided, will be used to filter results to
            contain only subclasses of the bound.
    """
    return {k: set(field_atomic_types(v, bound=bound)) for k, v in m.__fields__.items()}

field_parent_type

field_parent_type(
    m: Type[BaseModel], name: str
) -> Type[BaseModel]

Return type of field assigned in the next parent that provides a type hint.

Source code in src/metador_core/util/models.py
42
43
44
45
46
47
def field_parent_type(m: Type[BaseModel], name: str) -> Type[BaseModel]:
    """Return type of field assigned in the next parent that provides a type hint."""
    b = next(filter(lambda x: x is not m, field_origins(m, name)), None)
    if not b:
        raise ValueError(f"No base class of {m} defines a field called '{name}'!")
    return get_type_hints(b).get(name)