def get_flat_models_from_field(field: Field) -> Set[Type['main.BaseModel']]:
    """
    Take a single Pydantic ``Field`` (from a model) that could have been declared as a sublcass of BaseModel
    (so, it could be a submodel), and generate a set with its model and all the sub-models in the tree.
    I.e. if you pass a field that was declared to be of type ``Foo`` (subclass of BaseModel) as ``field``, and that
    model ``Foo`` has a field of type ``Bar`` (also subclass of ``BaseModel``) and that model ``Bar`` has a field of
    type ``Baz`` (also subclass of ``BaseModel``), the return value will be ``set([Foo, Bar, Baz])``.

    :param field: a Pydantic ``Field``
    :return: a set with the model used in the declaration for this field, if any, and all its sub-models
    """
    flat_models: Set[Type['main.BaseModel']] = set()
    if field.sub_fields:
        flat_models |= get_flat_models_from_fields(field.sub_fields)
    elif lenient_issubclass(field.type_, main.BaseModel):
        flat_models |= get_flat_models_from_model(field.type_)
    elif lenient_issubclass(getattr(field.type_, '__pydantic_model__', None), main.BaseModel):
        field.type_ = cast(Type['dataclasses.DataclassType'], field.type_)
        flat_models |= get_flat_models_from_model(field.type_.__pydantic_model__)
    return flat_models
def get_flat_models_from_field ( field : Field ) -> Set [ Type [ 'main.BaseModel' ] ] : flat_models : Set [ Type [ 'main.BaseModel' ] ] = set ( ) if field . sub_fields : flat_models |= get_flat_models_from_fields ( field . sub_fields ) elif lenient_issubclass ( field . type_ , main . BaseModel ) : flat_models |= get_flat_models_from_model ( field . type_ ) elif lenient_issubclass ( getattr ( field . type_ , '__pydantic_model__' , None ) , main . BaseModel ) : field . type_ = cast ( Type [ 'dataclasses.DataclassType' ] , field . type_ ) flat_models |= get_flat_models_from_model ( field . type_ . __pydantic_model__ ) return flat_models
Take a single Pydantic ``Field`` (from a model) that could have been declared as a sublcass of BaseModel
    (so, it could be a submodel), and generate a set with its model and all the sub-models in the tree.
    I.e. if you pass a field that was declared to be of type ``Foo`` (subclass of BaseModel) as ``field``, and that
    model ``Foo`` has a field of type ``Bar`` (also subclass of ``BaseModel``) and that model ``Bar`` has a field of
    type ``Baz`` (also subclass of ``BaseModel``), the return value will be ``set([Foo, Bar, Baz])``.

    :param field: a Pydantic ``Field``
    :return: a set with the model used in the declaration for this field, if any, and all its sub-models