From ce3fe8fd1302b91a156c415b5f2fea5432dce041 Mon Sep 17 00:00:00 2001 From: David Peter Date: Mon, 16 Feb 2026 15:39:59 +0100 Subject: [PATCH 1/3] [ty] Fix `assert_type` diagnostic messages --- .../mdtest/directives/assert_type.md | 10 +++++----- ..._Unspellable_types_(385d082f9803b184).snap | 13 ++++++------- .../ty_python_semantic/src/types/function.rs | 2 +- ...e_without_related_information_support.snap | 2 +- .../e2e__pull_diagnostics__main.snap | 19 ------------------- 5 files changed, 13 insertions(+), 33 deletions(-) diff --git a/crates/ty_python_semantic/resources/mdtest/directives/assert_type.md b/crates/ty_python_semantic/resources/mdtest/directives/assert_type.md index b35053ccd54a5..66606e9d87da8 100644 --- a/crates/ty_python_semantic/resources/mdtest/directives/assert_type.md +++ b/crates/ty_python_semantic/resources/mdtest/directives/assert_type.md @@ -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]): @@ -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` diff --git a/crates/ty_python_semantic/resources/mdtest/snapshots/assert_type.md_-_`assert_type`_-_Unspellable_types_(385d082f9803b184).snap b/crates/ty_python_semantic/resources/mdtest/snapshots/assert_type.md_-_`assert_type`_-_Unspellable_types_(385d082f9803b184).snap index b0fdb5898322d..c984648b3e7b6 100644 --- a/crates/ty_python_semantic/resources/mdtest/snapshots/assert_type.md_-_`assert_type`_-_Unspellable_types_(385d082f9803b184).snap +++ b/crates/ty_python_semantic/resources/mdtest/snapshots/assert_type.md_-_`assert_type`_-_Unspellable_types_(385d082f9803b184).snap @@ -2,7 +2,6 @@ source: crates/ty_test/src/lib.rs expression: snapshot --- - --- mdtest name: assert_type.md - `assert_type` - Unspellable types mdtest path: crates/ty_python_semantic/resources/mdtest/directives/assert_type.md @@ -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` @@ -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 @@ -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` diff --git a/crates/ty_python_semantic/src/types/function.rs b/crates/ty_python_semantic/src/types/function.rs index 7362de61c6a51..f82a45b489576 100644 --- a/crates/ty_python_semantic/src/types/function.rs +++ b/crates/ty_python_semantic/src/types/function.rs @@ -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), )); } } diff --git a/crates/ty_server/tests/e2e/snapshots/e2e__publish_diagnostics__message_without_related_information_support.snap b/crates/ty_server/tests/e2e/snapshots/e2e__publish_diagnostics__message_without_related_information_support.snap index d2056ebf20fcf..fe24d5e7debf9 100644 --- a/crates/ty_server/tests/e2e/snapshots/e2e__publish_diagnostics__message_without_related_information_support.snap +++ b/crates/ty_server/tests/e2e/snapshots/e2e__publish_diagnostics__message_without_related_information_support.snap @@ -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, diff --git a/crates/ty_server/tests/e2e/snapshots/e2e__pull_diagnostics__main.snap b/crates/ty_server/tests/e2e/snapshots/e2e__pull_diagnostics__main.snap index 8342b9c6b6fe0..d510246b33d6f 100644 --- a/crates/ty_server/tests/e2e/snapshots/e2e__pull_diagnostics__main.snap +++ b/crates/ty_server/tests/e2e/snapshots/e2e__pull_diagnostics__main.snap @@ -6,25 +6,6 @@ expression: main_diagnostics "kind": "full", "resultId": "[RESULT_ID]", "items": [ - { - "range": { - "start": { - "line": 0, - "character": 0 - }, - "end": { - "line": 0, - "character": 11 - } - }, - "severity": 2, - "code": "undefined-reveal", - "codeDescription": { - "href": "https://ty.dev/rules#undefined-reveal" - }, - "source": "ty", - "message": "`reveal_type` used without importing it" - }, { "range": { "start": { From a8aef2a14875241b56aba971dbbb69465c299f36 Mon Sep 17 00:00:00 2001 From: David Peter Date: Mon, 16 Feb 2026 15:42:31 +0100 Subject: [PATCH 2/3] Revert change in e2e__pull_diagnostics__main.snap --- .../e2e__pull_diagnostics__main.snap | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/crates/ty_server/tests/e2e/snapshots/e2e__pull_diagnostics__main.snap b/crates/ty_server/tests/e2e/snapshots/e2e__pull_diagnostics__main.snap index d510246b33d6f..8342b9c6b6fe0 100644 --- a/crates/ty_server/tests/e2e/snapshots/e2e__pull_diagnostics__main.snap +++ b/crates/ty_server/tests/e2e/snapshots/e2e__pull_diagnostics__main.snap @@ -6,6 +6,25 @@ expression: main_diagnostics "kind": "full", "resultId": "[RESULT_ID]", "items": [ + { + "range": { + "start": { + "line": 0, + "character": 0 + }, + "end": { + "line": 0, + "character": 11 + } + }, + "severity": 2, + "code": "undefined-reveal", + "codeDescription": { + "href": "https://ty.dev/rules#undefined-reveal" + }, + "source": "ty", + "message": "`reveal_type` used without importing it" + }, { "range": { "start": { From 1fb5ff1197c07a0419707ccd6577dc5787e5ed07 Mon Sep 17 00:00:00 2001 From: David Peter Date: Mon, 16 Feb 2026 15:52:40 +0100 Subject: [PATCH 3/3] Fix snapshot --- ...d_-_`assert_type`_-_Unspellable_types_(385d082f9803b184).snap | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/ty_python_semantic/resources/mdtest/snapshots/assert_type.md_-_`assert_type`_-_Unspellable_types_(385d082f9803b184).snap b/crates/ty_python_semantic/resources/mdtest/snapshots/assert_type.md_-_`assert_type`_-_Unspellable_types_(385d082f9803b184).snap index c984648b3e7b6..9a32be1187f27 100644 --- a/crates/ty_python_semantic/resources/mdtest/snapshots/assert_type.md_-_`assert_type`_-_Unspellable_types_(385d082f9803b184).snap +++ b/crates/ty_python_semantic/resources/mdtest/snapshots/assert_type.md_-_`assert_type`_-_Unspellable_types_(385d082f9803b184).snap @@ -2,6 +2,7 @@ source: crates/ty_test/src/lib.rs expression: snapshot --- + --- mdtest name: assert_type.md - `assert_type` - Unspellable types mdtest path: crates/ty_python_semantic/resources/mdtest/directives/assert_type.md