Coverage for cuda/core/_vendored/deprecated/classic.py: 63.24%

68 statements  

« prev     ^ index     » next       coverage.py v7.15.0, created at 2026-07-03 01:38 +0000

1# Vendored from the Deprecated package (https://pypi.org/project/Deprecated/), 

2# version 1.3.1, (c) Laurent LAPORTE, MIT License. 

3# Modified to remove the dependency on the `wrapt` package. 

4 

5import functools 

6import inspect 

7import warnings 

8 

9# stacklevel=2 points past the wrapper to the actual call site 

10_routine_stacklevel = 2 

11_class_stacklevel = 2 

12 

13string_types = (bytes, str) 

14 

15 

16class ClassicAdapter: 

17 """ 

18 Classic adapter -- *for advanced usage only* 

19 

20 This adapter is used to get the deprecation message according to the wrapped 

21 object type: class, function, standard method, static method, or class method. 

22 

23 This is the base class of the :class:`~deprecated.sphinx.SphinxAdapter` class 

24 which is used to update the wrapped object docstring. 

25 """ 

26 

27 def __init__(self, reason="", version="", action=None, category=DeprecationWarning, extra_stacklevel=0): 

28 self.reason = reason or "" 

29 self.version = version or "" 

30 self.action = action 

31 self.category = category 

32 self.extra_stacklevel = extra_stacklevel 

33 

34 def get_deprecated_msg(self, wrapped, instance): 

35 if instance is None: 1b

36 if inspect.isclass(wrapped): 1b

37 fmt = "Call to deprecated class {name}." 

38 else: 

39 fmt = "Call to deprecated function (or staticmethod) {name}." 1b

40 else: 

41 if inspect.isclass(instance): 

42 fmt = "Call to deprecated class method {name}." 

43 else: 

44 fmt = "Call to deprecated method {name}." 

45 if self.reason: 1b

46 fmt += " ({reason})" 1b

47 if self.version: 1b

48 fmt += " -- Deprecated since version {version}." 1b

49 return fmt.format(name=wrapped.__name__, reason=self.reason or "", version=self.version or "") 1b

50 

51 def __call__(self, wrapped): 

52 if inspect.isclass(wrapped): 

53 old_new1 = wrapped.__new__ 

54 

55 def wrapped_cls(cls, *args, **kwargs): 

56 msg = self.get_deprecated_msg(wrapped, None) 

57 stacklevel = _class_stacklevel + self.extra_stacklevel 

58 if self.action: 

59 with warnings.catch_warnings(): 

60 warnings.simplefilter(self.action, self.category) 

61 warnings.warn(msg, category=self.category, stacklevel=stacklevel) 

62 else: 

63 warnings.warn(msg, category=self.category, stacklevel=stacklevel) 

64 if old_new1 is object.__new__: 

65 return old_new1(cls) 

66 return old_new1(cls, *args, **kwargs) 

67 

68 wrapped.__new__ = staticmethod(wrapped_cls) 

69 return wrapped 

70 

71 elif inspect.isroutine(wrapped): 

72 adapter = self 

73 

74 @functools.wraps(wrapped) 

75 def wrapper(*args, **kwargs): 

76 msg = adapter.get_deprecated_msg(wrapped, None) 1b

77 stacklevel = _routine_stacklevel + adapter.extra_stacklevel 1b

78 if adapter.action: 1b

79 with warnings.catch_warnings(): 

80 warnings.simplefilter(adapter.action, adapter.category) 

81 warnings.warn(msg, category=adapter.category, stacklevel=stacklevel) 

82 else: 

83 warnings.warn(msg, category=adapter.category, stacklevel=stacklevel) 1b

84 return wrapped(*args, **kwargs) 1b

85 

86 return wrapper 

87 

88 else: 

89 raise TypeError(repr(type(wrapped))) 

90 

91 

92def deprecated(*args, **kwargs): 

93 """ 

94 Decorator which can be used to mark functions as deprecated. 

95 

96 It will result in a warning being emitted when the function is used. 

97 """ 

98 if args and isinstance(args[0], string_types): 

99 kwargs["reason"] = args[0] 

100 args = args[1:] 

101 

102 if args and not callable(args[0]): 

103 raise TypeError(repr(type(args[0]))) 

104 

105 if args: 

106 adapter_cls = kwargs.pop("adapter_cls", ClassicAdapter) 

107 adapter = adapter_cls(**kwargs) 

108 wrapped = args[0] 

109 return adapter(wrapped) 

110 

111 return functools.partial(deprecated, **kwargs)