PY-32793 PY-89314 python: Numpy - verify inferred return type via assert_type in tests

Passing a call result to a typed function silently accepts `Any`, so
regressions collapsing the docstring type to `Any` slip through.

Use `typing.assert_type` instead - it compares the inferred and expected
types exactly.

(cherry picked from commit c2d7f45626bb7284e50097cb912d5ec3714ac1c3)

GitOrigin-RevId: 10acb7748548befaf129a3e5cd41786e163e5505
This commit is contained in:
Nikita Paniukhin
2026-08-18 20:28:08 +00:00
committed by intellij-monorepo-bot
parent 6579a64a07
commit 2c651ace18
4 changed files with 15 additions and 21 deletions
@@ -1,4 +1,4 @@
from typing import Callable
from typing import Callable, assert_type
def test_explicit_type() -> Callable[[str, str, str], str]:
@@ -32,9 +32,6 @@ def test_numpy_docstring():
...
def expect_callable(cb: Callable[[str, str, str], str]): ...
def expect_int(x: int): ...
expect_callable(test_explicit_type())
expect_callable(test_explicit_type_comment())
expect_int(test_numpy_docstring())
assert_type(test_explicit_type(), Callable[[str, str, str], str])
assert_type(test_explicit_type_comment(), Callable[[str, str, str], str])
assert_type(test_numpy_docstring(), int)
@@ -1,3 +1,6 @@
from typing import assert_type
def test_explicit_union():
"""
Returns
@@ -34,10 +37,7 @@ def test_pep604_union():
...
def expect_int_or_str(x: int | str): ...
def expect_list_or_tuple(x: list[int] | tuple[int, int]): ...
expect_int_or_str(test_explicit_union())
expect_int_or_str(test_implicit_union())
expect_list_or_tuple(test_union_with_parameterized())
expect_int_or_str(test_pep604_union())
assert_type(test_explicit_union(), int | str)
assert_type(test_implicit_union(), int | str)
assert_type(test_union_with_parameterized(), list[int] | tuple[int, int])
assert_type(test_pep604_union(), int | str)
@@ -1,4 +1,4 @@
from typing import Callable
from typing import Callable, assert_type
def test_callable_from_docstring():
@@ -19,8 +19,5 @@ def test_triple_tuple_from_docstring():
...
def expect_callable(cb: Callable[[str, str, str], str]): ...
def expect_triple_tuple(t: tuple[int, int, int]): ...
expect_callable(test_callable_from_docstring())
expect_triple_tuple(test_triple_tuple_from_docstring())
assert_type(test_callable_from_docstring(), Callable[[str, str, str], str])
assert_type(test_triple_tuple_from_docstring(), tuple[int, int, int])
@@ -42,7 +42,7 @@ public class PyNumpyTypeTest extends PyTestCase {
settings.setFormat(DocStringFormat.NUMPY);
}
myFixture.configureByFile(TEST_DIRECTORY + getTestName(false) + ".py");
myFixture.enableInspections(PyTypeCheckerInspection.class);
myFixture.enableInspections(PyTypeCheckerInspection.class, PyAssertTypeInspection.class);
myFixture.checkHighlighting(true, false, true);
}
finally {