Skip to content

Commit 10e9494

Browse files
committed
Enable native parsing to use source directly
1 parent dee7b3d commit 10e9494

2 files changed

Lines changed: 30 additions & 8 deletions

File tree

mypy/nativeparse.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,11 @@ def add_error(
190190

191191

192192
def native_parse(
193-
filename: str, options: Options, skip_function_bodies: bool = False, imports_only: bool = False
193+
filename: str,
194+
options: Options,
195+
source: str | bytes | None = None,
196+
skip_function_bodies: bool = False,
197+
imports_only: bool = False,
194198
) -> tuple[MypyFile, list[ParseError], TypeIgnores]:
195199
"""Parse a Python file using the native Rust-based parser.
196200
@@ -208,7 +212,7 @@ def native_parse(
208212
return node, [], []
209213

210214
b, errors, ignores, import_bytes, is_partial_package, uses_template_strings = (
211-
parse_to_binary_ast(filename, options, skip_function_bodies)
215+
parse_to_binary_ast(filename, options, source, skip_function_bodies)
212216
)
213217
data = ReadBuffer(b)
214218
n = read_int(data)
@@ -253,7 +257,10 @@ def read_statements(state: State, data: ReadBuffer, n: int) -> list[Statement]:
253257

254258

255259
def parse_to_binary_ast(
256-
filename: str, options: Options, skip_function_bodies: bool = False
260+
filename: str,
261+
options: Options,
262+
source: str | bytes | None = None,
263+
skip_function_bodies: bool = False,
257264
) -> tuple[bytes, list[ParseError], TypeIgnores, bytes, bool, bool]:
258265
# This is a horrible hack to work around a mypyc bug where imported
259266
# module may be not ready in a thread sometimes.
@@ -264,6 +271,7 @@ def parse_to_binary_ast(
264271
raise ImportError("Cannot import ast_serialize")
265272
ast_bytes, errors, ignores, import_bytes, ast_data = ast_serialize.parse(
266273
filename,
274+
source,
267275
skip_function_bodies=skip_function_bodies,
268276
python_version=options.python_version,
269277
platform=options.platform,

mypy/test/test_nativeparse.py

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

213213
@unittest.skipUnless(has_nativeparse, "nativeparse not available")
214214
class TestNativeParserBinaryFormat(unittest.TestCase):
215-
def test_trivial_binary_data(self) -> None:
215+
def _assert_trivial_binary_data(self, b: bytes) -> None:
216216
# A quick sanity check to ensure the serialized data looks as expected. Only covers
217217
# a few AST nodes.
218218

@@ -228,9 +228,9 @@ def locs(start_line: int, start_column: int, end_line: int, end_column: int) ->
228228
int_enc(end_column - start_column),
229229
]
230230

231-
with temp_source("print('hello')") as fnam:
232-
b, _, _, _, _, _ = parse_to_binary_ast(fnam, Options())
233-
assert list(b) == (
231+
self.assertEqual(
232+
list(b),
233+
(
234234
[LITERAL_INT, 22, nodes.EXPR_STMT, nodes.CALL_EXPR]
235235
+ [nodes.NAME_EXPR, LITERAL_STR]
236236
+ [int_enc(5)]
@@ -247,7 +247,21 @@ def locs(start_line: int, start_column: int, end_line: int, end_column: int) ->
247247
+ [LIST_GEN, 22, LITERAL_NONE]
248248
+ locs(1, 0, 1, 14)
249249
+ [END_TAG, END_TAG]
250-
)
250+
),
251+
)
252+
253+
def test_trivial_binary_data_from_file(self) -> None:
254+
with temp_source("print('hello')") as fnam:
255+
b, _, _, _, _, _ = parse_to_binary_ast(fnam, Options())
256+
self._assert_trivial_binary_data(b)
257+
258+
def test_trivial_binary_data_from_string_source(self) -> None:
259+
b, _, _, _, _, _ = parse_to_binary_ast("", Options(), "print('hello')")
260+
self._assert_trivial_binary_data(b)
261+
262+
def test_trivial_binary_data_from_bytes_source(self) -> None:
263+
b, _, _, _, _, _ = parse_to_binary_ast("", Options(), b"print('hello')")
264+
self._assert_trivial_binary_data(b)
251265

252266

253267
@contextlib.contextmanager

0 commit comments

Comments
 (0)