1313MYPY_TYPES_DIR = REPO_ROOT / "tests" / "mypy_types"
1414
1515_INVALID_FIXTURE = "message_dict_invalid.py"
16- _INVALID_ERROR_LINES = ( 6 , 9 )
16+ _EXPECT_MARKER = "expect: typeddict-item"
1717
1818
1919def _run_mypy_on (path : Path ) -> subprocess .CompletedProcess [str ]:
@@ -31,6 +31,7 @@ def _run_mypy_on(path: Path) -> subprocess.CompletedProcess[str]:
3131 capture_output = True ,
3232 text = True ,
3333 check = False ,
34+ timeout = 60.0 ,
3435 )
3536
3637
@@ -39,6 +40,19 @@ def _typeddict_item_error_lines(output: str, fixture_name: str) -> set[int]:
3940 return {int (match .group (1 )) for match in re .finditer (pattern , output )}
4041
4142
43+ def _invalid_fixture_expectations (fixture_path : Path ) -> tuple [set [int ], list [str ]]:
44+ expected_lines : set [int ] = set ()
45+ expected_tokens : list [str ] = []
46+ for lineno , line in enumerate (fixture_path .read_text (encoding = "utf-8" ).splitlines (), start = 1 ):
47+ if _EXPECT_MARKER not in line :
48+ continue
49+ expected_lines .add (lineno )
50+ suffix = line .split (_EXPECT_MARKER , 1 )[1 ].strip ()
51+ if suffix :
52+ expected_tokens .append (suffix )
53+ return expected_lines , expected_tokens
54+
55+
4256@pytest .mark .parametrize (
4357 ("fixture_name" , "should_pass" ),
4458 [
@@ -47,14 +61,18 @@ def _typeddict_item_error_lines(output: str, fixture_name: str) -> set[int]:
4761 ],
4862)
4963def test_message_dict_mypy_fixtures (fixture_name : str , should_pass : bool ) -> None :
50- result = _run_mypy_on (MYPY_TYPES_DIR / fixture_name )
64+ fixture_path = MYPY_TYPES_DIR / fixture_name
65+ result = _run_mypy_on (fixture_path )
5166 output = result .stdout + result .stderr
5267 if should_pass :
5368 assert result .returncode == 0 , output
5469 else :
5570 assert result .returncode != 0
5671 error_lines = _typeddict_item_error_lines (output , fixture_name )
57- assert error_lines >= set (_INVALID_ERROR_LINES ), (
58- f"expected typeddict-item on lines { _INVALID_ERROR_LINES } , "
72+ expected_lines , expected_tokens = _invalid_fixture_expectations (fixture_path )
73+ assert error_lines >= expected_lines , (
74+ f"expected typeddict-item on lines { sorted (expected_lines )} , "
5975 f"got { sorted (error_lines )} : { output } "
6076 )
77+ for token in expected_tokens :
78+ assert token in output , f"expected mypy output to mention { token !r} : { output } "
0 commit comments