1

I have an ast.UnaryOp object, but I manually added a parent attribute (see answer). How do I annotate this in a function?

I currently simply have:

def _get_sim206(node: ast.UnaryOp):
    if isinstance(node.parent, ast.If):
        return False
    return True

But mypy complains (rightfully so) that ast.UnaryOp does not have the parent attribute.

How can I tell mypy that the node is not ast.UnaryOp but ast.UnaryOp + parent attribute?

My Try

I've created my own UnaryOp class which has a parent attribute. I can use this for type-casting:

class UnaryOp(ast.UnaryOp):
    def __init__(self, orig: ast.UnaryOp) -> None:
        self.op = orig.op
        self.operand = orig.operand
        self.lineno = orig.lineno
        self.col_offset = orig.col_offset
        self.parent: ast.Expr = orig.parent  # type: ignore

The downside of it is that I need to type cast in a lot of places and that I introduced Any. I would prefer if I could just state somewhere that all ast.* types in that file do have a parent attribute

4

1 回答 1

0

作为一种解决方法,您可以使用isinstance()带有@runtime_checkable修饰的协议,它将在运行时检查所有协议成员是否已定义并确保静态正确性。

from typing import Protocol, runtime_checkable, cast
import ast


@runtime_checkable
class ParentProto(Protocol):
    parent: ast.AST
    

def foo(node: ast.UnaryOp):
    if isinstance(node, ParentProto):
        # use node.parent
        p = node.parent
        l = node.lineno
# assignment
g = ast.UnaryOp()
cast(ParentProto, g).parent = ast.If()
于 2021-01-04T12:20:04.003 回答