Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.10/site-packages/pillow-11.0.0-py3.10-linux-x86_64.egg/PIL/_deprecate.py: 17%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

23 statements  

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 msg = f"{deprecated} {is_} deprecated and should be removed." 

47 raise RuntimeError(msg) 

48 elif when == 12: 

49 removed = "Pillow 12 (2025-10-15)" 

50 else: 

51 msg = f"Unknown removal version: {when}. Update {__name__}?" 

52 raise ValueError(msg) 

53 

54 if replacement and action: 

55 msg = "Use only one of 'replacement' and 'action'" 

56 raise ValueError(msg) 

57 

58 if replacement: 

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

60 elif action: 

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

62 else: 

63 action = "" 

64 

65 warnings.warn( 

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

67 DeprecationWarning, 

68 stacklevel=3, 

69 )