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

Function read_statement

mypy/nativeparse.py:281–558  ·  view source on GitHub ↗
(state: State, data: ReadBuffer)

Source from the content-addressed store, hash-verified

279
280
281def read_statement(state: State, data: ReadBuffer) -> Statement:
282 # Branches ordered by frequency (based on mypy self-check)
283 tag = read_tag(data)
284 stmt: Statement
285 if tag == nodes.ASSIGNMENT_STMT:
286 lvalues = read_expression_list(state, data)
287 rvalue = read_expression(state, data)
288 has_type = read_bool(data)
289 if has_type:
290 type_annotation = read_type(state, data)
291 else:
292 type_annotation = None
293 new_syntax = read_bool(data)
294 a = AssignmentStmt(lvalues, rvalue, type=type_annotation, new_syntax=new_syntax)
295 read_loc(data, a)
296 # If rvalue is TempNode, copy location from AssignmentStmt
297 if isinstance(rvalue, TempNode):
298 set_line_column_range(rvalue, a)
299 expect_end_tag(data)
300 return a
301 elif tag == nodes.EXPR_STMT:
302 es = ExpressionStmt(read_expression(state, data))
303 set_line_column_range(es, es.expr)
304 expect_end_tag(data)
305 return es
306 elif tag == nodes.IF_STMT:
307 expr = read_expression(state, data)
308 body = read_block(state, data)
309
310 num_elif = read_int(data)
311 elif_exprs = []
312 elif_bodies = []
313 for i in range(num_elif):
314 elif_exprs.append(read_expression(state, data))
315 elif_bodies.append(read_block(state, data))
316
317 has_else = read_bool(data)
318 if has_else:
319 else_body = read_block(state, data)
320 else:
321 else_body = None
322
323 # Normalize elif into nested if/else statements
324 # Build from the bottom up, starting with the final else body
325 current_else = else_body
326
327 for elif_expr, elif_body in reversed(list(zip(elif_exprs, elif_bodies))):
328 elif_stmt = IfStmt([elif_expr], [elif_body], current_else)
329 elif_stmt.line = elif_expr.line
330 elif_stmt.column = elif_expr.column
331 if current_else is not None:
332 elif_stmt.end_line = current_else.end_line
333 elif_stmt.end_column = current_else.end_column
334 else:
335 elif_stmt.end_line = elif_body.end_line
336 elif_stmt.end_column = elif_body.end_column
337
338 current_else = Block([elif_stmt])

Callers 2

read_statementsFunction · 0.85
read_optional_blockFunction · 0.85

Calls 15

AssignmentStmtClass · 0.90
ExpressionStmtClass · 0.90
read_intFunction · 0.90
IfStmtClass · 0.90
BlockClass · 0.90
ReturnStmtClass · 0.90
read_strFunction · 0.90
ImportFromClass · 0.90
ForStmtClass · 0.90
AssertStmtClass · 0.90
VarClass · 0.90
DecoratorClass · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…