from typing import Type, TypeVar
from pydantic import BaseModel # type: ignore
import copy


T = TypeVar('T', bound=BaseModel)

def cast_to_model(model_cls: Type[T], source: BaseModel) -> T:
    data = {field: getattr(source, field) for field in model_cls.__fields__}
    return model_cls(**data)

def cast_to_model_safe(model_cls: Type[T], source: BaseModel) -> T:
    data = {field: copy.deepcopy(getattr(source, field)) for field in model_cls.__fields__}
    return model_cls(**data)
