Skip to content

Commit 4fd34d2

Browse files
committed
PEP 781: Adding __type_checking__ constant
1 parent f52625f commit 4fd34d2

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

peps/pep-0781.rst

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
PEP: 781
2+
Title: Adding __type_checking__ constant
3+
Author: Inada Naoki <songofacandy@gmail.com>
4+
Discussions-To: https://discuss.python.org/t/85728
5+
Status: Draft
6+
Type: Standards Track
7+
Topic: Typing
8+
Created: 24-Mar-2025
9+
Python-Version: 3.14
10+
11+
12+
Abstract
13+
========
14+
15+
This PEP proposes adding a new keyword ``__type_checking__`` that serves the
16+
same purpose as ``typing.TYPE_CHECKING``.
17+
This constant is ``True`` when the code is being analyzed by a static type
18+
checker, and ``False`` during normal runtime execution.
19+
20+
21+
Motivation
22+
==========
23+
24+
For libraries that may be used in scripts where startup time is critical,
25+
the time taken to import the ``typing`` module cannot be ignored.
26+
27+
To avoid importing ``typing``, developers currently define a module-level
28+
variable ``TYPE_CHECKING = False`` or use code like
29+
``if False: # TYPE_CHECKING``.
30+
Providing a standard method will allow many tools to implement the same
31+
behavior consistently.
32+
33+
Furthermore, this allows compilers to eliminate type-checking-only code at
34+
compile time. This reduces bytecode size and memory usage,
35+
and would help with writing type-hinted code for memory-constrained
36+
environments like WASM or micropython.
37+
38+
39+
Specification
40+
=============
41+
42+
``__type_checking__`` is a keyword and its value is ``False``.
43+
It can be used in the same way as ``False``, except it can not be used as
44+
a matching pattern.
45+
46+
Similar to typing.TYPE_CHECKING, type checking tools should treat
47+
``__type_checking__`` as True.
48+
49+
50+
Rationale
51+
=========
52+
53+
Difference from ``__debug__``
54+
-----------------------------
55+
56+
``__debug__`` is not a keyword, but a built-in constant.
57+
Not adding a new keyword is attractive, but it requires special handling in
58+
both the compiler and the runtime to change the value of the constant
59+
depending on the startup option.
60+
61+
By implementing ``__type_checking__`` the same way as ``False``,
62+
we avoid the need for special handling in both the compiler and runtime.
63+
Therefore, making it a keyword is the simpler approach.
64+
65+
66+
How to teach this
67+
=================
68+
69+
Add this note to the ``typing.TYPE_CHECKING`` document:
70+
71+
> If you don't want to import ``typing``, you can use ``__type_checking__``.
72+
> Workaround like ``TYPE_CHECKING = False`` or
73+
> ``if False: # TYPE_CHECKING`` are not recommended since Python 3.14.
74+
75+
76+
Resources
77+
=========
78+
79+
* Previous discussion: https://discuss.python.org/t/76766
80+
* Reference Implementation: https://github.com/python/cpython/pull/131641
81+
82+
83+
Copyright
84+
=========
85+
86+
This document is placed in the public domain or under the
87+
CC0-1.0-Universal license, whichever is more permissive.

0 commit comments

Comments
 (0)