Skip to content

Commit abdda5b

Browse files
srittauAlexWaygoodJelleZijlstra
authored
gh-92871: Remove typing.{io,re} namespaces (#92873)
Closes #92871 Co-authored-by: Alex Waygood <[email protected]> Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent 9dc476b commit abdda5b

File tree

5 files changed

+7
-76
lines changed

5 files changed

+7
-76
lines changed

Doc/library/typing.rst

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2074,10 +2074,6 @@ Other concrete types
20742074
represent the types of I/O streams such as returned by
20752075
:func:`open`.
20762076

2077-
.. deprecated-removed:: 3.8 3.13
2078-
The ``typing.io`` namespace is deprecated and will be removed.
2079-
These types should be directly imported from ``typing`` instead.
2080-
20812077
.. class:: Pattern
20822078
Match
20832079

@@ -2088,10 +2084,6 @@ Other concrete types
20882084
``Pattern[str]``, ``Pattern[bytes]``, ``Match[str]``, or
20892085
``Match[bytes]``.
20902086

2091-
.. deprecated-removed:: 3.8 3.13
2092-
The ``typing.re`` namespace is deprecated and will be removed.
2093-
These types should be directly imported from ``typing`` instead.
2094-
20952087
.. deprecated:: 3.9
20962088
Classes ``Pattern`` and ``Match`` from :mod:`re` now support ``[]``.
20972089
See :pep:`585` and :ref:`types-genericalias`.
@@ -2981,9 +2973,6 @@ convenience. This is subject to change, and not all deprecations are listed.
29812973
+----------------------------------+---------------+-------------------+----------------+
29822974
| Feature | Deprecated in | Projected removal | PEP/issue |
29832975
+==================================+===============+===================+================+
2984-
| ``typing.io`` and ``typing.re`` | 3.8 | 3.13 | :issue:`38291` |
2985-
| submodules | | | |
2986-
+----------------------------------+---------------+-------------------+----------------+
29872976
| ``typing`` versions of standard | 3.9 | Undecided | :pep:`585` |
29882977
| collections | | | |
29892978
+----------------------------------+---------------+-------------------+----------------+

Doc/whatsnew/3.13.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ Removed
107107
`Exscript <https://pypi.org/project/Exscript/>`_ instead.
108108
(Contributed by Victor Stinner in :gh:`104773`.)
109109

110+
* Namespaces ``typing.io`` and ``typing.re``, deprecated in Python 3.8,
111+
are now removed. The items in those namespaces can be imported directly
112+
from :mod:`typing`. (Contributed by Sebastian Rittau in :gh:`92871`.)
110113

111114
Porting to Python 3.13
112115
======================

Lib/test/test_typing.py

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7529,17 +7529,6 @@ def stuff(a: BinaryIO) -> bytes:
75297529
a = stuff.__annotations__['a']
75307530
self.assertEqual(a.__parameters__, ())
75317531

7532-
def test_io_submodule(self):
7533-
with warnings.catch_warnings(record=True) as w:
7534-
warnings.filterwarnings("default", category=DeprecationWarning)
7535-
from typing.io import IO, TextIO, BinaryIO, __all__, __name__
7536-
self.assertIs(IO, typing.IO)
7537-
self.assertIs(TextIO, typing.TextIO)
7538-
self.assertIs(BinaryIO, typing.BinaryIO)
7539-
self.assertEqual(set(__all__), set(['IO', 'TextIO', 'BinaryIO']))
7540-
self.assertEqual(__name__, 'typing.io')
7541-
self.assertEqual(len(w), 1)
7542-
75437532

75447533
class RETests(BaseTestCase):
75457534
# Much of this is really testing _TypeAlias.
@@ -7584,16 +7573,6 @@ def test_repr(self):
75847573
self.assertEqual(repr(Match[str]), 'typing.Match[str]')
75857574
self.assertEqual(repr(Match[bytes]), 'typing.Match[bytes]')
75867575

7587-
def test_re_submodule(self):
7588-
with warnings.catch_warnings(record=True) as w:
7589-
warnings.filterwarnings("default", category=DeprecationWarning)
7590-
from typing.re import Match, Pattern, __all__, __name__
7591-
self.assertIs(Match, typing.Match)
7592-
self.assertIs(Pattern, typing.Pattern)
7593-
self.assertEqual(set(__all__), set(['Match', 'Pattern']))
7594-
self.assertEqual(__name__, 'typing.re')
7595-
self.assertEqual(len(w), 1)
7596-
75977576
def test_cannot_subclass(self):
75987577
with self.assertRaisesRegex(
75997578
TypeError,
@@ -8765,7 +8744,7 @@ def test_all(self):
87658744
# Context managers.
87668745
self.assertIn('ContextManager', a)
87678746
self.assertIn('AsyncContextManager', a)
8768-
# Check that io and re are not exported.
8747+
# Check that former namespaces io and re are not exported.
87698748
self.assertNotIn('io', a)
87708749
self.assertNotIn('re', a)
87718750
# Spot-check that stdlib modules aren't exported.
@@ -8785,7 +8764,6 @@ def test_all_exported_names(self):
87858764
if k in actual_all or (
87868765
# avoid private names
87878766
not k.startswith('_') and
8788-
k not in {'io', 're'} and
87898767
# there's a few types and metaclasses that aren't exported
87908768
not k.endswith(('Meta', '_contra', '_co')) and
87918769
not k.upper() == k and

Lib/typing.py

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
no_type_check_decorator.
1717
* Generic aliases for collections.abc ABCs and few additional protocols.
1818
* Special types: NewType, NamedTuple, TypedDict.
19-
* Wrapper submodules for re and io related types.
2019
"""
2120

2221
from abc import abstractmethod, ABCMeta
@@ -27,7 +26,7 @@
2726
import contextlib
2827
import functools
2928
import operator
30-
import re as stdlib_re # Avoid confusion with the re we export.
29+
import re as stdlib_re # Avoid confusion with the typing.re namespace on <=3.11
3130
import sys
3231
import types
3332
import warnings
@@ -158,10 +157,6 @@
158157
'Unpack',
159158
]
160159

161-
# The pseudo-submodules 're' and 'io' are part of the public
162-
# namespace, but excluded from __all__ because they might stomp on
163-
# legitimate imports of those modules.
164-
165160

166161
def _type_convert(arg, module=None, *, allow_special_forms=False):
167162
"""For converting None to type(None), and strings to ForwardRef."""
@@ -3150,45 +3145,9 @@ def __enter__(self) -> 'TextIO':
31503145
pass
31513146

31523147

3153-
class _DeprecatedType(type):
3154-
def __getattribute__(cls, name):
3155-
if name not in ("__dict__", "__module__") and name in cls.__dict__:
3156-
warnings.warn(
3157-
f"{cls.__name__} is deprecated, import directly "
3158-
f"from typing instead. {cls.__name__} will be removed "
3159-
"in Python 3.12.",
3160-
DeprecationWarning,
3161-
stacklevel=2,
3162-
)
3163-
return super().__getattribute__(name)
3164-
3165-
3166-
class io(metaclass=_DeprecatedType):
3167-
"""Wrapper namespace for IO generic classes."""
3168-
3169-
__all__ = ['IO', 'TextIO', 'BinaryIO']
3170-
IO = IO
3171-
TextIO = TextIO
3172-
BinaryIO = BinaryIO
3173-
3174-
3175-
io.__name__ = __name__ + '.io'
3176-
sys.modules[io.__name__] = io
3177-
31783148
Pattern = _alias(stdlib_re.Pattern, 1)
31793149
Match = _alias(stdlib_re.Match, 1)
31803150

3181-
class re(metaclass=_DeprecatedType):
3182-
"""Wrapper namespace for re type aliases."""
3183-
3184-
__all__ = ['Pattern', 'Match']
3185-
Pattern = Pattern
3186-
Match = Match
3187-
3188-
3189-
re.__name__ = __name__ + '.re'
3190-
sys.modules[re.__name__] = re
3191-
31923151

31933152
def reveal_type[T](obj: T, /) -> T:
31943153
"""Reveal the inferred type of a variable.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Remove the ``typing.io`` and ``typing.re`` namespaces, deprecated since Python
2+
3.8. All items are still available from the main :mod:`typing` module.

0 commit comments

Comments
 (0)