MCPcopy
hub / github.com/python/mypy / parse

Function parse

mypy/parse.py:14–56  ·  view source on GitHub ↗

Parse a source file, without doing any semantic analysis. Return the parse tree, use the errors object to report parse errors. The python_version (major, minor) option determines the Python syntax variant. New parser returns empty tree with serialized data. To get the full tree and

(
    source: str | bytes,
    fnam: str,
    module: str | None,
    errors: Errors,
    options: Options,
    file_exists: bool,
    eager: bool = False,
)

Source from the content-addressed store, hash-verified

12
13
14def parse(
15 source: str | bytes,
16 fnam: str,
17 module: str | None,
18 errors: Errors,
19 options: Options,
20 file_exists: bool,
21 eager: bool = False,
22) -> MypyFile:
23 """Parse a source file, without doing any semantic analysis.
24
25 Return the parse tree, use the errors object to report parse errors.
26 The python_version (major, minor) option determines the Python syntax variant.
27
28 New parser returns empty tree with serialized data. To get the full tree and
29 the parse errors, use eager=True.
30 """
31 if options.native_parser:
32 # Native parser only works with actual files on disk
33 # Fall back to fastparse for in-memory source or non-existent files
34 if file_exists:
35 import mypy.nativeparse
36
37 ignore_errors = options.ignore_errors or fnam in errors.ignored_files
38 # If errors are ignored, we can drop many function bodies to speed up type checking.
39 strip_function_bodies = ignore_errors and not options.preserve_asts
40 tree, _, _ = mypy.nativeparse.native_parse(
41 fnam, options, skip_function_bodies=strip_function_bodies
42 )
43 # Set is_stub based on file extension
44 tree.is_stub = fnam.endswith(".pyi")
45 # Note: tree.imports is populated directly by load_from_raw() with deserialized
46 # import metadata, so we don't need to collect imports via AST traversal
47 if eager and tree.raw_data is not None:
48 tree = load_from_raw(fnam, module, tree.raw_data, errors, options)
49 return tree
50 # Fall through to fastparse for non-existent files
51
52 if options.transform_source is not None:
53 source = options.transform_source(source)
54 import mypy.fastparse
55
56 return mypy.fastparse.parse(source, fnam=fnam, module=module, errors=errors, options=options)
57
58
59def load_from_raw(

Callers 5

parse_fileMethod · 0.90
apply_field_accessorsMethod · 0.90
test_parserFunction · 0.90
test_parse_errorFunction · 0.90
dumpFunction · 0.90

Calls 2

load_from_rawFunction · 0.85
endswithMethod · 0.45

Tested by 2

test_parserFunction · 0.72
test_parse_errorFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…