# Specify \`TYPE\_CHECKING = False\` without typing import

**URL:** <https://discuss.python.org/t/specify-type-checking-false-without-typing-import/76766>\
**Category:** Typing\
**Created:** [January 11, 2025, 7:42am UTC](https://discuss.python.org/t/specify-type-checking-false-without-typing-import/76766 "2025-01-11T07:42:44Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![hauntsaninja](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/hauntsaninja/32/12451_2.png) [@hauntsaninja](https://discuss.python.org/u/hauntsaninja)\
**Post date:** [January 11, 2025, 7:42am UTC](https://discuss.python.org/t/specify-type-checking-false-without-typing-import/76766/1 "2025-01-11T07:42:44Z")

</div>

I believe both mypy and pyright already recognise use of:

```python
TYPE_CHECKING = False
if TYPE_CHECKING: ...

```

as an alternative to `from typing import TYPE_CHECKING`.

Any objections to putting this in spec? Something like:

> Type checkers should consider all variables named TYPE\_CHECKING as True during type checking

I checked [usage in the wild](https://grep.app/search?q=TYPE_CHECKING%20%3D&case=true&filter%5Blang%5D%5B0%5D=Python) in the off chance there was some non static typing usage of this variable name.

The runtime cost of `import typing` came up recently in:

- [Reduce import time part 3: remove `typing` import by hukkin · Pull Request #251 · hukkin/tomli · GitHub](https://github.com/hukkin/tomli/pull/251)
- [Static analysis at odds with runtime goals](https://discuss.python.org/t/static-analysis-at-odds-with-runtime-goals/76319)

An alternative could be to make `TYPE_CHECKING` a built-in, as discussed in [Moving TYPE\_CHECKING out of typing and into the built-in namespace](https://discuss.python.org/t/moving-type-checking-out-of-typing-and-into-the-built-in-namespace/7870). I don’t personally love making it a builtin, since `import typing` is cheap enough for the vast majority of users, runtime typing is a real part of the language and `TYPE_CHECKING` can get very sketchy.

---

<div class="post-metadata">

**Author:** ![Monarch](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/monarch/32/18530_2.png) [@Monarch](https://discuss.python.org/u/Monarch)\
**Post date:** [January 11, 2025, 8:08am UTC](https://discuss.python.org/t/specify-type-checking-false-without-typing-import/76766/2 "2025-01-11T08:08:59Z")

</div>

Would it be possible to support this:

```py
if TYPE_CHECKING := False:
    from typing import Self

```

---

<div class="post-metadata">

**Author:** ![erictraut](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/erictraut/32/15190_2.png) [@erictraut](https://discuss.python.org/u/erictraut)\
**Post date:** [January 11, 2025, 8:31am UTC](https://discuss.python.org/t/specify-type-checking-false-without-typing-import/76766/3 "2025-01-11T08:31:07Z")

</div>

> Any objections to putting this in spec?

Yeah, I object to this. I don’t think that any symbol named `TYPE_CHECKING` should special semantics. This should apply only to `typing. TYPE_CHECKING`.

Both mypy and pyright already have mechanisms to specify that specific symbols should have hard-coded values at type checking time, so if someone wants to specify that a symbol named `TYPE_CHECKING` has the value of `True` or `False`, they can already configure their type checker as such. This shouldn’t be imposed on everyone unconditionally.

---

<div class="post-metadata">

**Author:** ![ali](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@ali](https://discuss.python.org/u/ali)\
**Post date:** [January 11, 2025, 8:38am UTC](https://discuss.python.org/t/specify-type-checking-false-without-typing-import/76766/4 "2025-01-11T08:38:20Z")

</div>

> Both mypy and pyright already have mechanisms to specify that specific symbols should have hard-coded values at type checking time, so if someone wants to specify that a symbol named `TYPE_CHECKING` has the value of `True` or `False` , they can already configure their type checker as such. This shouldn’t be imposed on everyone unconditionally.

This doesn’t work for libraries. A library cannot know what type checker its users are going to use. Having a universal way of deferring typing imports without importing the typing module is very useful for libraries used in command line applications. I saw 35% improvement in import time in my library using this trick, I couldn’t do this if it didn’t work with either mypy or pyright.

---

<div class="post-metadata">

**Author:** ![NeilGirdhar](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/neilgirdhar/32/5776_2.png) [@NeilGirdhar](https://discuss.python.org/u/NeilGirdhar)\
**Post date:** [January 11, 2025, 8:40am UTC](https://discuss.python.org/t/specify-type-checking-false-without-typing-import/76766/5 "2025-01-11T08:40:26Z")

</div>

Will there be any need for `TYPE_CHECKING` when PEP 749 is guaranteed (after Python 3.13 is EOL)?

Would it be possible to cache the interpreter state after the `typing` import and save it to disk? (Like a `pyc` file, but better). This might be possible if `typing` doesn’t really interact with anything the user has done to the interpreter, which may be the case?

---

<div class="post-metadata">

**Author:** ![Eneg](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/eneg/32/18177_2.png) [@Eneg](https://discuss.python.org/u/Eneg)\
**Post date:** [January 11, 2025, 8:58am UTC](https://discuss.python.org/t/specify-type-checking-false-without-typing-import/76766/6 "2025-01-11T08:58:17Z")

</div>

> [@NeilGirdhar](#):
>
> Will there be any need for `TYPE_CHECKING` when PEP 749 is guaranteed

Yes, when a type comes from an external, expensive module, and isn’t used at runtime.

I also find it useful in resolving metaclass related issues (for example, an `Enum` which explicitly implements a `Protocol`)

---

<div class="post-metadata">

**Author:** ![NeilGirdhar](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/neilgirdhar/32/5776_2.png) [@NeilGirdhar](https://discuss.python.org/u/NeilGirdhar)\
**Post date:** [January 11, 2025, 9:10am UTC](https://discuss.python.org/t/specify-type-checking-false-without-typing-import/76766/7 "2025-01-11T09:10:05Z")

</div>

Makes sense, thanks. Those cases seem pretty niche? Do we really need to worry about making `TYPE_CHECKING` faster to import for them?

---

<div class="post-metadata">

**Author:** ![oscarbenjamin](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/oscarbenjamin/32/1209_2.png) [@oscarbenjamin](https://discuss.python.org/u/oscarbenjamin)\
**Post date:** [January 11, 2025, 11:07am UTC](https://discuss.python.org/t/specify-type-checking-false-without-typing-import/76766/8 "2025-01-11T11:07:40Z")

</div>

> [@NeilGirdhar](#):
>
> Those cases seem pretty niche?

There is nothing niche about wanting to avoid expensive imports that are not necessarily needed at runtime. The modules to be imported from may not even be installed in the case of optional dependencies.

> [@erictraut](#):
>
> Yeah, I object to this. I don’t think that any symbol named `TYPE_CHECKING` should special semantics. This should apply only to `typing. TYPE_CHECKING`.

The problem is that `import typing` has a real runtime overhead and it isn’t possible to avoid that overhead at runtime if the only way to check `TYPE_CHECKING` is by importing it from the typing module. From my measurements here a simple hello world program is made about 10% slower by `from typing import TYPE_CHECKING`.

---

<div class="post-metadata">

**Author:** ![mikeshardmind](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mikeshardmind/32/14381_2.png) [@mikeshardmind](https://discuss.python.org/u/mikeshardmind)\
**Post date:** [January 11, 2025, 2:44pm UTC](https://discuss.python.org/t/specify-type-checking-false-without-typing-import/76766/9 "2025-01-11T14:44:07Z")

</div>

What if it was spelled like this, and worked for any symbol assigned the literal value False?

```py
TYPE_CHECKING = False # type: typechecking

```

This isn’t unconditional, but it does add in another type-comment that needs to be understood.

---

<div class="post-metadata">

**Author:** ![mikeshardmind](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mikeshardmind/32/14381_2.png) [@mikeshardmind](https://discuss.python.org/u/mikeshardmind)\
**Post date:** [January 11, 2025, 2:55pm UTC](https://discuss.python.org/t/specify-type-checking-false-without-typing-import/76766/10 "2025-01-11T14:55:04Z")

</div>

I don’t think so, or at least not without more consideration; The typing module is stateful thanks to the overload decorator storing overloads for runtime retrieval. I also wouldn’t want to special-case the typing module here, if we’re looking to make something better than .pyc files, it should be something more generally useful.

As I mentioned in one of the other recent import time related threads though, anything we can do to speed up imports as well as defer expensive imports is welcome and these shouldn’t be mutually exclusive. Deferring imports where reasonable improves the startup cost for code that may not use everything it imports each time, making imports faster helps everyone who can’t defer or actually does use it.

---

<div class="post-metadata">

**Author:** ![Daverball](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/daverball/32/15931_2.png) [@Daverball](https://discuss.python.org/u/Daverball)\
**Post date:** [January 11, 2025, 3:32pm UTC](https://discuss.python.org/t/specify-type-checking-false-without-typing-import/76766/11 "2025-01-11T15:32:48Z")

</div>

There used to be something similar to this for supporting earlier versions of Python:

```python
if False: # TYPE_CHECKING

```

Although AFAIK this hasn’t been supported for quite a while now\[1\], I think I’d still prefer this over the other ways to spell this.

* * *

1. I think the last version of mypy that supported this was before support for 3.5 and 2.7 was dropped, so sometime before mypy 1.0

---

<div class="post-metadata">

**Author:** ![erictraut](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/erictraut/32/15190_2.png) [@erictraut](https://discuss.python.org/u/erictraut)\
**Post date:** [January 11, 2025, 3:42pm UTC](https://discuss.python.org/t/specify-type-checking-false-without-typing-import/76766/12 "2025-01-11T15:42:02Z")

</div>

It’s very unlikely that a library could completely avoid importing `typing` if it is typed, so in practice I don’t think this would help anyone.

In rare cases, a library could use a stub to provide its type information and completely avoid importing from `typing`. Although if module load time is such a concern, it would be best to implement such a library in a compiled language.

This would also set a terrible precedent in ascribing meaning to a non-builtin symbol based only on its name. I don’t think we should go there.

I also think that `typing.TYPE_CHECKING` is a significant footgun. It provides a way to lie to the type checker, and this makes code fragile. In my experience, the use of `TYPE_CHECKING` is a _way_ bigger source of uncaught bugs than most of the esoteric type theory concerns that are frequently brought up in this forum. I advise developers to avoid using `TYPE_CHECKING` if they want to write robust and maintainable code.

---

<div class="post-metadata">

**Author:** ![oscarbenjamin](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/oscarbenjamin/32/1209_2.png) [@oscarbenjamin](https://discuss.python.org/u/oscarbenjamin)\
**Post date:** [January 11, 2025, 4:05pm UTC](https://discuss.python.org/t/specify-type-checking-false-without-typing-import/76766/13 "2025-01-11T16:05:28Z")

</div>

> [@erictraut](#):
>
> It’s very unlikely that a library could completely avoid importing `typing` if it is typed, so in practice I don’t think this would help anyone.

We are not necessarily talking about a library but in any case it is simple to avoid importing the typing module at runtime:

```python
from __future__ import annotations

TYPE_CHECKING = False

if TYPE_CHECKING:
    from typing import Protocol, Any, ...

```

It needs to be possible to get the magic `TYPE_CHECKING` variable without importing it from typing though.

There are even lint rules for [flake8](https://pypi.org/project/flake8-type-checking/0.1.2/) and [ruff](https://docs.astral.sh/ruff/rules/#flake8-type-checking-tc) that disallow runtime import of anything that is only needed for type checking.

---

<div class="post-metadata">

**Author:** ![erictraut](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/erictraut/32/15190_2.png) [@erictraut](https://discuss.python.org/u/erictraut)\
**Post date:** [January 11, 2025, 4:32pm UTC](https://discuss.python.org/t/specify-type-checking-false-without-typing-import/76766/14 "2025-01-11T16:32:11Z")

</div>

> [@oscarbenjamin](#):
>
> We are not necessarily talking about a library but in any case it is simple to avoid importing the typing module at runtime:

As I said above, this leads to fragile code, so I strongly recommend against it. When you lie to type checkers and linters, you’re going to mask bugs that they would otherwise find. There are almost always ways to avoid using `typing.TYPE_CHECKING`. I use it only as a last resort. It’s a footgun that many developers are too eager to reach for.

If you’re insistent on using this approach in your own code (as opposed to a library), you have the option of telling mypy or pyright to treat a specified global symbol as “always True”. For example, if you’re using pyright you can add the following line to its config file:

```toml
defineConstant = { TYPE_CHECKING = true }

```

I’ll also note that if your program is so sensitive to startup times that you are trying to shave tens of milliseconds from it, then Python is probably not a great language choice to begin with.

---

<div class="post-metadata">

**Author:** ![mikeshardmind](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mikeshardmind/32/14381_2.png) [@mikeshardmind](https://discuss.python.org/u/mikeshardmind)\
**Post date:** [January 11, 2025, 5:22pm UTC](https://discuss.python.org/t/specify-type-checking-false-without-typing-import/76766/15 "2025-01-11T17:22:13Z")

</div>

> [@erictraut](#):
>
> I’ll also note that if your program is so sensitive to startup times that you are trying to shave tens of milliseconds from it, then Python is probably not a great language choice to begin with.

Unfortunately, while I agree with you, the people dealing with this don’t always have the freedom to make a different choice here for various non-technical reasons.

I’ll have some actual numbers from a staging environment in about a week from now, but it was a noticeable delay in something that creates multiple interpreters.

> [@erictraut](#):
>
> As I said above, this leads to fragile code, so I strongly recommend against it. When you lie to type checkers and linters, you’re going to mask bugs that they would otherwise find.

Unfortunately, you specifically have also held that other means of deferring the import that ensure the runtime symbol is identical aren’t supported. (module getattr, and deffering the import through indirection, linked at top)

---

<div class="post-metadata">

**Author:** ![oscarbenjamin](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/oscarbenjamin/32/1209_2.png) [@oscarbenjamin](https://discuss.python.org/u/oscarbenjamin)\
**Post date:** [January 11, 2025, 5:28pm UTC](https://discuss.python.org/t/specify-type-checking-false-without-typing-import/76766/16 "2025-01-11T17:28:39Z")

</div>

> [@erictraut](#):
>
> When you lie to type checkers and linters, you’re going to mask bugs that they would otherwise find.

I don’t see how putting the typing imports into a `TYPE_CHECKING` block is lying about anything.

> [@erictraut](#):
>
> I’ll also note that if your program is so sensitive to startup times that you are trying to shave tens of milliseconds from it, then Python is probably not a great language choice to begin with.

I see this sort of attitude a lot whenever anyone talks about speed in Python. I guess there are two different approaches that people take to the idea that Python is slow:

- Maybe we should make things faster or reduce unnecessary overheads.
- Maybe we should just give up and let everything become slower and slower.

You can use Python to wrap up C/Rust etc code but whatever you do to accelerate the heavy lifting a simple CLI program is very often limited by startup overhead. Every time someone decides that it’s okay to waste a bit more time here and there by making different parts of startup slower it all adds up.

If people took the attitude that runtime performance does actually matter then I think a lot of things would be designed differently. Instead every time a discussion about performance comes up people will call into question even the notion that anyone should care about it because “use a different language if you care about that”. If you don’t care about performance then it is inevitable that things will end up being slow.

---

<div class="post-metadata">

**Author:** ![mikeshardmind](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mikeshardmind/32/14381_2.png) [@mikeshardmind](https://discuss.python.org/u/mikeshardmind)\
**Post date:** [January 11, 2025, 5:29pm UTC](https://discuss.python.org/t/specify-type-checking-false-without-typing-import/76766/17 "2025-01-11T17:29:10Z")

</div>

> [@erictraut](#):
>
> It’s very unlikely that a library could completely avoid importing `typing` if it is typed, so in practice I don’t think this would help anyone.

This is representative of things I’ve done to ensure import costs are minimal in library code. Youll find zero typing imports that are eager here, and the library is fully typed including complex type signatures: [GitHub - mikeshardmind/async-utils: Collection of async utillities](https://github.com/mikeshardmind/async-utils) It’s a design goal that things using this can import and only incur the cost of what is used, and the only thing I had to do that I wasn’t happy about was remove use of typing.Final, not because of it having any runtime effect, but because type checkers don’t understand Final as a deferrable re-export.

---

<div class="post-metadata">

**Author:** ![ali](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@ali](https://discuss.python.org/u/ali)\
**Post date:** [January 11, 2025, 6:38pm UTC](https://discuss.python.org/t/specify-type-checking-false-without-typing-import/76766/18 "2025-01-11T18:38:17Z")

</div>

Eric, I respect your goal of making type checking more inline with runtime. I think it definitely a good rule to write code that is type checked the way it is run by the interpreter. What is proposed here however doesn’t encourage people to have more divergence between type checking and runtime, it merely gives people a way to not incur the typing module overhead. What you are suggesting here fits more in a style guide or a linter rule or maybe a super strict type checker setting, it shouldn’t be up for type checkers to _prevent_ people who want faster import from using typing.

As I said before, having type checker-specific setting to define this won’t help libraries that need to support many users with different type-checkers/IDEs. I don’t think telling these libraries to write their code in a more performant language is fair. I also don’t think that pushing them to use stub files and leave their code un-annotated is a good idea. It makes it harder for them to maintain their code and makes them lose the benefit of type checking their own code with inline annotations.

---

<div class="post-metadata">

**Author:** ![ntessore](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ntessore/32/10786_2.png) [@ntessore](https://discuss.python.org/u/ntessore)\
**Post date:** [January 11, 2025, 11:29pm UTC](https://discuss.python.org/t/specify-type-checking-false-without-typing-import/76766/19 "2025-01-11T23:29:08Z")

</div>

> [@hauntsaninja](#):
>
> An alternative could be to make `TYPE_CHECKING` a built-in, as discussed in [Moving TYPE\_CHECKING out of typing and into the built-in namespace](https://discuss.python.org/t/moving-type-checking-out-of-typing-and-into-the-built-in-namespace/7870). I don’t personally love making it a builtin, since `import typing` is cheap enough for the vast majority of users, runtime typing is a real part of the language and `TYPE_CHECKING` can get very sketchy.

How would `TYPE_CHECKING` be functionally any different if it simply moved to the builtins?

---

<div class="post-metadata">

**Author:** ![Liz](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/liz/32/15021_2.png) [@Liz](https://discuss.python.org/u/Liz)\
**Post date:** [January 12, 2025, 4:08pm UTC](https://discuss.python.org/t/specify-type-checking-false-without-typing-import/76766/20 "2025-01-12T16:08:33Z")

</div>

> [@hauntsaninja](#):
>
> An alternative could be to make `TYPE_CHECKING` a built-in, as discussed in [Moving TYPE\_CHECKING out of typing and into the built-in namespace](https://discuss.python.org/t/moving-type-checking-out-of-typing-and-into-the-built-in-namespace/7870). I don’t personally love making it a builtin, since `import typing` is cheap enough for the vast majority of users, runtime typing is a real part of the language and `TYPE_CHECKING` can get very sketchy.

> [@erictraut](#):
>
> I also think that `typing.TYPE_CHECKING` is a significant footgun. It provides a way to lie to the type checker, and this makes code fragile. In my experience, the use of `TYPE_CHECKING` is a _way_ bigger source of uncaught bugs than most of the esoteric type theory concerns that are frequently brought up in this forum. I advise developers to avoid using `TYPE_CHECKING` if they want to write robust and maintainable code.

This is needed to have lazy importing work properly. Rather than treat it as a footgun and tell people not to use it, it would be beneficial to look at why people need it and teach how to make sure it is only used sparingly, and in a way that runtime matches type time.

The alternative requires type checkers to understand and support module getattr and custom module loaders, both of which are in active use. This seems far too dynamic to be worth putting into the type system.

My experience is that it’s only a footgun when there’s no appropriate matching runtime symbol in the else branch.

[Next page](https://discuss.python.org/t/specify-type-checking-false-without-typing-import/76766.md?page=2)
