MCPcopy Index your code
hub / github.com/python/mypy / visit_with_stmt

Method visit_with_stmt

mypy/semanal.py:5559–5607  ·  view source on GitHub ↗
(self, s: WithStmt)

Source from the content-addressed store, hash-verified

5557 s.finally_body.accept(visitor)
5558
5559 def visit_with_stmt(self, s: WithStmt) -> None:
5560 self.statement = s
5561 types: list[Type] = []
5562
5563 if s.is_async:
5564 if not self.is_func_scope() or not self.function_stack[-1].is_coroutine:
5565 self.fail(message_registry.ASYNC_WITH_OUTSIDE_COROUTINE, s, code=codes.SYNTAX)
5566
5567 if s.unanalyzed_type:
5568 assert isinstance(s.unanalyzed_type, ProperType)
5569 actual_targets = [t for t in s.target if t is not None]
5570 if len(actual_targets) == 0:
5571 # We have a type for no targets
5572 self.fail('Invalid type comment: "with" statement has no targets', s)
5573 elif len(actual_targets) == 1:
5574 # We have one target and one type
5575 types = [s.unanalyzed_type]
5576 elif isinstance(s.unanalyzed_type, TupleType):
5577 # We have multiple targets and multiple types
5578 if len(actual_targets) == len(s.unanalyzed_type.items):
5579 types = s.unanalyzed_type.items.copy()
5580 else:
5581 # But it's the wrong number of items
5582 self.fail('Incompatible number of types for "with" targets', s)
5583 else:
5584 # We have multiple targets and one type
5585 self.fail('Multiple types expected for multiple "with" targets', s)
5586
5587 new_types: list[Type] = []
5588 for e, n in zip(s.expr, s.target):
5589 e.accept(self)
5590 if n:
5591 self.analyze_lvalue(n, explicit_type=s.unanalyzed_type is not None)
5592
5593 # Since we have a target, pop the next type from types
5594 if types:
5595 t = types.pop(0)
5596 if self.is_classvar(t):
5597 self.fail_invalid_classvar(n)
5598 allow_tuple_literal = isinstance(n, TupleExpr)
5599 analyzed = self.anal_type(t, allow_tuple_literal=allow_tuple_literal)
5600 if analyzed is not None:
5601 # TODO: Deal with this better
5602 new_types.append(analyzed)
5603 self.store_declared_types(n, analyzed)
5604
5605 s.analyzed_types = new_types
5606
5607 self.visit_block(s.body)
5608
5609 def visit_del_stmt(self, s: DelStmt) -> None:
5610 self.statement = s

Callers

nothing calls this directly

Calls 15

is_func_scopeMethod · 0.95
failMethod · 0.95
analyze_lvalueMethod · 0.95
is_classvarMethod · 0.95
fail_invalid_classvarMethod · 0.95
anal_typeMethod · 0.95
store_declared_typesMethod · 0.95
visit_blockMethod · 0.95
isinstanceFunction · 0.85
lenFunction · 0.85
zipFunction · 0.85
appendMethod · 0.80

Tested by

no test coverage detected