Exemplo n.º 1
0
def _get_field_default(field_call: Call) -> Tuple[str, Optional[NodeNG]]:
    """Return a the default value of a field call, and the corresponding keyword argument name.

    field(default=...) results in the ... node
    field(default_factory=...) results in a Call node with func ... and no arguments

    If neither or both arguments are present, return ("", None) instead,
    indicating that there is not a valid default value.
    """
    default, default_factory = None, None
    for keyword in field_call.keywords:
        if keyword.arg == "default":
            default = keyword.value
        elif keyword.arg == "default_factory":
            default_factory = keyword.value

    if default is not None and default_factory is None:
        return "default", default

    if default is None and default_factory is not None:
        new_call = Call(
            lineno=field_call.lineno,
            col_offset=field_call.col_offset,
            parent=field_call.parent,
        )
        new_call.postinit(func=default_factory)
        return "default_factory", new_call

    return "", None
Exemplo n.º 2
0
def _looks_like_dataclass_field_call(node: Call,
                                     check_scope: bool = True) -> bool:
    """Return True if node is calling dataclasses field or Field
    from an AnnAssign statement directly in the body of a ClassDef.

    If check_scope is False, skips checking the statement and body.
    """
    if check_scope:
        stmt = node.statement(future=True)
        scope = stmt.scope()
        if not (isinstance(stmt, AnnAssign) and stmt.value is not None
                and isinstance(scope, ClassDef)
                and is_decorated_with_dataclass(scope)):
            return False

    try:
        inferred = next(node.func.infer())
    except (InferenceError, StopIteration):
        return False

    if not isinstance(inferred, FunctionDef):
        return False

    return inferred.name == FIELD_NAME and inferred.root(
    ).name in DATACLASS_MODULES
Exemplo n.º 3
0
 def transform_call(node: Call) -> Const:
     # Let's do some function inlining
     inferred = next(node.infer())
     return inferred
Exemplo n.º 4
0
 def transform_call(node: Call) -> Const:
     inferred = next(node.infer())
     return inferred