summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMathias Rav <mathiasrav@gmail.com>2017-04-26 10:49:45 (GMT)
committerBerker Peksag <berker.peksag@gmail.com>2017-04-26 10:49:45 (GMT)
commit87c07fe9d908d0a2143fcc8369255c6ff3241503 (patch)
tree4c5707b04d8d723cf23dc8f2185ea6c53ddd52b1
parent0d637e236d7099f7b724026c8cb7bd83d8e12e6b (diff)
downloadcpython-87c07fe9d908d0a2143fcc8369255c6ff3241503.zip
cpython-87c07fe9d908d0a2143fcc8369255c6ff3241503.tar.gz
cpython-87c07fe9d908d0a2143fcc8369255c6ff3241503.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.
-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 2cfc37f..1780739 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.