With#

class astroid.nodes.With(lineno: int | None = None, col_offset: int | None = None, parent: NodeNG | None = None, *, end_lineno: int | None = None, end_col_offset: int | None = None)[source]#

Bases: MultiLineWithElseBlockNode, AssignTypeNode, Statement

Class representing an ast.With node.

>>> import astroid
>>> node = astroid.extract_node('''
with open(file_path) as file_:
    print(file_.read())
''')
>>> node
<With l.2 at 0x7f23b2e4e710>
assigned_stmts(**kwargs: _P.kwargs) Generator[InferenceResult, None, None]#

Returns the assigned statement (non inferred) according to the assignment type. See astroid/protocols.py for actual implementation.

property blockstart_tolineno#

The line on which the beginning of this block ends.

Type:

int

body: list[NodeNG]#

The contents of the with block.

get_children()[source]#

Get the child nodes below this node.

Returns:

The children.

Return type:

iterable(NodeNG)

items: list[tuple[NodeNG, NodeNG | None]]#

The pairs of context managers and the names they are assigned to.

postinit(items: list[tuple[NodeNG, NodeNG | None]] | None = None, body: list[NodeNG] | None = None, type_annotation: NodeNG | None = None) None[source]#

Do some setup after initialisation.

Parameters:
  • items – The pairs of context managers and the names they are assigned to.

  • body – The contents of the with block.

type_annotation: NodeNG | None#

If present, this will contain the type annotation passed by a type comment