Parse a Python file using the native Rust-based parser. Return (MypyFile, errors, type_ignores). The returned tree is empty with actual serialized data stored in `raw_data` attribute. Use read_statements() and/or deserialize_imports() to de-serialize. The caller should set these a
(
filename: str, options: Options, skip_function_bodies: bool = False
)
| 182 | |
| 183 | |
| 184 | def native_parse( |
| 185 | filename: str, options: Options, skip_function_bodies: bool = False |
| 186 | ) -> tuple[MypyFile, list[ParseError], TypeIgnores]: |
| 187 | """Parse a Python file using the native Rust-based parser. |
| 188 | |
| 189 | Return (MypyFile, errors, type_ignores). |
| 190 | |
| 191 | The returned tree is empty with actual serialized data stored in `raw_data` |
| 192 | attribute. Use read_statements() and/or deserialize_imports() to de-serialize. |
| 193 | |
| 194 | The caller should set these additional attributes on the returned MypyFile: |
| 195 | - ignored_lines: dict of type ignore comments (from the TypeIgnores return value) |
| 196 | - is_stub: whether the file is a .pyi stub |
| 197 | """ |
| 198 | # If the path is a directory, return empty AST (matching fastparse behavior) |
| 199 | # This can happen for packages that only contain .pyc files without source |
| 200 | if os.path.isdir(filename): |
| 201 | node = MypyFile([], []) |
| 202 | node.path = filename |
| 203 | return node, [], [] |
| 204 | |
| 205 | ( |
| 206 | b, |
| 207 | errors, |
| 208 | ignores, |
| 209 | import_bytes, |
| 210 | is_partial_package, |
| 211 | uses_template_strings, |
| 212 | source_hash, |
| 213 | mypy_comments, |
| 214 | ) = parse_to_binary_ast(filename, options, skip_function_bodies) |
| 215 | node = MypyFile([], []) |
| 216 | node.path = filename |
| 217 | node.raw_data = FileRawData( |
| 218 | b, |
| 219 | import_bytes, |
| 220 | errors, |
| 221 | dict(ignores), |
| 222 | is_partial_package, |
| 223 | uses_template_strings, |
| 224 | source_hash, |
| 225 | mypy_comments, |
| 226 | ) |
| 227 | return node, errors, ignores |
| 228 | |
| 229 | |
| 230 | def expect_end_tag(data: ReadBuffer) -> None: |
searching dependent graphs…