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

1from __future__ import annotations 

2 

3from abc import ABC, abstractmethod 

4from typing import Generic, TypeVar, Iterable, cast 

5from typing_extensions import override 

6 

7T = TypeVar('T') 

8 

9 

10# NOTE: this is not an exhaustive list of supported data methods. 

11# Feel free to add any more if need be. 

12 

13 

14class LazyProxy(Generic[T], ABC): 

15 def __init__(self) -> None: 

16 self.__proxied: T | None = None 

17 

18 def __getattr__(self, attr: str) -> object: 

19 return getattr(self.__get_proxied__(), attr) 

20 

21 @override 

22 def __repr__(self) -> str: 

23 return repr(self.__get_proxied__()) 

24 

25 @override 

26 def __str__(self) -> str: 

27 return str(self.__get_proxied__()) 

28 

29 @override 

30 def __dir__(self) -> Iterable[str]: 

31 return self.__get_proxied__().__dir__() 

32 

33 def __get_proxied__(self) -> T: 

34 proxied = self.__proxied 

35 if proxied is not None: 

36 return proxied 

37 

38 self.__proxied = proxied = self.__load__() 

39 return proxied 

40 

41 def __set_proxied__(self, value: T) -> None: 

42 self.__proxied = value 

43 

44 def __as_proxied__(self) -> T: 

45 """Helper method that returns the current proxy, typed as the loaded object""" 

46 return cast(T, self) 

47 

48 @abstractmethod 

49 def __load__(self) -> T: ...