Skip to content

Commit 2e793ae

Browse files
committed
Enable native parsing to use source directly
1 parent be46935 commit 2e793ae

2 files changed

Lines changed: 29 additions & 8 deletions

File tree

mypy/nativeparse.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,10 @@ def add_error(
189189

190190

191191
def native_parse(
192-
filename: str, options: Options, skip_function_bodies: bool = False
192+
filename: str,
193+
options: Options,
194+
source: str | bytes | None = None,
195+
skip_function_bodies: bool = False,
193196
) -> tuple[MypyFile, list[ParseError], TypeIgnores]:
194197
"""Parse a Python file using the native Rust-based parser.
195198
@@ -218,7 +221,7 @@ def native_parse(
218221
uses_template_strings,
219222
source_hash,
220223
mypy_comments,
221-
) = parse_to_binary_ast(filename, options, skip_function_bodies)
224+
) = parse_to_binary_ast(filename, options, source, skip_function_bodies)
222225
node = MypyFile([], [])
223226
node.path = filename
224227
node.raw_data = FileRawData(
@@ -255,7 +258,10 @@ def read_statements(state: State, data: ReadBuffer, n: int) -> list[Statement]:
255258

256259

257260
def parse_to_binary_ast(
258-
filename: str, options: Options, skip_function_bodies: bool = False
261+
filename: str,
262+
options: Options,
263+
source: str | bytes | None = None,
264+
skip_function_bodies: bool = False,
259265
) -> tuple[bytes, list[ParseError], TypeIgnores, bytes, bool, bool, str, list[tuple[int, str]]]:
260266
# This is a horrible hack to work around a mypyc bug where imported
261267
# module may be not ready in a thread sometimes.
@@ -266,6 +272,7 @@ def parse_to_binary_ast(
266272
raise ImportError("Cannot import ast_serialize")
267273
ast_bytes, errors, ignores, import_bytes, ast_data = ast_serialize.parse(
268274
filename,
275+
source,
269276
skip_function_bodies=skip_function_bodies,
270277
python_version=options.python_version,
271278
platform=options.platform,

mypy/test/test_nativeparse.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def format_reachable_imports(node: MypyFile) -> list[str]:
234234

235235
@unittest.skipUnless(has_nativeparse, "nativeparse not available")
236236
class TestNativeParserBinaryFormat(unittest.TestCase):
237-
def test_trivial_binary_data(self) -> None:
237+
def _assert_trivial_binary_data(self, b: bytes, /) -> None:
238238
# A quick sanity check to ensure the serialized data looks as expected. Only covers
239239
# a few AST nodes.
240240

@@ -250,9 +250,9 @@ def locs(start_line: int, start_column: int, end_line: int, end_column: int) ->
250250
int_enc(end_column - start_column),
251251
]
252252

253-
with temp_source("print('hello')") as fnam:
254-
b, _, _, _, _, _, _, _ = parse_to_binary_ast(fnam, Options())
255-
assert list(b) == (
253+
self.assertEqual(
254+
list(b),
255+
(
256256
[LITERAL_INT, 22, nodes.EXPR_STMT, nodes.CALL_EXPR]
257257
+ [nodes.NAME_EXPR, LITERAL_STR]
258258
+ [int_enc(5)]
@@ -269,7 +269,21 @@ def locs(start_line: int, start_column: int, end_line: int, end_column: int) ->
269269
+ [LIST_GEN, 22, LITERAL_NONE]
270270
+ locs(1, 0, 1, 14)
271271
+ [END_TAG, END_TAG]
272-
)
272+
),
273+
)
274+
275+
def test_trivial_binary_data_from_file(self) -> None:
276+
with temp_source("print('hello')") as fnam:
277+
b, _, _, _, _, _, _, _ = parse_to_binary_ast(fnam, Options())
278+
self._assert_trivial_binary_data(b)
279+
280+
def test_trivial_binary_data_from_string_source(self) -> None:
281+
b, _, _, _, _, _, _, _ = parse_to_binary_ast("", Options(), "print('hello')")
282+
self._assert_trivial_binary_data(b)
283+
284+
def test_trivial_binary_data_from_bytes_source(self) -> None:
285+
b, _, _, _, _, _, _, _ = parse_to_binary_ast("", Options(), b"print('hello')")
286+
self._assert_trivial_binary_data(b)
273287

274288

275289
@contextlib.contextmanager

0 commit comments

Comments
 (0)