Skip to content

Commit 09eb817

Browse files
authored
bpo-38291: DeprecationWarning when importing typing.{io,re} (#26719)
1 parent 2918481 commit 09eb817

File tree

4 files changed

+35
-16
lines changed

4 files changed

+35
-16
lines changed

Lib/importlib/resources.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
from pathlib import Path
1212
from types import ModuleType
1313
from typing import ContextManager, Iterable, Union
14-
from typing import cast
15-
from typing.io import BinaryIO, TextIO
14+
from typing import cast, BinaryIO, TextIO
1615
from collections.abc import Sequence
1716
from functools import singledispatch
1817

Lib/test/test_typing.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pickle
44
import re
55
import sys
6+
import warnings
67
from unittest import TestCase, main, skipUnless, skip
78
from copy import copy, deepcopy
89

@@ -1976,7 +1977,7 @@ def test_weakref_all(self):
19761977
T = TypeVar('T')
19771978
things = [Any, Union[T, int], Callable[..., T], Tuple[Any, Any],
19781979
Optional[List[int]], typing.Mapping[int, str],
1979-
typing.re.Match[bytes], typing.Iterable['whatever']]
1980+
typing.Match[bytes], typing.Iterable['whatever']]
19801981
for t in things:
19811982
self.assertEqual(weakref.ref(t)(), t)
19821983

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

39983999
def test_io_submodule(self):
3999-
from typing.io import IO, TextIO, BinaryIO, __all__, __name__
4000-
self.assertIs(IO, typing.IO)
4001-
self.assertIs(TextIO, typing.TextIO)
4002-
self.assertIs(BinaryIO, typing.BinaryIO)
4003-
self.assertEqual(set(__all__), set(['IO', 'TextIO', 'BinaryIO']))
4004-
self.assertEqual(__name__, 'typing.io')
4000+
with warnings.catch_warnings(record=True) as w:
4001+
from typing.io import IO, TextIO, BinaryIO, __all__, __name__
4002+
self.assertIs(IO, typing.IO)
4003+
self.assertIs(TextIO, typing.TextIO)
4004+
self.assertIs(BinaryIO, typing.BinaryIO)
4005+
self.assertEqual(set(__all__), set(['IO', 'TextIO', 'BinaryIO']))
4006+
self.assertEqual(__name__, 'typing.io')
4007+
self.assertEqual(len(w), 1)
40054008

40064009

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

40504053
def test_re_submodule(self):
4051-
from typing.re import Match, Pattern, __all__, __name__
4052-
self.assertIs(Match, typing.Match)
4053-
self.assertIs(Pattern, typing.Pattern)
4054-
self.assertEqual(set(__all__), set(['Match', 'Pattern']))
4055-
self.assertEqual(__name__, 'typing.re')
4054+
with warnings.catch_warnings(record=True) as w:
4055+
from typing.re import Match, Pattern, __all__, __name__
4056+
self.assertIs(Match, typing.Match)
4057+
self.assertIs(Pattern, typing.Pattern)
4058+
self.assertEqual(set(__all__), set(['Match', 'Pattern']))
4059+
self.assertEqual(__name__, 'typing.re')
4060+
self.assertEqual(len(w), 1)
40564061

40574062
def test_cannot_subclass(self):
40584063
with self.assertRaises(TypeError) as ex:

Lib/typing.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import re as stdlib_re # Avoid confusion with the re we export.
2929
import sys
3030
import types
31+
import warnings
3132
from types import WrapperDescriptorType, MethodWrapperType, MethodDescriptorType, GenericAlias
3233

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

25142515

2515-
class io:
2516+
class _DeprecatedType(type):
2517+
def __getattribute__(cls, name):
2518+
if name != "__dict__" and name in cls.__dict__:
2519+
warnings.warn(
2520+
f"{cls.__name__} is deprecated, import directly "
2521+
f"from typing instead. {cls.__name__} will be removed "
2522+
"in Python 3.12.",
2523+
DeprecationWarning,
2524+
stacklevel=2,
2525+
)
2526+
return super().__getattribute__(name)
2527+
2528+
2529+
class io(metaclass=_DeprecatedType):
25162530
"""Wrapper namespace for IO generic classes."""
25172531

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

2530-
class re:
2544+
class re(metaclass=_DeprecatedType):
25312545
"""Wrapper namespace for re type aliases."""
25322546

25332547
__all__ = ['Pattern', 'Match']
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Importing typing.io or typing.re now prints a `DeprecationWarning`.

0 commit comments

Comments
 (0)