Skip to content

Commit 87c07fe

Browse files
Mortalberkerpeksag
authored andcommitted
bpo-29974: Improve typing.TYPE_CHECKING example (pythonGH-982)
* Fix PEP 8 (SomeType instead of some_type) * Add a function parameter annotation * Explain, using wording from PEP 484 and PEP 526, why one annotation is in quotes and another is not. Suggested by Ivan Levkevskyi.
1 parent 0d637e2 commit 87c07fe

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

Doc/library/typing.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,5 +1041,10 @@ The module defines the following classes, functions and decorators:
10411041
if TYPE_CHECKING:
10421042
import expensive_mod
10431043

1044-
def fun():
1045-
local_var: expensive_mod.some_type = other_fun()
1044+
def fun(arg: 'expensive_mod.SomeType') -> None:
1045+
local_var: expensive_mod.AnotherType = other_fun()
1046+
1047+
Note that the first type annotation must be enclosed in quotes, making it a
1048+
"forward reference", to hide the ``expensive_mod`` reference from the
1049+
interpreter runtime. Type annotations for local variables are not
1050+
evaluated, so the second annotation does not need to be enclosed in quotes.

0 commit comments

Comments
 (0)