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

Method parse_file

mypy/build.py:3202–3274  ·  view source on GitHub ↗

Parse file and run first pass of semantic analysis. Everything done here is local to the file. Don't depend on imported modules in any way. Logic here should be kept in sync with BuildManager.parse_all().

(self, *, temporary: bool = False, raw_data: FileRawData | None = None)

Source from the content-addressed store, hash-verified

3200 self.time_spent_us += time_spent_us(t0)
3201
3202 def parse_file(self, *, temporary: bool = False, raw_data: FileRawData | None = None) -> None:
3203 """Parse file and run first pass of semantic analysis.
3204
3205 Everything done here is local to the file. Don't depend on imported
3206 modules in any way. Logic here should be kept in sync with BuildManager.parse_all().
3207 """
3208 self.needs_parse = False
3209 tree = self.tree
3210 if tree is not None:
3211 # The file was already parsed.
3212 return
3213
3214 if raw_data is None:
3215 source = self.get_source()
3216 else:
3217 source = ""
3218 manager = self.manager
3219 # Can we reuse a previously parsed AST? This avoids redundant work in daemon.
3220 if self.id not in manager.ast_cache:
3221 self.manager.log(f"Parsing {self.xpath} ({self.id})")
3222 ignore_errors = self.ignore_all or self.options.ignore_errors
3223 if ignore_errors:
3224 self.manager.errors.ignored_files.add(self.xpath)
3225 with self.wrap_context():
3226 manager.errors.set_file(self.xpath, self.id, options=self.options)
3227 if raw_data is not None:
3228 # Apply inline mypy config before deserialization, since
3229 # some options (e.g. implicit_optional) affect how the
3230 # AST is built during deserialization.
3231 self.source_hash = raw_data.source_hash
3232 self.apply_inline_configuration(raw_data.mypy_comments)
3233 self.parse_file_inner(source, raw_data)
3234 assert self.tree is not None
3235 # New parser returns serialized trees that need to be de-serialized.
3236 if self.tree.raw_data is not None:
3237 assert raw_data is None
3238 self.tree = load_from_raw(
3239 self.xpath,
3240 self.id,
3241 self.tree.raw_data,
3242 manager.errors,
3243 self.options,
3244 imports_only=bool(self.manager.workers),
3245 )
3246 if manager.errors.is_blockers():
3247 manager.log("Bailing due to parse errors")
3248 manager.errors.raise_error()
3249 # Make a copy of any errors produced during parse time so that
3250 # fine-grained mode can repeat them when the module is
3251 # reprocessed.
3252 self.early_errors = list(manager.errors.error_info_map.get(self.xpath, []))
3253 self.semantic_analysis_pass1()
3254 else:
3255 # Reuse a cached AST
3256 manager.log(f"Using cached AST for {self.xpath} ({self.id})")
3257 self.tree, self.early_errors, source_hash = manager.ast_cache[self.id]
3258 self.source_hash = source_hash
3259

Callers 6

new_stateMethod · 0.95
parse_allMethod · 0.45
parse_file_innerMethod · 0.45
load_statesFunction · 0.45
update_module_isolatedFunction · 0.45

Calls 15

get_sourceMethod · 0.95
wrap_contextMethod · 0.95
parse_file_innerMethod · 0.95
check_blockersMethod · 0.95
setup_errorsMethod · 0.95
load_from_rawFunction · 0.90
boolClass · 0.85
listClass · 0.85
lenFunction · 0.85
set_fileMethod · 0.80

Tested by

no test coverage detected