From: Mathias Rav Date: Wed, 26 Apr 2017 10:49:45 +0000 (-0400) Subject: bpo-29974: Improve typing.TYPE_CHECKING example (GH-982) X-Git-Tag: v3.7.0a1~900 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=87c07fe9d908d0a2143fcc8369255c6ff3241503;p=python 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. --- diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index 2cfc37f695..1780739ad1 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -1041,5 +1041,10 @@ The module defines the following classes, functions and decorators: if TYPE_CHECKING: import expensive_mod - def fun(): - local_var: expensive_mod.some_type = other_fun() + def fun(arg: 'expensive_mod.SomeType') -> None: + local_var: expensive_mod.AnotherType = other_fun() + + Note that the first type annotation must be enclosed in quotes, making it a + "forward reference", to hide the ``expensive_mod`` reference from the + interpreter runtime. Type annotations for local variables are not + evaluated, so the second annotation does not need to be enclosed in quotes.