Skip to content

Commit 400534f

Browse files
authored
Deprecate mypy_extensions.NoReturn (#56)
1 parent 23fbfa5 commit 400534f

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

mypy_extensions.py

+25-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from mypy_extensions import TypedDict
66
"""
77

8-
from typing import Any
8+
from typing import Any, Dict
99

1010
import sys
1111
# _type_check is NOT a part of public typing API, it is used here only to mimic
@@ -150,7 +150,8 @@ def KwArg(type=Any):
150150

151151

152152
# Return type that indicates a function does not return
153-
class NoReturn: pass
153+
# Deprecated, use typing or typing_extensions variants instead
154+
class _DEPRECATED_NoReturn: pass
154155

155156

156157
def trait(cls):
@@ -226,3 +227,25 @@ def __new__(cls, x=0, base=_sentinel):
226227
* isinstance(x, {name}) is the same as isinstance(x, int)
227228
""".format(name=_int_type.__name__)
228229
del _int_type
230+
231+
232+
def _warn_deprecation(name: str, module_globals: Dict[str, Any]) -> Any:
233+
if (val := module_globals.get(f"_DEPRECATED_{name}")) is None:
234+
msg = f"module '{__name__}' has no attribute '{name}'"
235+
raise AttributeError(msg)
236+
module_globals[name] = val
237+
if name in {"NoReturn"}:
238+
msg = (
239+
f"'mypy_extensions.{name}' is deprecated, "
240+
"and will be removed in a future version. "
241+
f"Use 'typing.{name}' or 'typing_extensions.{name}' instead"
242+
)
243+
else:
244+
assert False, f"Add deprecation message for 'mypy_extensions.{name}'"
245+
import warnings
246+
warnings.warn(msg, DeprecationWarning, stacklevel=3)
247+
return val
248+
249+
250+
def __getattr__(name: str) -> Any:
251+
return _warn_deprecation(name, module_globals=globals())

tests/testextensions.py

+17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import collections.abc
22
import pickle
3+
import sys
34
import typing
45
import warnings
56
from contextlib import contextmanager
@@ -190,5 +191,21 @@ def assert_same(self, x, y):
190191
assert x == y
191192

192193

194+
class DeprecationTests(TestCase):
195+
def test_no_return_deprecation(self):
196+
del sys.modules["mypy_extensions"]
197+
with self.assertWarnsRegex(
198+
DeprecationWarning, "'mypy_extensions.NoReturn' is deprecated"
199+
):
200+
import mypy_extensions
201+
mypy_extensions.NoReturn
202+
203+
del sys.modules["mypy_extensions"]
204+
with self.assertWarnsRegex(
205+
DeprecationWarning, "'mypy_extensions.NoReturn' is deprecated"
206+
):
207+
from mypy_extensions import NoReturn # noqa: F401
208+
209+
193210
if __name__ == '__main__':
194211
main()

0 commit comments

Comments
 (0)