-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[red-knot] Add support for assert_never
#17287
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
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
106 changes: 106 additions & 0 deletions
106
crates/red_knot_python_semantic/resources/mdtest/directives/assert_never.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,106 @@ | ||
# `assert_never` | ||
|
||
## Basic functionality | ||
|
||
`assert_never` makes sure that the type of the argument is `Never`. If it is not, a | ||
`type-assertion-failure` diagnostic is emitted. | ||
|
||
```py | ||
from typing_extensions import assert_never, Never, Any | ||
from knot_extensions import Unknown | ||
|
||
def _(never: Never, any_: Any, unknown: Unknown, flag: bool): | ||
assert_never(never) # fine | ||
|
||
assert_never(0) # error: [type-assertion-failure] | ||
assert_never("") # error: [type-assertion-failure] | ||
assert_never(None) # error: [type-assertion-failure] | ||
assert_never([]) # error: [type-assertion-failure] | ||
assert_never({}) # error: [type-assertion-failure] | ||
assert_never(()) # error: [type-assertion-failure] | ||
assert_never(1 if flag else never) # error: [type-assertion-failure] | ||
|
||
assert_never(any_) # error: [type-assertion-failure] | ||
assert_never(unknown) # error: [type-assertion-failure] | ||
``` | ||
|
||
## Use case: Type narrowing and exhaustiveness checking | ||
|
||
`assert_never` can be used in combination with type narrowing as a way to make sure that all cases | ||
are handled in a series of `isinstance` checks or other narrowing patterns that are supported. | ||
|
||
```py | ||
from typing_extensions import assert_never, Literal | ||
|
||
class A: ... | ||
class B: ... | ||
class C: ... | ||
|
||
def if_else_isinstance_success(obj: A | B): | ||
if isinstance(obj, A): | ||
pass | ||
elif isinstance(obj, B): | ||
pass | ||
elif isinstance(obj, C): | ||
pass | ||
else: | ||
assert_never(obj) | ||
|
||
def if_else_isinstance_error(obj: A | B): | ||
if isinstance(obj, A): | ||
pass | ||
# B is missing | ||
elif isinstance(obj, C): | ||
pass | ||
else: | ||
# error: [type-assertion-failure] "Expected type `Never`, got `B & ~A & ~C` instead" | ||
assert_never(obj) | ||
|
||
def if_else_singletons_success(obj: Literal[1, "a"] | None): | ||
if obj == 1: | ||
pass | ||
elif obj == "a": | ||
pass | ||
elif obj is None: | ||
pass | ||
else: | ||
assert_never(obj) | ||
|
||
def if_else_singletons_error(obj: Literal[1, "a"] | None): | ||
if obj == 1: | ||
pass | ||
elif obj is "A": # "A" instead of "a" | ||
pass | ||
elif obj is None: | ||
pass | ||
else: | ||
# error: [type-assertion-failure] "Expected type `Never`, got `Literal["a"]` instead" | ||
assert_never(obj) | ||
|
||
def match_singletons_success(obj: Literal[1, "a"] | None): | ||
match obj: | ||
case 1: | ||
pass | ||
case "a": | ||
pass | ||
case None: | ||
pass | ||
case _ as obj: | ||
# TODO: Ideally, we would not emit an error here | ||
# error: [type-assertion-failure] "Expected type `Never`, got `@Todo" | ||
assert_never(obj) | ||
|
||
def match_singletons_error(obj: Literal[1, "a"] | None): | ||
match obj: | ||
case 1: | ||
pass | ||
case "A": # "A" instead of "a" | ||
pass | ||
case None: | ||
pass | ||
case _ as obj: | ||
# TODO: We should emit an error here, but the message should | ||
# show the type `Literal["a"]` instead of `@Todo(…)`. | ||
# error: [type-assertion-failure] "Expected type `Never`, got `@Todo" | ||
assert_never(obj) | ||
``` |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did consider making more changes in the lint description here. I also considered a dedicated new diagnostic. But eventually thought that this might be enough? Let me know if you have a different opinion.