Esempio n. 1
0
def has_conditional_instantiation(node: astroid.ClassDef, context=None):
    if 'pyomo' not in node.qname():
        return
    try:
        # check if the class defines a __new__()
        dunder_new_node: astroid.FunctionDef = node.local_attr('__new__')[0]
    except astroid.AttributeInferenceError:
        return False
    else:
        # _display(node)
        # find all return statements; if there's more than one, assume that instances are created conditionally,
        # and therefore the type of the instantiated object cannot be known with static analysis
        # to be more accurate, we should check for If nodes as well as maybe the presence of other __new__() calls
        return_statements = list(
            dunder_new_node.nodes_of_class(astroid.node_classes.Return))
        return len(return_statements) > 1
Esempio n. 2
0
def is_class_subscriptable_pep585_with_postponed_evaluation_enabled(
        value: astroid.ClassDef, node: astroid.node_classes.NodeNG) -> bool:
    """Check if class is subscriptable with PEP 585 and
    postponed evaluation enabled.
    """
    if not is_postponed_evaluation_enabled(node):
        return False

    parent_node = node.parent
    while True:
        # Check if any parent node matches condition
        if isinstance(
                parent_node,
            (astroid.AnnAssign, astroid.Arguments, astroid.FunctionDef)):
            break
        parent_node = parent_node.parent
        if isinstance(parent_node, astroid.Module):
            return False
    if value.qname() in SUBSCRIPTABLE_CLASSES_PEP585:
        return True
    return False