def _replace_type_var(ret_type: MypyType, to_replace: str, replace_by: MypyType) -> MypyType:
    """
    Substitutes a specified type variable within a Mypy type expression with an actual type.

    This function is recursive, and it operates on various kinds of Mypy types like Instance,
    ProperType, etc., to deeply replace the specified type variable.

    Parameters:
    - ret_type: A Mypy type expression where the substitution should occur.
    - to_replace: The type variable to be replaced, specified as its full name.
    - replace_by: The actual Mypy type to substitute in place of 'to_replace'.

    Example:
    Given:
        ret_type = "typing.Collection[T]"
        to_replace = "T"
        replace_by = "myapp.models.MyModel"
    Result:
        "typing.Collection[myapp.models.MyModel]"
    """
    if isinstance(ret_type, TypeVarType) and ret_type.fullname == to_replace:
        return replace_by

    ret_type = copy_type(get_proper_type(ret_type))

    if isinstance(ret_type, Instance):
        # Since it is an instance, recursively find the type var for all its args.
        ret_type.args = tuple(_replace_type_var(item, to_replace, replace_by) for item in ret_type.args)
        return ret_type

    if hasattr(ret_type, "item"):
        # For example TypeType has an item. find the type_var for this item
        ret_type.item = _replace_type_var(ret_type.item, to_replace, replace_by)
    if hasattr(ret_type, "items"):
        # For example TypeList has items. find recursively type_var for its items
        ret_type.items = [_replace_type_var(item, to_replace, replace_by) for item in ret_type.items]
    return ret_type

    Substitutes a specified type variable within a Mypy type expression with an actual type.

    This function is recursive, and it operates on various kinds of Mypy types like Instance,
    ProperType, etc., to deeply replace the specified type variable.

    Parameters:
    - ret_type: A Mypy type expression where the substitution should occur.
    - to_replace: The type variable to be replaced, specified as its full name.
    - replace_by: The actual Mypy type to substitute in place of 'to_replace'.

    Example:
    Given:
        ret_type = "typing.Collection[T]"
        to_replace = "T"
        replace_by = "myapp.models.MyModel"
    Result:
        "typing.Collection[myapp.models.MyModel]"
    
mypy_django_plugin/transformers/managers.py