Skip to content
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

bpo-38291: DeprecationWarning when importing typing.{io,re} #26719

Merged
merged 6 commits into from
Jun 19, 2021
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
3 changes: 1 addition & 2 deletions Lib/importlib/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
from pathlib import Path
from types import ModuleType
from typing import ContextManager, Iterable, Union
from typing import cast
from typing.io import BinaryIO, TextIO
from typing import cast, BinaryIO, TextIO
from collections.abc import Sequence
from functools import singledispatch

Expand Down
29 changes: 17 additions & 12 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pickle
import re
import sys
import warnings
from unittest import TestCase, main, skipUnless, skip
from copy import copy, deepcopy

Expand Down Expand Up @@ -1976,7 +1977,7 @@ def test_weakref_all(self):
T = TypeVar('T')
things = [Any, Union[T, int], Callable[..., T], Tuple[Any, Any],
Optional[List[int]], typing.Mapping[int, str],
typing.re.Match[bytes], typing.Iterable['whatever']]
typing.Match[bytes], typing.Iterable['whatever']]
for t in things:
self.assertEqual(weakref.ref(t)(), t)

Expand Down Expand Up @@ -3996,12 +3997,14 @@ def stuff(a: BinaryIO) -> bytes:
self.assertEqual(a.__parameters__, ())

def test_io_submodule(self):
from typing.io import IO, TextIO, BinaryIO, __all__, __name__
self.assertIs(IO, typing.IO)
self.assertIs(TextIO, typing.TextIO)
self.assertIs(BinaryIO, typing.BinaryIO)
self.assertEqual(set(__all__), set(['IO', 'TextIO', 'BinaryIO']))
self.assertEqual(__name__, 'typing.io')
with warnings.catch_warnings(record=True) as w:
from typing.io import IO, TextIO, BinaryIO, __all__, __name__
self.assertIs(IO, typing.IO)
self.assertIs(TextIO, typing.TextIO)
self.assertIs(BinaryIO, typing.BinaryIO)
self.assertEqual(set(__all__), set(['IO', 'TextIO', 'BinaryIO']))
self.assertEqual(__name__, 'typing.io')
self.assertEqual(len(w), 1)


class RETests(BaseTestCase):
Expand Down Expand Up @@ -4048,11 +4051,13 @@ def test_repr(self):
self.assertEqual(repr(Match[bytes]), 'typing.Match[bytes]')

def test_re_submodule(self):
from typing.re import Match, Pattern, __all__, __name__
self.assertIs(Match, typing.Match)
self.assertIs(Pattern, typing.Pattern)
self.assertEqual(set(__all__), set(['Match', 'Pattern']))
self.assertEqual(__name__, 'typing.re')
with warnings.catch_warnings(record=True) as w:
from typing.re import Match, Pattern, __all__, __name__
self.assertIs(Match, typing.Match)
self.assertIs(Pattern, typing.Pattern)
self.assertEqual(set(__all__), set(['Match', 'Pattern']))
self.assertEqual(__name__, 'typing.re')
self.assertEqual(len(w), 1)

def test_cannot_subclass(self):
with self.assertRaises(TypeError) as ex:
Expand Down
18 changes: 16 additions & 2 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import re as stdlib_re # Avoid confusion with the re we export.
import sys
import types
import warnings
from types import WrapperDescriptorType, MethodWrapperType, MethodDescriptorType, GenericAlias

# Please keep __all__ alphabetized within each category.
Expand Down Expand Up @@ -2512,7 +2513,20 @@ def __enter__(self) -> 'TextIO':
pass


class io:
class _DeprecatedType(type):
def __getattribute__(cls, name):
if name != "__dict__" and name in cls.__dict__:
warnings.warn(
f"{cls.__name__} is deprecated, import directly "
f"from typing instead. {cls.__name__} will be removed "
"in Python 3.12.",
DeprecationWarning,
stacklevel=2,
)
return super().__getattribute__(name)


class io(metaclass=_DeprecatedType):
"""Wrapper namespace for IO generic classes."""

__all__ = ['IO', 'TextIO', 'BinaryIO']
Expand All @@ -2527,7 +2541,7 @@ class io:
Pattern = _alias(stdlib_re.Pattern, 1)
Match = _alias(stdlib_re.Match, 1)

class re:
class re(metaclass=_DeprecatedType):
"""Wrapper namespace for re type aliases."""

__all__ = ['Pattern', 'Match']
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Importing typing.io or typing.re now prints a `DeprecationWarning`.