Skip to content

Commit 271a289

Browse files
authored
bpo-29974: Improve typing.TYPE_CHECKING example (GH-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. (cherry picked from commit 87c07fe)
1 parent c7b8367 commit 271a289

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
@@ -963,5 +963,10 @@ The module defines the following classes, functions and decorators:
963963
if TYPE_CHECKING:
964964
import expensive_mod
965965

966-
def fun():
967-
local_var: expensive_mod.some_type = other_fun()
966+
def fun(arg: 'expensive_mod.SomeType') -> None:
967+
local_var: expensive_mod.AnotherType = other_fun()
968+
969+
Note that the first type annotation must be enclosed in quotes, making it a
970+
"forward reference", to hide the ``expensive_mod`` reference from the
971+
interpreter runtime. Type annotations for local variables are not
972+
evaluated, so the second annotation does not need to be enclosed in quotes.

0 commit comments

Comments
 (0)