8000 bpo-43766: Fix TypeGuard docs (#25660) · python/cpython@99fdd78 · GitHub
[go: up one dir, main page]

Skip to content

Commit 99fdd78

Browse files
bpo-43766: Fix TypeGuard docs (#25660)
1 parent 05ab4b6 commit 99fdd78

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

Doc/library/typing.rst

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -974,10 +974,10 @@ These can be used as types in annotations using ``[]``, each having a unique syn
974974

975975
def func1(val: List[object]):
976976
if is_str_list(val):
977-
# Type of ``val`` is narrowed to List[str]
977+
# Type of ``val`` is narrowed to ``List[str]``.
978978
print(" ".join(val))
979979
else:
980-
# Type of ``val`` remains as List[object]
980+
# Type of ``val`` remains as ``List[object]``.
981981
print("Not a list of strings!")
982982

983983
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
994994
wider form. The main reason is to allow for things like
995995
narrowing ``List[object]`` to ``List[str]`` even though the latter
996996
is not a subtype of the former, since ``List`` is invariant.
997-
The responsibility of
998-
writing type-safe type guards is left to the user. Even if
999-
the type guard function passes type checks, it may still fail at runtime.
1000-
The type guard function may perform erroneous checks and return wrong
1001-
booleans. Consequently, the type it promises in ``TypeGuard[TypeB]`` may
1002-
not hold.
997+
The responsibility of writing type-safe type guards is left to the user.
1003998

1004999
``TypeGuard`` also works with type variables. For more information, see
10051000
:pep:`647` (User-Defined Type Guards).

Doc/tools/susp-ignored.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,3 +370,7 @@ library/importlib.metadata,,:main,"EntryPoint(name='wheel', value='wheel.cli:mai
370370
library/importlib.metadata,,`,loading the metadata for packages for the indicated ``context``.
371371
library/re,,`,"`"
372372
using/configure,84,:db2,=db1:db2:...
373+
library/typing,1004,`,# Type of ``val`` is narrowed to ``str``
374+
library/typing,1004,`,"# Else, type of ``val`` is narrowed to ``float``."
375+
library/typing,1004,`,# Type of ``val`` is narrowed to ``List[str]``.
376+
library/typing,1004,`,# Type of ``val`` remains as ``List[object]``.

0 commit comments

Comments
 (0)
0