@@ -12,11 +12,10 @@ runtime. For example, here the variable *name* can be either a ``str`` or
1212 print("Hello, " + name)
1313
1414This technique is called *type narrowing *.
15- To avoid false positives on such code, type checkers understand
16- various kinds of conditional checks that are used to narrow types in Python code.
17- The exact set of type narrowing constructs that a type checker understands
18- is not specified and varies across type checkers. Commonly understood
19- patterns include:
15+ To avoid false positives, type checkers recognize various kinds of
16+ conditional checks that narrow types in Python code. The exact set of
17+ supported constructs is not specified and varies across type checkers.
18+ Commonly understood patterns include:
2019
2120* ``if x is not None ``
2221* ``if x ``
@@ -31,13 +30,13 @@ conditions for this behavior differ between type checkers.
3130Consult your type checker's documentation for more information on the type
3231narrowing constructs it supports.
3332
34- The type system also includes two ways to create *user-defined * type narrowing
35- functions: :py:data: `typing.TypeIs ` and :py:data: `typing.TypeGuard `. These
36- are useful if you want to reuse a more complicated check in multiple places, or
37- you use a check that the type checker doesn't understand. In these cases, you
38- can define a ``TypeIs `` or ``TypeGuard `` function to perform the check and allow type checkers
39- to use it to narrow the type of a variable. Between the two, `` TypeIs `` usually
40- has the more intuitive behavior, so we'll talk about it more ; see
33+ The type system also provides two ways to define *user-defined * type narrowing
34+ functions: :py:data: `typing.TypeIs ` and :py:data: `typing.TypeGuard `. These are
35+ useful when you want to reuse a more complex check in multiple places or when
36+ the type checker does not understand a particular check . In such cases, you can
37+ define a ``TypeIs `` or ``TypeGuard `` function to perform the check and allow
38+ type checkers to use it to narrow the type of a variable. Between the two,
39+ `` TypeIs `` usually has more intuitive behavior, so we focus on it here ; see
4140:ref: `below <guide-type-narrowing-typeis-typeguard >` for a comparison.
4241
4342How to use ``TypeIs `` and ``TypeGuard ``
@@ -63,7 +62,7 @@ For example::
6362 print(f"{x} is not a cardinal direction")
6463
6564A ``TypeGuard `` function looks similar and is used in the same way, but the
66- type narrowing behavior is different, as dicussed in :ref: `the section below <guide-type-narrowing-typeis-typeguard >`.
65+ type narrowing behavior is different, as discussed in :ref: `the section below <guide-type-narrowing-typeis-typeguard >`.
6766
6867Depending on the version of Python you are running, you will be able to
6968import ``TypeIs `` and ``TypeGuard `` either from the standard library :py:mod: `typing `
@@ -143,7 +142,7 @@ Here is an example of a correct ``TypeIs`` function for a more complicated type:
143142:py:data: `typing.TypeIs ` and :py:data: `typing.TypeGuard ` are both tools for narrowing the type of a variable
144143based on a user-defined function. Both can be used to annotate functions that take an
145144argument and return a boolean depending on whether the input argument is compatible with
146- the narrowed type. These function can then be used in ``if `` checks to narrow the type
145+ the narrowed type. These functions can then be used in ``if `` checks to narrow the type
147146of a variable.
148147
149148``TypeIs `` usually has the more intuitive behavior, but it
0 commit comments