-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
56 additions
and
73 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
crates/red_knot_python_semantic/resources/mdtest/annotations/deferred.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,56 @@ | ||
# Deferred annotations | ||
|
||
## Basic deferred annotation | ||
|
||
```pyi path=mod.pyi | ||
class C(object): ... | ||
``` | ||
|
||
```py | ||
from mod import C | ||
|
||
reveal_type(C.__mro__) # revealed: tuple[Literal[C], Literal[object]] | ||
``` | ||
|
||
## Deferred annotations in stubs always resolve | ||
|
||
```pyi path=mod.pyi | ||
def get_foo() -> Foo: ... | ||
class Foo: ... | ||
``` | ||
|
||
```py | ||
from mod import get_foo | ||
|
||
reveal_type(get_foo()) # revealed: Foo | ||
``` | ||
|
||
## Deferred annotations in regular code fail | ||
|
||
In (regular) source files, annotations are *not* deferred. This also tests that imports from | ||
`__future__` that are not `annotations` are ignored. | ||
|
||
```py | ||
from __future__ import with_statement as annotations | ||
|
||
# error: [unresolved-reference] | ||
def get_foo() -> Foo: ... | ||
|
||
class Foo: ... | ||
|
||
reveal_type(get_foo()) # revealed: Unknown | ||
``` | ||
|
||
## Deferred annotations in regular code with `__future__.annotations` | ||
|
||
If `__future__.annotations` is imported, annotations *are* deferred. | ||
|
||
```py | ||
from __future__ import annotations | ||
|
||
def get_foo() -> Foo: ... | ||
|
||
class Foo: ... | ||
|
||
reveal_type(get_foo()) # revealed: Foo | ||
``` |
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