summaryrefslogtreecommitdiffstats
path: root/Doc/library
diff options
context:
space:
mode:
authorBerker Peksag <berker.peksag@gmail.com>2017-04-26 14:24:35 (GMT)
committerGitHub <noreply@github.com>2017-04-26 14:24:35 (GMT)
commit271a289a03ad10c91c5326bde3020f1cdf6a1fff (patch)
tree5c6ce97c6ff83525c79111027b8eea3d9793b5a5 /Doc/library
parentc7b8367076dc7771dabcb9491bd98218c788d489 (diff)
downloadcpython-271a289a03ad10c91c5326bde3020f1cdf6a1fff.zip
cpython-271a289a03ad10c91c5326bde3020f1cdf6a1fff.tar.gz
cpython-271a289a03ad10c91c5326bde3020f1cdf6a1fff.tar.bz2
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 87c07fe9d908d0a2143fcc8369255c6ff3241503)
Diffstat (limited to 'Doc/library')
-rw-r--r--Doc/library/typing.rst9
1 files changed, 7 insertions, 2 deletions
diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst
index 93ea4f9..2ca3bcd 100644
--- a/Doc/library/typing.rst
+++ b/Doc/library/typing.rst
@@ -963,5 +963,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.