|
1 |
| -import functools |
2 |
| -import inspect |
3 |
| -import warnings |
| 1 | +from warnings import warn |
4 | 2 |
|
5 |
| -string_types = (type(b""), type("")) |
6 | 3 |
|
7 |
| - |
8 |
| -def warn_deprecation(text): |
9 |
| - warnings.warn(text, category=DeprecationWarning, stacklevel=2) |
10 |
| - |
11 |
| - |
12 |
| -def deprecated(reason): |
13 |
| - """ |
14 |
| - This is a decorator which can be used to mark functions |
15 |
| - as deprecated. It will result in a warning being emitted |
16 |
| - when the function is used. |
17 |
| - """ |
18 |
| - |
19 |
| - if isinstance(reason, string_types): |
20 |
| - # The @deprecated is used with a 'reason'. |
21 |
| - # |
22 |
| - # .. code-block:: python |
23 |
| - # |
24 |
| - # @deprecated("please, use another function") |
25 |
| - # def old_function(x, y): |
26 |
| - # pass |
27 |
| - |
28 |
| - def decorator(func1): |
29 |
| - if inspect.isclass(func1): |
30 |
| - fmt1 = f"Call to deprecated class {func1.__name__} ({reason})." |
31 |
| - else: |
32 |
| - fmt1 = f"Call to deprecated function {func1.__name__} ({reason})." |
33 |
| - |
34 |
| - @functools.wraps(func1) |
35 |
| - def new_func1(*args, **kwargs): |
36 |
| - warn_deprecation(fmt1) |
37 |
| - return func1(*args, **kwargs) |
38 |
| - |
39 |
| - return new_func1 |
40 |
| - |
41 |
| - return decorator |
42 |
| - |
43 |
| - elif inspect.isclass(reason) or inspect.isfunction(reason): |
44 |
| - # The @deprecated is used without any 'reason'. |
45 |
| - # |
46 |
| - # .. code-block:: python |
47 |
| - # |
48 |
| - # @deprecated |
49 |
| - # def old_function(x, y): |
50 |
| - # pass |
51 |
| - |
52 |
| - func2 = reason |
53 |
| - |
54 |
| - if inspect.isclass(func2): |
55 |
| - fmt2 = f"Call to deprecated class {func2.__name__}." |
56 |
| - else: |
57 |
| - fmt2 = f"Call to deprecated function {func2.__name__}." |
58 |
| - |
59 |
| - @functools.wraps(func2) |
60 |
| - def new_func2(*args, **kwargs): |
61 |
| - warn_deprecation(fmt2) |
62 |
| - return func2(*args, **kwargs) |
63 |
| - |
64 |
| - return new_func2 |
65 |
| - |
66 |
| - else: |
67 |
| - raise TypeError(repr(type(reason))) |
| 4 | +def warn_deprecation(text: str): |
| 5 | + warn(text, category=DeprecationWarning, stacklevel=2) |
0 commit comments