Skip to content

Commit

Permalink
bpo-29974: Improve typing.TYPE_CHECKING example (GH-982)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
Mortal authored and berkerpeksag committed Apr 26, 2017
1 parent 0d637e2 commit 87c07fe
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions Doc/library/typing.rst
Expand Up @@ -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.

0 comments on commit 87c07fe

Please sign in to comment.