Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Deprecate mypy_extensions.NoReturn #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions mypy_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from mypy_extensions import TypedDict
"""

from typing import Any
from typing import Any, Dict

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


# Return type that indicates a function does not return
class NoReturn: pass
# Deprecated, use typing or typing_extensions variants instead
class _DEPRECATED_NoReturn: pass


def trait(cls):
Expand Down Expand Up @@ -226,3 +227,25 @@ def __new__(cls, x=0, base=_sentinel):
* isinstance(x, {name}) is the same as isinstance(x, int)
""".format(name=_int_type.__name__)
del _int_type


def _warn_deprecation(name: str, module_globals: Dict[str, Any]) -> Any:
if (val := module_globals.get(f"_DEPRECATED_{name}")) is None:
msg = f"module '{__name__}' has no attribute '{name}'"
raise AttributeError(msg)
module_globals[name] = val
if name in {"NoReturn"}:
msg = (
f"'mypy_extensions.{name}' is deprecated, "
"and will be removed in a future version. "
f"Use 'typing.{name}' or 'typing_extensions.{name}' instead"
)
else:
assert False, f"Add deprecation message for 'mypy_extensions.{name}'"
import warnings
warnings.warn(msg, DeprecationWarning, stacklevel=3)
return val


def __getattr__(name: str) -> Any:
return _warn_deprecation(name, module_globals=globals())
17 changes: 17 additions & 0 deletions tests/testextensions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import collections.abc
import pickle
import sys
import typing
import warnings
from contextlib import contextmanager
Expand Down Expand Up @@ -190,5 +191,21 @@ def assert_same(self, x, y):
assert x == y


class DeprecationTests(TestCase):
def test_no_return_deprecation(self):
del sys.modules["mypy_extensions"]
with self.assertWarnsRegex(
DeprecationWarning, "'mypy_extensions.NoReturn' is deprecated"
):
import mypy_extensions
mypy_extensions.NoReturn

del sys.modules["mypy_extensions"]
with self.assertWarnsRegex(
DeprecationWarning, "'mypy_extensions.NoReturn' is deprecated"
):
from mypy_extensions import NoReturn # noqa: F401


if __name__ == '__main__':
main()
Loading