mirror of
https://github.com/python/cpython.git
synced 2025-11-04 03:44:55 +00:00
bpo-43766: Fix TypeGuard docs (#25660)
This commit is contained in:
parent
05ab4b60ab
commit
99fdd78200
2 changed files with 7 additions and 8 deletions
|
|
@ -974,10 +974,10 @@ These can be used as types in annotations using ``[]``, each having a unique syn
|
||||||
|
|
||||||
def func1(val: List[object]):
|
def func1(val: List[object]):
|
||||||
if is_str_list(val):
|
if is_str_list(val):
|
||||||
# Type of ``val`` is narrowed to List[str]
|
# Type of ``val`` is narrowed to ``List[str]``.
|
||||||
print(" ".join(val))
|
print(" ".join(val))
|
||||||
else:
|
else:
|
||||||
# Type of ``val`` remains as List[object]
|
# Type of ``val`` remains as ``List[object]``.
|
||||||
print("Not a list of strings!")
|
print("Not a list of strings!")
|
||||||
|
|
||||||
If ``is_str_list`` is a class or instance method, then the type in
|
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
|
wider form. The main reason is to allow for things like
|
||||||
narrowing ``List[object]`` to ``List[str]`` even though the latter
|
narrowing ``List[object]`` to ``List[str]`` even though the latter
|
||||||
is not a subtype of the former, since ``List`` is invariant.
|
is not a subtype of the former, since ``List`` is invariant.
|
||||||
The responsibility of
|
The responsibility of writing type-safe type guards is left to the user.
|
||||||
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.
|
|
||||||
|
|
||||||
``TypeGuard`` also works with type variables. For more information, see
|
``TypeGuard`` also works with type variables. For more information, see
|
||||||
:pep:`647` (User-Defined Type Guards).
|
:pep:`647` (User-Defined Type Guards).
|
||||||
|
|
|
||||||
|
|
@ -370,3 +370,7 @@ library/importlib.metadata,,:main,"EntryPoint(name='wheel', value='wheel.cli:mai
|
||||||
library/importlib.metadata,,`,loading the metadata for packages for the indicated ``context``.
|
library/importlib.metadata,,`,loading the metadata for packages for the indicated ``context``.
|
||||||
library/re,,`,"`"
|
library/re,,`,"`"
|
||||||
using/configure,84,:db2,=db1:db2:...
|
using/configure,84,:db2,=db1:db2:...
|
||||||
|
library/typing,1004,`,# Type of ``val`` is narrowed to ``str``
|
||||||
|
library/typing,1004,`,"# Else, type of ``val`` is narrowed to ``float``."
|
||||||
|
library/typing,1004,`,# Type of ``val`` is narrowed to ``List[str]``.
|
||||||
|
library/typing,1004,`,# Type of ``val`` remains as ``List[object]``.
|
||||||
|
|
|
||||||
|
Can't render this file because it contains an unexpected character in line 348 and column 55.
|
Loading…
Add table
Add a link
Reference in a new issue