MCPcopy Index your code
hub / github.com/python/mypy / test_parser

Function test_parser

mypy/test/test_nativeparse.py:70–111  ·  view source on GitHub ↗

Perform a single native parser test case. The argument contains the description of the test case.

(testcase: DataDrivenTestCase)

Source from the content-addressed store, hash-verified

68
69
70def test_parser(testcase: DataDrivenTestCase) -> None:
71 """Perform a single native parser test case.
72
73 The argument contains the description of the test case.
74 """
75 options = Options()
76 options.hide_error_codes = True
77
78 if testcase.file.endswith("python310.test"):
79 options.python_version = (3, 10)
80 elif testcase.file.endswith("python312.test"):
81 options.python_version = (3, 12)
82 elif testcase.file.endswith("python313.test"):
83 options.python_version = (3, 13)
84 elif testcase.file.endswith("python314.test"):
85 options.python_version = (3, 14)
86 else:
87 options.python_version = defaults.PYTHON3_VERSION
88
89 source = "\n".join(testcase.input)
90
91 # Apply mypy: comments to options.
92 comments = get_mypy_comments(source)
93 changes, _ = parse_mypy_comments(comments, options)
94 options = options.apply_changes(changes)
95
96 # Check if we should skip function bodies (when ignoring errors)
97 skip_function_bodies = "# mypy: ignore-errors=True" in source
98
99 try:
100 with temp_source(source) as fnam:
101 node, errors, type_ignores = native_parse(fnam, options, skip_function_bodies)
102 errors += load_tree(node, options)
103 node.path = "main"
104 a = node.str_with_options(options).split("\n")
105 a = [format_error(err) for err in errors] + a
106 a = [format_ignore(ignore) for ignore in type_ignores] + a
107 except CompileError as e:
108 a = e.messages
109 assert_string_arrays_equal(
110 testcase.output, a, f"Invalid parser output ({testcase.file}, line {testcase.line})"
111 )
112
113
114def format_error(err: ParseError) -> str:

Callers 1

run_caseMethod · 0.70

Calls 14

apply_changesMethod · 0.95
OptionsClass · 0.90
get_mypy_commentsFunction · 0.90
parse_mypy_commentsFunction · 0.90
native_parseFunction · 0.90
temp_sourceFunction · 0.85
load_treeFunction · 0.85
format_errorFunction · 0.85
format_ignoreFunction · 0.85
splitMethod · 0.80
endswithMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…