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

Function parse

mypy/fastparse.py:187–272  ·  view source on GitHub ↗

Parse a source file, without doing any semantic analysis. Return the parse tree. If errors is not provided, raise ParseError on failure. Otherwise, use the errors object to report parse errors.

(
    source: str | bytes,
    fnam: str,
    module: str | None,
    errors: Errors,
    options: Options | None = None,
)

Source from the content-addressed store, hash-verified

185
186
187def parse(
188 source: str | bytes,
189 fnam: str,
190 module: str | None,
191 errors: Errors,
192 options: Options | None = None,
193) -> MypyFile:
194 """Parse a source file, without doing any semantic analysis.
195
196 Return the parse tree. If errors is not provided, raise ParseError
197 on failure. Otherwise, use the errors object to report parse errors.
198 """
199 ignore_errors = (options is not None and options.ignore_errors) or (
200 fnam in errors.ignored_files
201 )
202 # If errors are ignored, we can drop many function bodies to speed up type checking.
203 strip_function_bodies = ignore_errors and (options is None or not options.preserve_asts)
204
205 if options is None:
206 options = Options()
207 errors.set_file(fnam, module, options=options)
208 is_stub_file = fnam.endswith(".pyi")
209 if is_stub_file:
210 feature_version = defaults.PYTHON3_VERSION[1]
211 if options.python_version[0] == 3 and options.python_version[1] > feature_version:
212 feature_version = options.python_version[1]
213 else:
214 assert options.python_version[0] >= 3
215 feature_version = options.python_version[1]
216 try:
217 # Disable
218 # - deprecation warnings for 'invalid escape sequence' (Python 3.11 and below)
219 # - syntax warnings for 'invalid escape sequence' (3.12+) and 'return in finally' (3.14+)
220 with warnings.catch_warnings():
221 warnings.filterwarnings("ignore", category=DeprecationWarning)
222 warnings.filterwarnings("ignore", category=SyntaxWarning)
223 ast = ast3_parse(source, fnam, "exec", feature_version=feature_version)
224
225 tree = ASTConverter(
226 options=options,
227 is_stub=is_stub_file,
228 errors=errors,
229 strip_function_bodies=strip_function_bodies,
230 path=fnam,
231 ).visit(ast)
232
233 except RecursionError as e:
234 # For very complex expressions it is possible to hit recursion limit
235 # before reaching a leaf node.
236 # Should reject at top level instead at bottom, since bottom would already
237 # be at the threshold of the recursion limit, and may fail again later.
238 # E.G. x1+x2+x3+...+xn -> BinOp(left=BinOp(left=BinOp(left=...
239 try:
240 # But to prove that is the cause of this particular recursion error,
241 # try to walk the tree using builtin visitor
242 ast3.NodeVisitor().visit(ast)
243 except RecursionError:
244 errors.report(

Callers

nothing calls this directly

Calls 12

OptionsClass · 0.90
MypyFileClass · 0.90
ast3_parseFunction · 0.85
ASTConverterClass · 0.85
isinstanceFunction · 0.85
set_fileMethod · 0.80
upperMethod · 0.80
groupMethod · 0.80
endswithMethod · 0.45
visitMethod · 0.45
reportMethod · 0.45
startswithMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…