从 3.9 版起弃用: Pattern and Match from re 现在支持 [] 。见 PEP 585 and 一般别名类型 .

class typing. 文本

Deprecated alias for str .

Text is provided to supply a forward compatible path for Python 2 code: in Python 2, Text 是别名化的 unicode .

使用 Text to indicate that a value must contain a unicode string in a manner that is compatible with both Python 2 and Python 3:

def add_unicode_checkmark(text: Text) -> Text:
    return text + u' \u2713'
											

Added in version 3.5.2.

Deprecated since version 3.11: Python 2 is no longer supported, and most type checkers also no longer support type checking Python 2 code. Removal of the alias is not currently planned, but users are encouraged to use str 而不是 Text .

Aliases to container ABCs in collections.abc

class typing. AbstractSet ( Collection[T_co] )

Deprecated alias to collections.abc.Set .

从 3.9 版起弃用: collections.abc.Set now supports subscripting ( [] )。见 PEP 585 and 一般别名类型 .

class typing. ByteString ( Sequence[int] )

此类型表示类型 bytes , bytearray ,和 memoryview of byte sequences.

Deprecated since version 3.9, will be removed in version 3.14: Prefer collections.abc.Buffer , or a union like bytes | bytearray | memoryview .

class typing. Collection ( Sized, Iterable[T_co], Container[T_co] )

Deprecated alias to collections.abc.Collection .

Added in version 3.6.

从 3.9 版起弃用: collections.abc.Collection now supports subscripting ( [] )。见 PEP 585 and 一般别名类型 .

class typing. Container ( Generic[T_co] )

Deprecated alias to collections.abc.Container .

从 3.9 版起弃用: collections.abc.Container now supports subscripting ( [] )。见 PEP 585 and 一般别名类型 .

class typing. ItemsView ( MappingView, AbstractSet[tuple[KT_co, VT_co]] )

Deprecated alias to collections.abc.ItemsView .

从 3.9 版起弃用: collections.abc.ItemsView now supports subscripting ( [] )。见 PEP 585 and 一般别名类型 .

class typing. KeysView ( MappingView, AbstractSet[KT_co] )

Deprecated alias to collections.abc.KeysView .

从 3.9 版起弃用: collections.abc.KeysView now supports subscripting ( [] )。见 PEP 585 and 一般别名类型 .

class typing. 映射 ( Collection[KT], Generic[KT, VT_co] )

Deprecated alias to collections.abc.Mapping .

此类型可以用于以下:

def get_position_in_index(word_list: Mapping[str, int], word: str) -> int:
    return word_list[word]
																

从 3.9 版起弃用: collections.abc.Mapping now supports subscripting ( [] )。见 PEP 585 and 一般别名类型 .

class typing. MappingView ( Sized )

Deprecated alias to collections.abc.MappingView .

从 3.9 版起弃用: collections.abc.MappingView now supports subscripting ( [] )。见 PEP 585 and 一般别名类型 .

class typing. MutableMapping ( Mapping[KT, VT] )

Deprecated alias to collections.abc.MutableMapping .

从 3.9 版起弃用: collections.abc.MutableMapping now supports subscripting ( [] )。见 PEP 585 and 一般别名类型 .

class typing. MutableSequence ( Sequence[T] )

Deprecated alias to collections.abc.MutableSequence .

从 3.9 版起弃用: collections.abc.MutableSequence now supports subscripting ( [] )。见 PEP 585 and 一般别名类型 .

class typing. MutableSet ( AbstractSet[T] )

Deprecated alias to collections.abc.MutableSet .

从 3.9 版起弃用: collections.abc.MutableSet now supports subscripting ( [] )。见 PEP 585 and 一般别名类型 .

class typing. Sequence ( Reversible[T_co], Collection[T_co] )

Deprecated alias to collections.abc.Sequence .

从 3.9 版起弃用: collections.abc.Sequence now supports subscripting ( [] )。见 PEP 585 and 一般别名类型 .

class typing. ValuesView ( MappingView, Collection[_VT_co] )

Deprecated alias to collections.abc.ValuesView .

从 3.9 版起弃用: collections.abc.ValuesView now supports subscripting ( [] )。见 PEP 585 and 一般别名类型 .

Aliases to asynchronous ABCs in collections.abc

class typing. Coroutine ( Awaitable[ReturnType], Generic[YieldType, SendType, ReturnType] )

Deprecated alias to collections.abc.Coroutine .

The variance and order of type variables correspond to those of Generator ,例如:

from collections.abc import Coroutine
c: Coroutine[list[str], str, int]  # Some coroutine defined elsewhere
x = c.send('hi')                   # Inferred type of 'x' is list[str]
async def bar() -> None:
    y = await c                    # Inferred type of 'y' is int
																					

Added in version 3.5.3.

从 3.9 版起弃用: collections.abc.Coroutine now supports subscripting ( [] )。见 PEP 585 and 一般别名类型 .

class typing. AsyncGenerator ( AsyncIterator[YieldType], Generic[YieldType, SendType] )

Deprecated alias to collections.abc.AsyncGenerator .

An async generator can be annotated by the generic type AsyncGenerator[YieldType, SendType] 。例如:

async def echo_round() -> AsyncGenerator[int, float]:
    sent = yield 0
    while sent >= 0.0:
        rounded = await round(sent)
        sent = yield rounded
																					

Unlike normal generators, async generators cannot return a value, so there is no ReturnType type parameter. As with Generator SendType behaves contravariantly.

If your generator will only yield values, set the SendType to None :

async def infinite_stream(start: int) -> AsyncGenerator[int, None]:
    while True:
        yield start
        start = await increment(start)
																					

Alternatively, annotate your generator as having a return type of either AsyncIterable[YieldType] or AsyncIterator[YieldType] :

async def infinite_stream(start: int) -> AsyncIterator[int]:
    while True:
        yield start
        start = await increment(start)
																					

Added in version 3.6.1.

从 3.9 版起弃用: collections.abc.AsyncGenerator now supports subscripting ( [] )。见 PEP 585 and 一般别名类型 .

class typing. AsyncIterable ( Generic[T_co] )

Deprecated alias to collections.abc.AsyncIterable .

Added in version 3.5.2.

从 3.9 版起弃用: collections.abc.AsyncIterable now supports subscripting ( [] )。见 PEP 585 and 一般别名类型 .

class typing. AsyncIterator ( AsyncIterable[T_co] )

Deprecated alias to collections.abc.AsyncIterator .

Added in version 3.5.2.

从 3.9 版起弃用: collections.abc.AsyncIterator now supports subscripting ( [] )。见 PEP 585 and 一般别名类型 .

class typing. Awaitable ( Generic[T_co] )

Deprecated alias to collections.abc.Awaitable .

Added in version 3.5.2.

从 3.9 版起弃用: collections.abc.Awaitable now supports subscripting ( [] )。见 PEP 585 and 一般别名类型 .

Aliases to other ABCs in collections.abc

class typing. Iterable ( Generic[T_co] )

Deprecated alias to collections.abc.Iterable .

从 3.9 版起弃用: collections.abc.Iterable now supports subscripting ( [] )。见 PEP 585 and 一般别名类型 .

class typing. Iterator ( Iterable[T_co] )

Deprecated alias to collections.abc.Iterator .

从 3.9 版起弃用: collections.abc.Iterator now supports subscripting ( [] )。见 PEP 585 and 一般别名类型 .

typing. 可调用

Deprecated alias to collections.abc.Callable .

Annotating callable objects for details on how to use collections.abc.Callable and typing.Callable in type annotations.

从 3.9 版起弃用: collections.abc.Callable now supports subscripting ( [] )。见 PEP 585 and 一般别名类型 .

3.10 版改变: Callable 现在支持 ParamSpec and Concatenate 。见 PEP 612 了解更多细节。

class typing. 生成器 ( Iterator[YieldType], Generic[YieldType, SendType, ReturnType] )

Deprecated alias to collections.abc.Generator .

A generator can be annotated by the generic type Generator[YieldType, SendType, ReturnType] 。例如:

def echo_round() -> Generator[int, float, str]:
    sent = yield 0
    while sent >= 0:
        sent = yield round(sent)
    return 'Done'
																						

Note that unlike many other generics in the typing module, the SendType of Generator behaves contravariantly, not covariantly or invariantly.

If your generator will only yield values, set the SendType and ReturnType to None :

def infinite_stream(start: int) -> Generator[int, None, None]:
    while True:
        yield start
        start += 1
																						

Alternatively, annotate your generator as having a return type of either Iterable[YieldType] or Iterator[YieldType] :

def infinite_stream(start: int) -> Iterator[int]:
    while True:
        yield start
        start += 1
																						

从 3.9 版起弃用: collections.abc.Generator now supports subscripting ( [] )。见 PEP 585 and 一般别名类型 .

class typing. Hashable

Deprecated alias to collections.abc.Hashable .

Deprecated since version 3.12: 使用 collections.abc.Hashable directly instead.

class typing. Reversible ( Iterable[T_co] )

Deprecated alias to collections.abc.Reversible .

从 3.9 版起弃用: collections.abc.Reversible now supports subscripting ( [] )。见 PEP 585 and 一般别名类型 .

class typing. Sized

Deprecated alias to collections.abc.Sized .

Deprecated since version 3.12: 使用 collections.abc.Sized directly instead.

Aliases to contextlib ABCs

class typing. ContextManager ( Generic[T_co] )

Deprecated alias to contextlib.AbstractContextManager .

Added in version 3.5.4.

从 3.9 版起弃用: contextlib.AbstractContextManager now supports subscripting ( [] )。见 PEP 585 and 一般别名类型 .

class typing. AsyncContextManager ( Generic[T_co] )

Deprecated alias to contextlib.AbstractAsyncContextManager .

Added in version 3.6.2.

从 3.9 版起弃用: contextlib.AbstractAsyncContextManager now supports subscripting ( [] )。见 PEP 585 and 一般别名类型 .

主要特征弃用时间线

Certain features in typing are deprecated and may be removed in a future version of Python. The following table summarizes major deprecations for your convenience. This is subject to change, and not all deprecations are listed.

特征

Deprecated in

Projected removal

PEP/issue

typing.io and typing.re submodules

3.8 3.13

bpo-38291

typing versions of standard collections

3.9

Undecided (see Deprecated aliases for more information)

PEP 585

typing.ByteString

3.9 3.14

gh-91896

typing.Text

3.11 Undecided

gh-92332

typing.Hashable and typing.Sized

3.12 Undecided

gh-94309

typing.TypeAlias

3.12 Undecided

PEP 695

内容表

上一话题

开发工具

下一话题

pydoc — 文档编制生成器和在线帮助系统

本页