ruff/crates/red_knot_python_semantic/resources/mdtest/known_constants.md
David Peter e96b13c027
[red-knot] Support typing.TYPE_CHECKING (#14952)
## 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
2024-12-13 09:24:48 +00:00

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]