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,
)
| 12 | |
| 13 | |
| 14 | def 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 | |
| 59 | def load_from_raw( |
searching dependent graphs…