diff options
author | Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com> | 2021-04-27 15:55:08 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-27 15:55:08 (GMT) |
commit | 99fdd782007db86f20aeb302b2ceaf79ce1ae2ba (patch) | |
tree | 754dac60653b602e84443ca0a4fdd054f82cce0d /Doc/library | |
parent | 05ab4b60ab3dae61ee75692b6624537d4f3fdf85 (diff) | |
download | cpython-99fdd782007db86f20aeb302b2ceaf79ce1ae2ba.zip cpython-99fdd782007db86f20aeb302b2ceaf79ce1ae2ba.tar.gz cpython-99fdd782007db86f20aeb302b2ceaf79ce1ae2ba.tar.bz2 |
bpo-43766: Fix TypeGuard docs (#25660)
Diffstat (limited to 'Doc/library')
-rw-r--r-- | Doc/library/typing.rst | 11 |
1 files changed, 3 insertions, 8 deletions
diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index cb9ba45..8b1ce34 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -974,10 +974,10 @@ These can be used as types in annotations using ``[]``, each having a unique syn def func1(val: List[object]): if is_str_list(val): - # Type of ``val`` is narrowed to List[str] + # Type of ``val`` is narrowed to ``List[str]``. print(" ".join(val)) else: - # Type of ``val`` remains as List[object] + # Type of ``val`` remains as ``List[object]``. print("Not a list of strings!") If ``is_str_list`` is a class or instance method, then the type in @@ -994,12 +994,7 @@ These can be used as types in annotations using ``[]``, each having a unique syn wider form. The main reason is to allow for things like narrowing ``List[object]`` to ``List[str]`` even though the latter is not a subtype of the former, since ``List`` is invariant. - The responsibility of - writing type-safe type guards is left to the user. Even if - the type guard function passes type checks, it may still fail at runtime. - The type guard function may perform erroneous checks and return wrong - booleans. Consequently, the type it promises in ``TypeGuard[TypeB]`` may - not hold. + The responsibility of writing type-safe type guards is left to the user. ``TypeGuard`` also works with type variables. For more information, see :pep:`647` (User-Defined Type Guards). |