Exemplo n.º 1
0
def infer_property(node, context=None):
    """Understand `property` class

    This only infers the output of `property`
    call, not the arguments themselves.
    """
    if len(node.args) < 1:
        # Invalid property call.
        raise UseInferenceDefault

    getter = node.args[0]
    try:
        inferred = next(getter.infer(context=context))
    except InferenceError as exc:
        raise UseInferenceDefault from exc

    if not isinstance(inferred, (nodes.FunctionDef, nodes.Lambda)):
        raise UseInferenceDefault

    return objects.Property(
        function=inferred,
        name=inferred.name,
        doc=getattr(inferred, "doc", None),
        lineno=node.lineno,
        parent=node,
        col_offset=node.col_offset,
    )
Exemplo n.º 2
0
def infer_property(
        node: nodes.Call,
        context: InferenceContext | None = None) -> objects.Property:
    """Understand `property` class

    This only infers the output of `property`
    call, not the arguments themselves.
    """
    if len(node.args) < 1:
        # Invalid property call.
        raise UseInferenceDefault

    getter = node.args[0]
    try:
        inferred = next(getter.infer(context=context))
    except (InferenceError, StopIteration) as exc:
        raise UseInferenceDefault from exc

    if not isinstance(inferred, (nodes.FunctionDef, nodes.Lambda)):
        raise UseInferenceDefault

    prop_func = objects.Property(
        function=inferred,
        name=inferred.name,
        lineno=node.lineno,
        parent=node,
        col_offset=node.col_offset,
    )
    prop_func.postinit(
        body=[],
        args=inferred.args,
        doc_node=getattr(inferred, "doc_node", None),
    )
    return prop_func