Skip to content

Commit d09177d

Browse files
committed
refactor: replace @deprecated decorator with upcoming native support (via typing-extensions)
1 parent 48678af commit d09177d

File tree

5 files changed

+6
-132
lines changed

5 files changed

+6
-132
lines changed

graphene/utils/deprecated.py

Lines changed: 3 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,5 @@
1-
import functools
2-
import inspect
3-
import warnings
1+
from warnings import warn
42

5-
string_types = (type(b""), type(""))
63

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)

graphene/utils/resolve_only_args.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from functools import wraps
2-
3-
from .deprecated import deprecated
2+
from typing_extensions import deprecated
43

54

65
@deprecated("This function is deprecated")
Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
from pytest import raises
2-
31
from .. import deprecated
4-
from ..deprecated import deprecated as deprecated_decorator
52
from ..deprecated import warn_deprecation
63

74

@@ -12,64 +9,3 @@ def test_warn_deprecation(mocker):
129
deprecated.warnings.warn.assert_called_with(
1310
"OH!", stacklevel=2, category=DeprecationWarning
1411
)
15-
16-
17-
def test_deprecated_decorator(mocker):
18-
mocker.patch.object(deprecated, "warn_deprecation")
19-
20-
@deprecated_decorator
21-
def my_func():
22-
return True
23-
24-
result = my_func()
25-
assert result
26-
deprecated.warn_deprecation.assert_called_with(
27-
"Call to deprecated function my_func."
28-
)
29-
30-
31-
def test_deprecated_class(mocker):
32-
mocker.patch.object(deprecated, "warn_deprecation")
33-
34-
@deprecated_decorator
35-
class X:
36-
pass
37-
38-
result = X()
39-
assert result
40-
deprecated.warn_deprecation.assert_called_with("Call to deprecated class X.")
41-
42-
43-
def test_deprecated_decorator_text(mocker):
44-
mocker.patch.object(deprecated, "warn_deprecation")
45-
46-
@deprecated_decorator("Deprecation text")
47-
def my_func():
48-
return True
49-
50-
result = my_func()
51-
assert result
52-
deprecated.warn_deprecation.assert_called_with(
53-
"Call to deprecated function my_func (Deprecation text)."
54-
)
55-
56-
57-
def test_deprecated_class_text(mocker):
58-
mocker.patch.object(deprecated, "warn_deprecation")
59-
60-
@deprecated_decorator("Deprecation text")
61-
class X:
62-
pass
63-
64-
result = X()
65-
assert result
66-
deprecated.warn_deprecation.assert_called_with(
67-
"Call to deprecated class X (Deprecation text)."
68-
)
69-
70-
71-
def test_deprecated_other_object(mocker):
72-
mocker.patch.object(deprecated, "warn_deprecation")
73-
74-
with raises(TypeError):
75-
deprecated_decorator({})

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ def run_tests(self):
8383
install_requires=[
8484
"graphql-core>=3.1,<3.3",
8585
"graphql-relay>=3.1,<3.3",
86+
"typing-extensions>=4.7.1,<5",
8687
],
8788
tests_require=tests_require,
8889
extras_require={"test": tests_require, "dev": dev_requires},

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ commands =
2020
[testenv:mypy]
2121
basepython = python3.10
2222
deps =
23-
mypy>=0.950,<1
23+
mypy>=1.10,<2
2424
commands =
2525
mypy graphene
2626

0 commit comments

Comments
 (0)