@@ -7,16 +7,20 @@ Type: Standards Track
77Topic: Typing
88Created: 24-Mar-2025
99Python-Version: 3.14
10+ Post-History: `11-Jan-2025 <https://discuss.python.org/t/76766 >`__,
11+ `24-Mar-2025 <https://discuss.python.org/t/85728 >`__,
1012Post-History: `11-Jan-2025 <https://discuss.python.org/t/76766 >`__
1113
1214
1315Abstract
1416========
1517
16- This PEP proposes adding a new keyword ``__type_checking__ `` that serves the
17- same purpose as :data: `typing.TYPE_CHECKING `.
18- This constant is ``True `` when the code is being analyzed by a static type
19- checker, and ``False `` during normal runtime execution.
18+ This PEP proposes adding a new keyword, :data: `!__type_checking__ `, to improve
19+ the experience of writing Python code with type annotations. It is evaluated
20+ as ``True `` when the code is being analyzed by a static type checker, and as
21+ ``False `` during normal runtime execution. Unlike :data: `typing.TYPE_CHECKING `,
22+ which this keyword replaces, it does not require an import statement, and
23+ creates the opportunity for compiler optimizations, such as smaller bytecode.
2024
2125
2226Motivation
@@ -44,8 +48,8 @@ Using Keyword
4448-------------
4549
4650Avoiding the addition of a new :ref: `keyword <python:keywords >`
47- (like :data: `__debug__ `) would be attractive because more keywords means more
48- complex language.
51+ (like :data: `__debug__ `) would be attractive because more keywords means
52+ a more complex language.
4953
5054However, adding a constant without a keyword (like :data: `__debug__ `) requires
5155special handling in both the compiler and runtime.
@@ -60,16 +64,17 @@ Specification
6064
6165``__type_checking__ `` is a :ref: `keyword <python:keywords >` and its value is
6266``False ``.
63- It can be used in the same way as ``False ``, except it can not be used as
67+ It can be used in the same way as ``False ``, except it cannot be used as
6468a matching pattern.
6569
66- Similar to :data: ` typing.TYPE_CHECKING `, type checking tools should treat
67- `` __type_checking__ `` as `` True ` `.
70+ Static type checkers must treat `` __type_checking__ `` as `` True ``,
71+ similar to :data: ` typing.TYPE_CHECKING `.
6872
69- Once this PEP is accepted, ``__type_checking__ `` will be the preferred way over
70- ``typing.TYPE_CHECKING ``. However, ``typing.TYPE_CHECKING `` will not be deprecated.
71- Instead, it will be implemented using ``__type_checking__ ``, allowing future type
72- checkers to focus on handling ``__type_checking__ `` only.
73+ If this PEP is accepted, ``__type_checking__ `` will be the preferred approach,
74+ instead of ``typing.TYPE_CHECKING ``. However, ``typing.TYPE_CHECKING `` will not
75+ be deprecated.
76+ Instead, it will be implemented as ``TYPE_CHECKING = __type_checking__ ``,
77+ allowing future type checkers to focus on only handling ``__type_checking__ ``.
7378
7479
7580How to Teach This
0 commit comments