Skip to content

Commit

Permalink
Remove typing.{io,re} namespaces
Browse files Browse the repository at this point in the history
Closes: python#92871
  • Loading branch information
srittau committed May 17, 2022
1 parent 702e0da commit 149e383
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 75 deletions.
11 changes: 0 additions & 11 deletions Doc/library/typing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1940,10 +1940,6 @@ Other concrete types
represent the types of I/O streams such as returned by
:func:`open`.

.. deprecated-removed:: 3.8 3.12
The ``typing.io`` namespace is deprecated and will be removed.
These types should be directly imported from ``typing`` instead.

.. class:: Pattern
Match

Expand All @@ -1954,10 +1950,6 @@ Other concrete types
``Pattern[str]``, ``Pattern[bytes]``, ``Match[str]``, or
``Match[bytes]``.

.. deprecated-removed:: 3.8 3.12
The ``typing.re`` namespace is deprecated and will be removed.
These types should be directly imported from ``typing`` instead.

.. deprecated:: 3.9
Classes ``Pattern`` and ``Match`` from :mod:`re` now support ``[]``.
See :pep:`585` and :ref:`types-genericalias`.
Expand Down Expand Up @@ -2687,9 +2679,6 @@ convenience. This is subject to change, and not all deprecations are listed.
+----------------------------------+---------------+-------------------+----------------+
| Feature | Deprecated in | Projected removal | PEP/issue |
+==================================+===============+===================+================+
| ``typing.io`` and ``typing.re`` | 3.8 | 3.12 | :issue:`38291` |
| submodules | | | |
+----------------------------------+---------------+-------------------+----------------+
| ``typing`` versions of standard | 3.9 | Undecided | :pep:`585` |
| collections | | | |
+----------------------------------+---------------+-------------------+----------------+
Expand Down
3 changes: 3 additions & 0 deletions Doc/whatsnew/3.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ Removed

(Contributed by Erlend E. Aasland in :gh:`92548`)

* Namespaces ``typing.io`` and ``typing.re``, deprecated in Python 3.8,
are now removed. The items in those namespaces can be imported directly
from ``typing``.

Porting to Python 3.12
======================
Expand Down
24 changes: 2 additions & 22 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6297,17 +6297,6 @@ def stuff(a: BinaryIO) -> bytes:
a = stuff.__annotations__['a']
self.assertEqual(a.__parameters__, ())

def test_io_submodule(self):
with warnings.catch_warnings(record=True) as w:
warnings.filterwarnings("default", category=DeprecationWarning)
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):
# Much of this is really testing _TypeAlias.
Expand Down Expand Up @@ -6352,16 +6341,6 @@ def test_repr(self):
self.assertEqual(repr(Match[str]), 'typing.Match[str]')
self.assertEqual(repr(Match[bytes]), 'typing.Match[bytes]')

def test_re_submodule(self):
with warnings.catch_warnings(record=True) as w:
warnings.filterwarnings("default", category=DeprecationWarning)
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 Expand Up @@ -7258,7 +7237,7 @@ def test_all(self):
# Context managers.
self.assertIn('ContextManager', a)
self.assertIn('AsyncContextManager', a)
# Check that io and re are not exported.
# Check that former namespaces io and re are not exported.
self.assertNotIn('io', a)
self.assertNotIn('re', a)
# Spot-check that stdlib modules aren't exported.
Expand All @@ -7278,6 +7257,7 @@ def test_all_exported_names(self):
if k in actual_all or (
# avoid private names
not k.startswith('_') and
# former namespaces
k not in {'io', 're'} and
# there's a few types and metaclasses that aren't exported
not k.endswith(('Meta', '_contra', '_co')) and
Expand Down
43 changes: 1 addition & 42 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
no_type_check_decorator.
* Generic aliases for collections.abc ABCs and few additional protocols.
* Special types: NewType, NamedTuple, TypedDict.
* Wrapper submodules for re and io related types.
"""

from abc import abstractmethod, ABCMeta
Expand All @@ -26,7 +25,7 @@
import contextlib
import functools
import operator
import re as stdlib_re # Avoid confusion with the re we export.
import re as stdlib_re # Avoid confusion with the former re re-export.
import sys
import types
import warnings
Expand Down Expand Up @@ -151,10 +150,6 @@ def _idfunc(_, x):
'Unpack',
]

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


def _type_convert(arg, module=None, *, allow_special_forms=False):
"""For converting None to type(None), and strings to ForwardRef."""
Expand Down Expand Up @@ -3295,45 +3290,9 @@ def __enter__(self) -> 'TextIO':
pass


class _DeprecatedType(type):
def __getattribute__(cls, name):
if name not in ("__dict__", "__module__") 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']
IO = IO
TextIO = TextIO
BinaryIO = BinaryIO


io.__name__ = __name__ + '.io'
sys.modules[io.__name__] = io

Pattern = _alias(stdlib_re.Pattern, 1)
Match = _alias(stdlib_re.Match, 1)

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

__all__ = ['Pattern', 'Match']
Pattern = Pattern
Match = Match


re.__name__ = __name__ + '.re'
sys.modules[re.__name__] = re


def reveal_type(obj: T, /) -> T:
"""Reveal the inferred type of a variable.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Remove the ``typing.io`` and ``typing.re`` namespaces, deprecated in Python
3.8. All items are still available from the main ``typing`` module.

0 comments on commit 149e383

Please sign in to comment.