GeezerVison is showing github.com — Refactor to allow a safer `source=None` · python/mypy@b029c44
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: mypy/build.py +15-6Lines changed: 15 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 163 | 163 | from mypy.modules_state import modules_state | 164 | 164 | from mypy.nodes import Expression | 165 | 165 | from mypy.options import Options | 166 | | -from mypy.parse import load_from_raw, parse | | 166 | +from mypy.parse import load_from_raw, parse, parse_native | 167 | 167 | from mypy.plugin import ChainedPlugin, Plugin, ReportConfigContext | 168 | 168 | from mypy.plugins.default import DefaultPlugin | 169 | 169 | from mypy.renaming import LimitedVariableRenameVisitor, VariableRenameVisitor | @@ -1114,7 +1114,9 @@ def parse_files_threaded_raw(self, states: list[State]) -> tuple[list[State], se | 1114 | 1114 | ignore_errors = state.ignore_all or state.options.ignore_errors | 1115 | 1115 | if ignore_errors: | 1116 | 1116 | self.errors.ignored_files.add(state.xpath) | 1117 | | - futures.append(executor.submit(state.parse_file_inner, state.source)) | | 1117 | + futures.append( | | 1118 | + executor.submit(state.parse_file_inner, state.source, parallel=True) | | 1119 | + ) | 1118 | 1120 | parallel_parsed_states.append(state) | 1119 | 1121 | parallel_parsed_states_set.add(state) | 1120 | 1122 | else: | @@ -1264,9 +1266,10 @@ def parse_file( | 1264 | 1266 | self, | 1265 | 1267 | id: str, | 1266 | 1268 | path: str, | 1267 | | - source: str, | | 1269 | + source: str | None, | 1268 | 1270 | options: Options, | 1269 | 1271 | raw_data: FileRawData | None = None, | | 1272 | + parallel: bool = False, | 1270 | 1273 | ) -> MypyFile: | 1271 | 1274 | """Parse the source of a file with the given name. | 1272 | 1275 |
| @@ -1277,7 +1280,11 @@ def parse_file( | 1277 | 1280 | # If possible, deserialize from known binary data instead of parsing from scratch. | 1278 | 1281 | tree = load_from_raw(path, id, raw_data, self.errors, options) | 1279 | 1282 | else: | 1280 | | - tree = parse(source, path, id, self.errors, options=options) | | 1283 | + if source is not None: | | 1284 | + tree = parse(source, path, id, self.errors, options=options) | | 1285 | + else: | | 1286 | + assert parallel | | 1287 | + tree = parse_native(source, path, id, self.errors, options=options) | 1281 | 1288 | tree._fullname = id | 1282 | 1289 | if self.stats_enabled: | 1283 | 1290 | with self.stats_lock: | @@ -3176,10 +3183,12 @@ def get_source(self) -> str: | 3176 | 3183 | self.time_spent_us += time_spent_us(t0) | 3177 | 3184 | return source | 3178 | 3185 |
| 3179 | | - def parse_file_inner(self, source: str, raw_data: FileRawData | None = None) -> None: | | 3186 | + def parse_file_inner( | | 3187 | + self, source: str | None, raw_data: FileRawData | None = None, parallel: bool = False | | 3188 | + ) -> None: | 3180 | 3189 | t0 = time_ref() | 3181 | 3190 | self.tree = self.manager.parse_file( | 3182 | | - self.id, self.xpath, source, options=self.options, raw_data=raw_data | | 3191 | + self.id, self.xpath, source, self.options, raw_data, parallel | 3183 | 3192 | ) | 3184 | 3193 | self.time_spent_us += time_spent_us(t0) | 3185 | 3194 |
| |
Collapse file Copy file name to clipboardExpand all lines: mypy/parse.py +26-15Lines changed: 26 additions & 15 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -28,21 +28,7 @@ def parse( | 28 | 28 | the parse errors, use eager=True. | 29 | 29 | """ | 30 | 30 | if options.native_parser: | 31 | | - import mypy.nativeparse | 32 | | - | 33 | | - ignore_errors = options.ignore_errors or fnam in errors.ignored_files | 34 | | - # If errors are ignored, we can drop many function bodies to speed up type checking. | 35 | | - strip_function_bodies = ignore_errors and not options.preserve_asts | 36 | | - tree, _, _ = mypy.nativeparse.native_parse( | 37 | | - fnam, options, source, skip_function_bodies=strip_function_bodies | 38 | | - ) | 39 | | - # Set is_stub based on file extension | 40 | | - tree.is_stub = fnam.endswith(".pyi") | 41 | | - # Note: tree.imports is populated directly by load_from_raw() with deserialized | 42 | | - # import metadata, so we don't need to collect imports via AST traversal | 43 | | - if eager and tree.raw_data is not None: | 44 | | - tree = load_from_raw(fnam, module, tree.raw_data, errors, options) | 45 | | - return tree | | 31 | + return parse_native(source, fnam, module, errors, options, eager) | 46 | 32 |
| 47 | 33 | if options.transform_source is not None: | 48 | 34 | source = options.transform_source(source) | @@ -97,6 +83,31 @@ def load_from_raw( | 97 | 83 | return tree | 98 | 84 |
| 99 | 85 |
| | 86 | +def parse_native( | | 87 | + source: str | bytes | None, | | 88 | + fnam: str, | | 89 | + module: str | None, | | 90 | + errors: Errors, | | 91 | + options: Options, | | 92 | + eager: bool = False, | | 93 | +) -> MypyFile: | | 94 | + import mypy.nativeparse | | 95 | + | | 96 | + ignore_errors = options.ignore_errors or fnam in errors.ignored_files | | 97 | + # If errors are ignored, we can drop many function bodies to speed up type checking. | | 98 | + strip_function_bodies = ignore_errors and not options.preserve_asts | | 99 | + tree, _, _ = mypy.nativeparse.native_parse( | | 100 | + fnam, options, source, skip_function_bodies=strip_function_bodies | | 101 | + ) | | 102 | + # Set is_stub based on file extension | | 103 | + tree.is_stub = fnam.endswith(".pyi") | | 104 | + # Note: tree.imports is populated directly by load_from_raw() with deserialized | | 105 | + # import metadata, so we don't need to collect imports via AST traversal | | 106 | + if eager and tree.raw_data is not None: | | 107 | + tree = load_from_raw(fnam, module, tree.raw_data, errors, options) | | 108 | + return tree | | 109 | + | | 110 | + | 100 | 111 | def report_parse_error(error: ParseError, errors: Errors) -> None: | 101 | 112 | message = error["message"] | 102 | 113 | # Standardize error message by capitalizing the first word | |
|