Python 3.5+до 3.13typingпонятие
typing.io / typing.re
удалено
Псевдопакеты typing.io (IO, TextIO, BinaryIO) и typing.re (Pattern, Match). Удалены в Python 3.13 — импортируйте из typing или re.
Синтаксис
from typing.io import TextIO # до 3.13
Это были не настоящие модули, а пространства имён внутри typing: typing.io держал IO, TextIO и BinaryIO, typing.re — Pattern и Match. Их убрали, потому что те же имена всегда лежали прямо в typing и два пути к одному объекту только путали. Как переписать: from typing.io import TextIO меняется на from typing import TextIO (так же IO и BinaryIO); from typing.re import Pattern, Match — на from re import Pattern, Match: с Python 3.9 сами re.Pattern и re.Match пишутся в аннотациях как re.Pattern[str], а typing.Pattern и typing.Match устарели.
Примеры
# Python 3.13+
try:
from typing.io import TextIO
except ModuleNotFoundError as e:
print(type(e).__name__) # → ModuleNotFoundError
# Python 3.13+
import re
from typing import TextIO
def first_word(stream: TextIO, rx: re.Pattern[str]) -> str:
m = rx.search(stream.read())
return m.group() if m else ''
import io
print(first_word(io.StringIO('hello world'), re.compile(r'\w+'))) # → hello
import typing
from typing.io import TextIO
from typing.re import Pattern
print(TextIO is typing.TextIO, Pattern is typing.Pattern) # → True True
См. также
Частые ошибки
Открыть в полном глоссарииdocs.python.orgСообщить о неточности