from typing import TypeVar, Type T = TypeVar("T") class BaseClientFactory: """External API clients factory.""" def _get_client(self, client_cls: Type[T]) -> T: """Get client or create new if does not exist.""" client_prop_name = f"_{client_cls.__name__.lower()}" instance = getattr(self, client_prop_name, None) if not instance: instance = client_cls() setattr(self, client_prop_name, instance) return instance