Load AST from parsed binary data and report stored errors. If imports_only is true, only deserialize imports and return a mostly empty AST.
(
fnam: str,
module: str | None,
raw_data: FileRawData,
errors: Errors,
options: Options,
imports_only: bool = False,
)
| 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) |
| 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: |
no test coverage detected
searching dependent graphs…