Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/PIL/_deprecate.py: 12%

20 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

1from __future__ import annotations 

2 

3import warnings 

4 

5from . import __version__ 

6 

7 

8def deprecate( 

9 deprecated: str, 

10 when: int | None, 

11 replacement: str | None = None, 

12 *, 

13 action: str | None = None, 

14 plural: bool = False, 

15) -> None: 

16 """ 

17 Deprecations helper. 

18 

19 :param deprecated: Name of thing to be deprecated. 

20 :param when: Pillow major version to be removed in. 

21 :param replacement: Name of replacement. 

22 :param action: Instead of "replacement", give a custom call to action 

23 e.g. "Upgrade to new thing". 

24 :param plural: if the deprecated thing is plural, needing "are" instead of "is". 

25 

26 Usually of the form: 

27 

28 "[deprecated] is deprecated and will be removed in Pillow [when] (yyyy-mm-dd). 

29 Use [replacement] instead." 

30 

31 You can leave out the replacement sentence: 

32 

33 "[deprecated] is deprecated and will be removed in Pillow [when] (yyyy-mm-dd)" 

34 

35 Or with another call to action: 

36 

37 "[deprecated] is deprecated and will be removed in Pillow [when] (yyyy-mm-dd). 

38 [action]." 

39 """ 

40 

41 is_ = "are" if plural else "is" 

42 

43 if when is None: 

44 removed = "a future version" 

45 elif when <= int(__version__.split(".")[0]): 

46 raise RuntimeError(f"{deprecated} {is_} deprecated and should be removed.") 

47 elif when == 10: 

48 removed = "Pillow 10 (2023-07-01)" 

49 else: 

50 raise ValueError(f"Unknown removal version, update {__name__}?") 

51 

52 if replacement and action: 

53 raise ValueError("Use only one of 'replacement' and 'action'") 

54 

55 if replacement: 

56 action = f". Use {replacement} instead." 

57 elif action: 

58 action = f". {action.rstrip('.')}." 

59 else: 

60 action = "" 

61 

62 warnings.warn( 

63 f"{deprecated} {is_} deprecated and will be removed in {removed}{action}", 

64 DeprecationWarning, 

65 stacklevel=3, 

66 )