1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
# encoding=utf-8
from typing import List, NamedTuple
import pytest
from pytest_mypy_plugins import utils
from pytest_mypy_plugins.utils import (
OutputMatcher,
TypecheckAssertionError,
assert_expected_matched_actual,
extract_output_matchers_from_comments,
sorted_by_file_and_line,
)
class ExpectMatchedActualTestData(NamedTuple):
source_lines: List[str]
actual_lines: List[str]
expected_message_lines: List[str]
def test_render_template_with_None_value() -> None:
# Given
template = "{{ a }} {{ b }}"
data = {"a": None, "b": 99}
# When
actual = utils.render_template(template=template, data=data)
# Then
assert actual == "None 99"
expect_matched_actual_data = [
ExpectMatchedActualTestData(
[
'''reveal_type(42) # N: Revealed type is "Literal['foo']?"''',
'''reveal_type("foo") # N: Revealed type is "Literal[42]?"''',
],
['''main:1: note: Revealed type is "Literal[42]?"''', '''main:2: note: Revealed type is "Literal['foo']?"'''],
[
"""Invalid output: """,
"""Actual:""",
""" main:1: note: Revealed type is "Literal[42]?" (diff)""",
""" main:2: note: Revealed type is "Literal['foo']?" (diff)""",
"""Expected:""",
""" main:1: note: Revealed type is "Literal['foo']?" (diff)""",
""" main:2: note: Revealed type is "Literal[42]?" (diff)""",
"""Alignment of first line difference:""",
''' E: ...ed type is "Literal['foo']?"''',
''' A: ...ed type is "Literal[42]?"''',
""" ^""",
],
),
ExpectMatchedActualTestData(
[
"""reveal_type(42)""",
'''reveal_type("foo") # N: Revealed type is "Literal['foo']?"''',
],
['''main:1: note: Revealed type is "Literal[42]?"''', '''main:2: note: Revealed type is "Literal['foo']?"'''],
[
"""Invalid output: """,
"""Actual:""",
""" main:1: note: Revealed type is "Literal[42]?" (diff)""",
""" main:2: note: Revealed type is "Literal['foo']?" (diff)""",
"""Expected:""",
""" main:2: note: Revealed type is "Literal['foo']?" (diff)""",
"""Alignment of first line difference:""",
''' E: main:2: note: Revealed type is "Literal['foo']?"''',
''' A: main:1: note: Revealed type is "Literal[42]?"''',
""" ^""",
],
),
ExpectMatchedActualTestData(
['''reveal_type(42) # N: Revealed type is "Literal[42]?"''', """reveal_type("foo")"""],
['''main:1: note: Revealed type is "Literal[42]?"''', '''main:2: note: Revealed type is "Literal['foo']?"'''],
[
"""Invalid output: """,
"""Actual:""",
""" main:2: note: Revealed type is "Literal['foo']?" (diff)""",
"""Expected:""",
""" (empty)""",
],
),
ExpectMatchedActualTestData(
['''42 + "foo"'''],
["""main:1: error: Unsupported operand types for + ("int" and "str")"""],
[
"""Output is not expected: """,
"""Actual:""",
""" main:1: error: Unsupported operand types for + ("int" and "str") (diff)""",
"""Expected:""",
""" (empty)""",
],
),
ExpectMatchedActualTestData(
[""" 1 + 1 # E: Unsupported operand types for + ("int" and "int")"""],
[],
[
"""Invalid output: """,
"""Actual:""",
""" (empty)""",
"""Expected:""",
""" main:1: error: Unsupported operand types for + ("int" and "int") (diff)""",
],
),
ExpectMatchedActualTestData(
[
'''reveal_type(42) # N: Revealed type is "Literal[42]?"''',
'''reveal_type("foo") # N: Revealed type is "builtins.int"''',
],
['''main:1: note: Revealed type is "Literal[42]?"''', '''main:2: note: Revealed type is "Literal['foo']?"'''],
[
"""Invalid output: """,
"""Actual:""",
""" ...""",
""" main:2: note: Revealed type is "Literal['foo']?" (diff)""",
"""Expected:""",
""" ...""",
""" main:2: note: Revealed type is "builtins.int" (diff)""",
"""Alignment of first line difference:""",
''' E: ...te: Revealed type is "builtins.int"''',
''' A: ...te: Revealed type is "Literal['foo']?"''',
""" ^""",
],
),
ExpectMatchedActualTestData(
[
'''reveal_type(42) # N: Revealed type is "Literal[42]?"''',
'''reveal_type("foo") # N: Revealed type is "builtins.int"''',
],
['''main:1: note: Revealed type is "Literal[42]?"''', '''main:2: note: Revealed type is "Literal['foo']?"'''],
[
"""Invalid output: """,
"""Actual:""",
""" ...""",
""" main:2: note: Revealed type is "Literal['foo']?" (diff)""",
"""Expected:""",
""" ...""",
""" main:2: note: Revealed type is "builtins.int" (diff)""",
"""Alignment of first line difference:""",
''' E: ...te: Revealed type is "builtins.int"''',
''' A: ...te: Revealed type is "Literal['foo']?"''',
""" ^""",
],
),
ExpectMatchedActualTestData(
[
'''reveal_type(42.0) # N: Revealed type is "builtins.float"''',
'''reveal_type("foo") # N: Revealed type is "builtins.int"''',
'''reveal_type(42) # N: Revealed type is "Literal[42]?"''',
],
[
'''main:1: note: Revealed type is "builtins.float"''',
'''main:2: note: Revealed type is "Literal['foo']?"''',
'''main:3: note: Revealed type is "Literal[42]?"''',
],
[
"""Invalid output: """,
"""Actual:""",
""" ...""",
""" main:2: note: Revealed type is "Literal['foo']?" (diff)""",
""" ...""",
"""Expected:""",
""" ...""",
""" main:2: note: Revealed type is "builtins.int" (diff)""",
""" ...""",
"""Alignment of first line difference:""",
''' E: ...te: Revealed type is "builtins.int"''',
''' A: ...te: Revealed type is "Literal['foo']?"''',
""" ^""",
],
),
]
@pytest.mark.parametrize("source_lines,actual_lines,expected_message_lines", expect_matched_actual_data)
def test_assert_expected_matched_actual_failures(
source_lines: List[str], actual_lines: List[str], expected_message_lines: List[str]
) -> None:
expected: List[OutputMatcher] = extract_output_matchers_from_comments("main", source_lines, False)
expected_error_message = "\n".join(expected_message_lines)
with pytest.raises(TypecheckAssertionError) as e:
assert_expected_matched_actual(expected, actual_lines)
assert e.value.error_message.strip() == expected_error_message.strip()
@pytest.mark.parametrize(
"input_lines",
[
[
'''main:12: error: No overload variant of "f" matches argument type "List[int]"''',
"""main:12: note: Possible overload variants:""",
"""main:12: note: def f(x: int) -> int""",
"""main:12: note: def f(x: str) -> str""",
],
[
'''main_a:12: error: No overload variant of "g" matches argument type "List[int]"''',
'''main_b:12: error: No overload variant of "f" matches argument type "List[int]"''',
"""main_b:12: note: Possible overload variants:""",
"""main_a:12: note: def g(b: int) -> int""",
"""main_a:12: note: def g(a: int) -> int""",
"""main_b:12: note: def f(x: int) -> int""",
"""main_b:12: note: def f(x: str) -> str""",
],
],
)
def test_sorted_by_file_and_line_is_stable(input_lines: List[str]) -> None:
def lines_for_file(lines: List[str], fname: str) -> List[str]:
prefix = f"{fname}:"
return [line for line in lines if line.startswith(prefix)]
files = sorted({line.split(":", maxsplit=1)[0] for line in input_lines})
sorted_lines = sorted_by_file_and_line(input_lines)
for f in files:
assert lines_for_file(sorted_lines, f) == lines_for_file(input_lines, f)
|