-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[red-knot] Understand Annotated
#14950
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
2d36d00
[red-knot] Understand `Annotated`
InSyncWithFoo 2bf97b8
Fix panic
InSyncWithFoo 36d8e87
Again
InSyncWithFoo d78ef82
Again
InSyncWithFoo f186ed1
Again
InSyncWithFoo 1659b56
Per review
InSyncWithFoo f7def14
Use a new rule for "invalid `Annotated` parameters" errors
InSyncWithFoo 5106007
Lints
InSyncWithFoo d997b74
Fix
InSyncWithFoo 6b4dfe9
New test
InSyncWithFoo aa0a781
Better test
InSyncWithFoo 0ed607f
Fix again
InSyncWithFoo fbe0a4e
Update crates/red_knot_python_semantic/src/types/diagnostic.rs
InSyncWithFoo 7568b31
Apply suggestions from code review
InSyncWithFoo f34c372
Rebase
InSyncWithFoo a7c2412
Rename
InSyncWithFoo ba8d2c0
This too
InSyncWithFoo 53d39ab
Another
InSyncWithFoo bda1d0c
Tests too
InSyncWithFoo b221236
Per review
InSyncWithFoo 1bb42e1
Merge `INVALID_LITERAL_PARAMETER`, `INVALID_ANNOTATED_ARGUMENTS` and …
InSyncWithFoo b96caf2
Tests
InSyncWithFoo 77769f9
Update crates/red_knot_python_semantic/src/types.rs
InSyncWithFoo fcfa9f0
Rust comment
InSyncWithFoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
crates/red_knot_python_semantic/resources/mdtest/annotations/annotated.md
This file contains hidden or 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,80 @@ | ||
# `Annotated` | ||
|
||
`Annotated` attaches arbitrary metadata to a given type. | ||
|
||
## Usages | ||
|
||
`Annotated[T, ...]` is equivalent to `T`: All metadata arguments are simply ignored. | ||
|
||
```py | ||
from typing_extensions import Annotated | ||
|
||
def _(x: Annotated[int, "foo"]): | ||
reveal_type(x) # revealed: int | ||
|
||
def _(x: Annotated[int, lambda: 0 + 1 * 2 // 3, _(4)]): | ||
reveal_type(x) # revealed: int | ||
|
||
def _(x: Annotated[int, "arbitrary", "metadata", "elements", "are", "fine"]): | ||
reveal_type(x) # revealed: int | ||
|
||
def _(x: Annotated[tuple[str, int], bytes]): | ||
reveal_type(x) # revealed: tuple[str, int] | ||
``` | ||
|
||
## Parameterization | ||
|
||
It is invalid to parameterize `Annotated` with less than two arguments. | ||
|
||
```py | ||
from typing_extensions import Annotated | ||
|
||
# TODO: This should be an error | ||
def _(x: Annotated): | ||
carljm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
reveal_type(x) # revealed: Unknown | ||
|
||
# error: [invalid-type-form] | ||
def _(x: Annotated[()]): | ||
InSyncWithFoo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
reveal_type(x) # revealed: Unknown | ||
|
||
# error: [invalid-type-form] | ||
def _(x: Annotated[int]): | ||
# `Annotated[T]` is invalid and will raise an error at runtime, | ||
# but we treat it the same as `T` to provide better diagnostics later on. | ||
# The subscription itself is still reported, regardless. | ||
# Same for the `(int,)` form below. | ||
reveal_type(x) # revealed: int | ||
|
||
# error: [invalid-type-form] | ||
def _(x: Annotated[(int,)]): | ||
reveal_type(x) # revealed: int | ||
``` | ||
|
||
## Inheritance | ||
|
||
### Correctly parameterized | ||
|
||
Inheriting from `Annotated[T, ...]` is equivalent to inheriting from `T` itself. | ||
|
||
```py | ||
from typing_extensions import Annotated | ||
|
||
# TODO: False positive | ||
# error: [invalid-base] | ||
class C(Annotated[int, "foo"]): ... | ||
|
||
# TODO: Should be `tuple[Literal[C], Literal[int], Literal[object]]` | ||
reveal_type(C.__mro__) # revealed: tuple[Literal[C], Unknown, Literal[object]] | ||
``` | ||
|
||
### Not parameterized | ||
|
||
```py | ||
from typing_extensions import Annotated | ||
|
||
# At runtime, this is an error. | ||
# error: [invalid-base] | ||
class C(Annotated): ... | ||
InSyncWithFoo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
reveal_type(C.__mro__) # revealed: tuple[Literal[C], Unknown, Literal[object]] | ||
``` |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.