mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 05:15:12 +00:00

## Summary Add support for `typing.TYPE_CHECKING` and `typing_extensions.TYPE_CHECKING`. relates to: https://github.com/astral-sh/ruff/issues/14170 ## Test Plan New Markdown-based tests
1.1 KiB
1.1 KiB
Known constants
typing.TYPE_CHECKING
This constant is True
when in type-checking mode, False
otherwise. The symbol is defined to be
False
at runtime. In typeshed, it is annotated as bool
. This test makes sure that we infer
Literal[True]
for it anyways.
Basic
from typing import TYPE_CHECKING
import typing
reveal_type(TYPE_CHECKING) # revealed: Literal[True]
reveal_type(typing.TYPE_CHECKING) # revealed: Literal[True]
Aliased
Make sure that we still infer the correct type if the constant has been given a different name:
from typing import TYPE_CHECKING as TC
reveal_type(TC) # revealed: Literal[True]
Must originate from typing
Make sure we only use our special handling for typing.TYPE_CHECKING
and not for other constants
with the same name:
TYPE_CHECKING: bool = False
from constants import TYPE_CHECKING
reveal_type(TYPE_CHECKING) # revealed: bool
typing_extensions
re-export
This should behave in the same way as typing.TYPE_CHECKING
:
from typing_extensions import TYPE_CHECKING
reveal_type(TYPE_CHECKING) # revealed: Literal[True]