Debian Codegraph @

mypy/parse.py

raw
1 from __future__ import annotations
2
3 import re
4
5 from librt.internal import ReadBuffer
6
7 from mypy import errorcodes as codes
8 from mypy.cache import read_int
9 from mypy.errors import Errors
10 from mypy.nodes import FileRawData, MypyFile, ParseError
11 from mypy.options import Options
12
13
14 def parse(
15 source: str | bytes | None,
16 fnam: str,
17 module: str | None,
18 errors: Errors,
19 options: Options,
20 eager: bool = False,
21 ) -> MypyFile:
22 """Parse a source file, without doing any semantic analysis.
23
24 Return the parse tree, use the errors object to report parse errors.
25 The python_version (major, minor) option determines the Python syntax variant.
26
27 New parser returns empty tree with serialized data. To get the full tree and
28 the parse errors, use eager=True.
29
30 `source` must not be `None` if the old parser is used. The new parser will read and
31 parse contents from path `fnam` if `source` is `None`.
32 """
33 if options.native_parser:
34 import mypy.nativeparse
35
36 ignore_errors = options.ignore_errors or fnam in errors.ignored_files
37 # If errors are ignored, we can drop many function bodies to speed up type checking.
38 strip_function_bodies = ignore_errors and not options.preserve_asts
39 tree, _, _ = mypy.nativeparse.native_parse(
40 fnam, options, source, skip_function_bodies=strip_function_bodies
41 )
42 # Set is_stub based on file extension
43 tree.is_stub = fnam.endswith(".pyi")
44 # Note: tree.imports is populated directly by load_from_raw() with deserialized
45 # import metadata, so we don't need to collect imports via AST traversal
46 if eager and tree.raw_data is not None:
47 tree = load_from_raw(fnam, module, tree.raw_data, errors, options)
48 return tree
49
50 if source is None:
51 raise ValueError("Source cannot be `None` when using the old parser")
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
59 def load_from_raw(
60 fnam: str,
61 module: str | None,
62 raw_data: FileRawData,
63 errors: Errors,
64 options: Options,
65 imports_only: bool = False,
66 ) -> MypyFile:
67 """Load AST from parsed binary data and report stored errors.
68
69 If imports_only is true, only deserialize imports and return a mostly
70 empty AST.
71 """
72 from mypy.nativeparse import State, deserialize_imports, read_statements
73
74 state = State(options, is_stub=fnam.endswith(".pyi"))
75 if imports_only:
76 defs = []
77 else:
78 data = ReadBuffer(raw_data.defs)
79 n = read_int(data)
80 defs = read_statements(state, data, n)
81 imports = deserialize_imports(raw_data.imports)
82
83 tree = MypyFile(defs, imports)
84 tree.path = fnam
85 tree.ignored_lines = raw_data.ignored_lines
86 tree.is_partial_stub_package = raw_data.is_partial_stub_package
87 tree.uses_template_strings = raw_data.uses_template_strings
88 tree.is_stub = fnam.endswith(".pyi")
89 if module is not None:
90 tree._fullname = module
91
92 # Report parse errors, this replicates the logic in parse().
93 all_errors = raw_data.raw_errors + state.errors
94 errors.set_file(fnam, module, options=options)
95 for error in all_errors:
96 # Note we never raise in this function, so it should not be called in coordinator.
97 report_parse_error(error, errors)
98 if imports_only:
99 # Preserve raw data when only de-serializing imports, it will be sent to
100 # the parallel workers.
101 tree.raw_data = raw_data
102 return tree
103
104
105 def report_parse_error(error: ParseError, errors: Errors) -> None:
106 message = error["message"]
107 # Standardize error message by capitalizing the first word
108 message = re.sub(r"^(\s*\w)", lambda m: m.group(1).upper(), message)
109 # Respect blocker status from error, default to True for syntax errors
110 is_blocker = error.get("blocker", True)
111 error_code = error.get("code")
112 if error_code is None:
113 error_code = codes.SYNTAX
114 else:
115 # Fallback to [syntax] for backwards compatibility.
116 error_code = codes.error_codes.get(error_code) or codes.SYNTAX
117 errors.report(error["line"], error["column"], message, blocker=is_blocker, code=error_code)