-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[red-knot] Support
typing.TYPE_CHECKING
(#14952)
## Summary Add support for `typing.TYPE_CHECKING` and `typing_extensions.TYPE_CHECKING`. relates to: #14170 ## Test Plan New Markdown-based tests
- Loading branch information
Showing
2 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
crates/red_knot_python_semantic/resources/mdtest/known_constants.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# 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 | ||
|
||
```py | ||
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: | ||
|
||
```py | ||
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: | ||
|
||
```py path=constants.py | ||
TYPE_CHECKING: bool = False | ||
``` | ||
|
||
```py | ||
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`: | ||
|
||
```py | ||
from typing_extensions import TYPE_CHECKING | ||
|
||
reveal_type(TYPE_CHECKING) # revealed: Literal[True] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters