Coverage for src/prisma/_proxy.py: 100%
30 statements
« prev ^ index » next coverage.py v7.2.7, created at 2024-08-27 18:25 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2024-08-27 18:25 +0000
1from __future__ import annotations
3from abc import ABC, abstractmethod
4from typing import Generic, TypeVar, Iterable, cast
5from typing_extensions import override
7T = TypeVar('T')
10# NOTE: this is not an exhaustive list of supported data methods.
11# Feel free to add any more if need be.
14class LazyProxy(Generic[T], ABC):
15 def __init__(self) -> None:
16 self.__proxied: T | None = None
18 def __getattr__(self, attr: str) -> object:
19 return getattr(self.__get_proxied__(), attr)
21 @override
22 def __repr__(self) -> str:
23 return repr(self.__get_proxied__())
25 @override
26 def __str__(self) -> str:
27 return str(self.__get_proxied__())
29 @override
30 def __dir__(self) -> Iterable[str]:
31 return self.__get_proxied__().__dir__()
33 def __get_proxied__(self) -> T:
34 proxied = self.__proxied
35 if proxied is not None:
36 return proxied
38 self.__proxied = proxied = self.__load__()
39 return proxied
41 def __set_proxied__(self, value: T) -> None:
42 self.__proxied = value
44 def __as_proxied__(self) -> T:
45 """Helper method that returns the current proxy, typed as the loaded object"""
46 return cast(T, self)
48 @abstractmethod
49 def __load__(self) -> T: ...