Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ from typing_extensions import assert_type

# Subtype does not count
def _(x: bool):
assert_type(x, int) # error: [type-assertion-failure] "Type `int` does not match asserted type `bool`"
assert_type(x, int) # error: [type-assertion-failure] "Type `bool` does not match asserted type `int`"

def _(a: type[int], b: type[Any]):
assert_type(a, type[Any]) # error: [type-assertion-failure] "Type `type[Any]` does not match asserted type `type[int]`"
assert_type(b, type[int]) # error: [type-assertion-failure] "Type `type[int]` does not match asserted type `type[Any]`"
assert_type(a, type[Any]) # error: [type-assertion-failure] "Type `type[int]` does not match asserted type `type[Any]`"
assert_type(b, type[int]) # error: [type-assertion-failure] "Type `type[Any]` does not match asserted type `type[int]`"

# The expression constructing the type is not taken into account
def _(a: type[int]):
Expand All @@ -69,9 +69,9 @@ class Bar: ...
class Baz: ...

def f(x: Foo):
assert_type(x, Bar) # error: [type-assertion-failure] "Type `Bar` does not match asserted type `Foo`"
assert_type(x, Bar) # error: [type-assertion-failure] "Type `Foo` does not match asserted type `Bar`"
if isinstance(x, Bar):
assert_type(x, Bar) # error: [assert-type-unspellable-subtype] "Type `Bar` does not match asserted type `Foo & Bar`"
assert_type(x, Bar) # error: [assert-type-unspellable-subtype] "Type `Foo & Bar` does not match asserted type `Bar`"

# The actual type must be a subtype of the asserted type, as well as being unspellable,
# in order for `assert-type-unspellable-subtype` to be emitted instead of `type-assertion-failure`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
source: crates/ty_test/src/lib.rs
expression: snapshot
---

---
Comment thread
AlexWaygood marked this conversation as resolved.
mdtest name: assert_type.md - `assert_type` - Unspellable types
mdtest path: crates/ty_python_semantic/resources/mdtest/directives/assert_type.md
Expand All @@ -20,9 +19,9 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/directives/assert_type.m
5 | class Baz: ...
6 |
7 | def f(x: Foo):
8 | assert_type(x, Bar) # error: [type-assertion-failure] "Type `Bar` does not match asserted type `Foo`"
8 | assert_type(x, Bar) # error: [type-assertion-failure] "Type `Foo` does not match asserted type `Bar`"
9 | if isinstance(x, Bar):
10 | assert_type(x, Bar) # error: [assert-type-unspellable-subtype] "Type `Bar` does not match asserted type `Foo & Bar`"
10 | assert_type(x, Bar) # error: [assert-type-unspellable-subtype] "Type `Foo & Bar` does not match asserted type `Bar`"
11 |
12 | # The actual type must be a subtype of the asserted type, as well as being unspellable,
13 | # in order for `assert-type-unspellable-subtype` to be emitted instead of `type-assertion-failure`
Expand All @@ -36,12 +35,12 @@ error[type-assertion-failure]: Argument does not have asserted type `Bar`
--> src/mdtest_snippet.py:8:5
|
7 | def f(x: Foo):
8 | assert_type(x, Bar) # error: [type-assertion-failure] "Type `Bar` does not match asserted type `Foo`"
8 | assert_type(x, Bar) # error: [type-assertion-failure] "Type `Foo` does not match asserted type `Bar`"
| ^^^^^^^^^^^^-^^^^^^
| |
| Inferred type is `Foo`
9 | if isinstance(x, Bar):
10 | assert_type(x, Bar) # error: [assert-type-unspellable-subtype] "Type `Bar` does not match asserted type `Foo & Bar`"
10 | assert_type(x, Bar) # error: [assert-type-unspellable-subtype] "Type `Foo & Bar` does not match asserted type `Bar`"
|
info: `Bar` and `Foo` are not equivalent types
info: rule `type-assertion-failure` is enabled by default
Expand All @@ -52,9 +51,9 @@ info: rule `type-assertion-failure` is enabled by default
error[assert-type-unspellable-subtype]: Argument does not have asserted type `Bar`
--> src/mdtest_snippet.py:10:9
|
8 | assert_type(x, Bar) # error: [type-assertion-failure] "Type `Bar` does not match asserted type `Foo`"
8 | assert_type(x, Bar) # error: [type-assertion-failure] "Type `Foo` does not match asserted type `Bar`"
9 | if isinstance(x, Bar):
10 | assert_type(x, Bar) # error: [assert-type-unspellable-subtype] "Type `Bar` does not match asserted type `Foo & Bar`"
10 | assert_type(x, Bar) # error: [assert-type-unspellable-subtype] "Type `Foo & Bar` does not match asserted type `Bar`"
| ^^^^^^^^^^^^-^^^^^^
| |
| Inferred type is `Foo & Bar`
Expand Down
2 changes: 1 addition & 1 deletion crates/ty_python_semantic/src/types/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1774,8 +1774,8 @@ impl KnownFunction {

diagnostic.set_concise_message(format_args!(
"Type `{}` does not match asserted type `{}`",
asserted_ty.display(db),
actual_ty.display(db),
asserted_ty.display(db),
));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ PublishDiagnosticsParams {
source: Some(
"ty",
),
message: "Type `list[str]` does not match asserted type `Literal[/"test/"]`",
message: "Type `Literal[/"test/"]` does not match asserted type `list[str]`",
related_information: None,
tags: None,
data: None,
Expand Down
Loading