class documentation

class If(mixins.MultiLineBlockMixin, mixins.BlockRangeMixIn, Statement): (source)

View In Hierarchy

Class representing an ast.If node.

>>> import astroid
>>> node = astroid.extract_node('if condition: print(True)')
>>> node
<If l.1 at 0x7f23b2e9dd30>
Method __init__
Method block​_range Get a range from the given line number to where this node ends.
Method get​_children Get the child nodes below this node.
Method has​_elif​_block Undocumented
Method is​_sys​_guard Return True if IF stmt is a sys.version_info guard.
Method is​_typing​_guard Return True if IF stmt is a typing guard.
Method postinit Do some setup after initialisation.
Instance Variable body The contents of the block.
Instance Variable is​_orelse Whether the if-statement is the orelse-block of another if statement.
Instance Variable orelse The contents of the else block.
Instance Variable test The condition that the statement tests.
Property blockstart​_tolineno The line on which the beginning of this block ends.
Method _get​_yield​_nodes​_skip​_lambdas An If node can contain a Yield node in the test
Class Variable _astroid​_fields Node attributes that contain child nodes.
Class Variable _multi​_line​_block​_fields Undocumented

Inherited from MultiLineBlockMixin:

Method _get​_assign​_nodes Undocumented
Method _get​_return​_nodes​_skip​_functions Undocumented
Property _multi​_line​_blocks Undocumented

Inherited from BlockRangeMixIn:

Method _elsed​_block​_range handle block line numbers range for try/finally, for, if and while statements

Inherited from Statement:

Method next​_sibling The next sibling statement node.
Method previous​_sibling The previous sibling statement.
Class Variable is​_statement Whether this node indicates a statement.

Inherited from NodeNG (via Statement):

Method __repr__ Undocumented
Method __str__ Undocumented
Method accept Visit this node using the given visitor.
Method as​_string Get the source code that this node represents.
Method bool​_value Determine the boolean value of this node.
Method callable Whether this node defines something that is callable.
Method child​_sequence Search for the sequence that contains this child.
Method eq Undocumented
Method frame The first parent frame node.
Method has​_base Check if this node inherits from the given type.
Method infer Get a generator of the inferred values.
Method inferred Get a list of the inferred values.
Method instantiate​_class Instantiate an instance of the defined class.
Method last​_child An optimized version of list(get_children())[-1]
Method locate​_child Find the field of this node that contains the given child.
Method node​_ancestors Yield parent, grandparent, etc until there are no more.
Method nodes​_of​_class Get the nodes (including this one or below) of the given types.
Method op​_left​_associative Undocumented
Method op​_precedence Undocumented
Method parent​_of Check if this node is the parent of the given node.
Method repr​_tree Get a string representation of the AST from this node.
Method root Return the root node of the syntax tree.
Method scope The first parent node defining a new scope. These can be Module, FunctionDef, ClassDef, Lambda, or GeneratorExp nodes.
Method set​_local Define that the given name is declared in the given statement node.
Method statement The first parent node, including self, marked as statement node.
Class Variable is​_function Whether this node indicates a function.
Class Variable is​_lambda Undocumented
Class Variable optional​_assign Whether this node optionally assigns a variable.
Instance Variable col​_offset The column that this node appears on in the source code.
Instance Variable end​_col​_offset The end column this node appears on in the source code. Note: This is after the last symbol.
Instance Variable end​_lineno The last line this node appears on in the source code.
Instance Variable lineno The line that this node appears on in the source code.
Instance Variable parent The parent node in the syntax tree.
Property fromlineno The first line that this node appears on in the source code.
Property tolineno The last line that this node appears on in the source code.
Method _fixed​_source​_line Attempt to find the line that this node appears on.
Method _get​_assign​_nodes Undocumented
Method _get​_name​_nodes Undocumented
Method _get​_return​_nodes​_skip​_functions Undocumented
Method _infer we don't know how to resolve a statement by default
Method _infer​_name Undocumented
Method _repr​_name Get a name for nice representation.
Class Variable _explicit​_inference Undocumented
Class Variable _other​_fields Node attributes that do not contain child nodes.
Class Variable _other​_other​_fields Attributes that contain AST-dependent fields.
def __init__(self, lineno=None, col_offset=None, parent=None, *, end_lineno=None, end_col_offset=None): (source)
Parameters
lineno:Optional[int]The line that this node appears on in the source code.
col​_offset:Optional[int]The column that this node appears on in the source code.
parent:Optional[NodeNG]The parent node in the syntax tree.
end​_lineno:Optional[int]The last line this node appears on in the source code.
end​_col​_offset:Optional[int]The end column this node appears on in the source code. Note: This is after the last symbol.
def block_range(self, lineno): (source)
Get a range from the given line number to where this node ends.
Parameters
lineno:intThe line number to start the range at.
Returns
tuple(int, int)The range of line numbers that this node belongs to, starting at the given line number.
def get_children(self): (source)
Get the child nodes below this node.
def has_elif_block(self): (source)

Undocumented

def is_sys_guard(self): (source)

Return True if IF stmt is a sys.version_info guard.

>>> import astroid
>>> node = astroid.extract_node('''
import sys
if sys.version_info > (3, 8):
    from typing import Literal
else:
    from typing_extensions import Literal
''')
>>> node.is_sys_guard()
True
Returns
boolUndocumented
def is_typing_guard(self): (source)

Return True if IF stmt is a typing guard.

>>> import astroid
>>> node = astroid.extract_node('''
from typing import TYPE_CHECKING
if TYPE_CHECKING:
    from xyz import a
''')
>>> node.is_typing_guard()
True
Returns
boolUndocumented
def postinit(self, test=None, body=None, orelse=None): (source)
Do some setup after initialisation.
Parameters
test:Optional[NodeNG]The condition that the statement tests.
body:Optional[typing.List[NodeNG]]The contents of the block.
orelse:Optional[typing.List[NodeNG]]The contents of the else block.
body = (source)
The contents of the block.
is_orelse: bool = (source)
Whether the if-statement is the orelse-block of another if statement.
orelse = (source)
The contents of the else block.
test = (source)
The condition that the statement tests.
@decorators.cachedproperty
blockstart_tolineno = (source)
The line on which the beginning of this block ends.
def _get_yield_nodes_skip_lambdas(self): (source)
An If node can contain a Yield node in the test
_astroid_fields: tuple[str, ...] = (source)

Node attributes that contain child nodes.

This is redefined in most concrete classes.

_multi_line_block_fields: tuple[str, ...] = (source)

Undocumented