Skip to content

Commit 0598730

Browse files
methaneAA-Turnerhugovk
authored
Apply suggestions from code review
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com> Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
1 parent a942926 commit 0598730

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

peps/pep-0781.rst

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@ Type: Standards Track
77
Topic: Typing
88
Created: 24-Mar-2025
99
Python-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>`__,
1012
Post-History: `11-Jan-2025 <https://discuss.python.org/t/76766>`__
1113

1214

1315
Abstract
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

2226
Motivation
@@ -44,8 +48,8 @@ Using Keyword
4448
-------------
4549

4650
Avoiding 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

5054
However, adding a constant without a keyword (like :data:`__debug__`) requires
5155
special 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
6468
a 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

7580
How to Teach This

0 commit comments

Comments
 (0)