Exemple #1
0
 def _handle_Index(  # noqa
     self, slice: cst.Index, node: cst.BaseExpression
 ) -> cst.BaseExpression:
     value = slice.value
     if isinstance(value, cst.Subscript):
         new_slice = slice.with_changes(value=self._handle_Subscript(value))
         return node.with_changes(slice=new_slice)
     elif isinstance(value, cst.Attribute):
         new_slice = slice.with_changes(value=self._add_annotation_to_imports(value))
         return node.with_changes(slice=new_slice)
     else:
         return node
Exemple #2
0
def _remove_do_not_care(oldtype: cst.BaseExpression) -> cst.BaseExpression:
    """
    Given a BaseExpression from a type, return a new BaseExpression that does not
    refer to a DoNotCareSentinel.
    """
    return ensure_type(oldtype.visit(RemoveDoNotCareFromGeneric()),
                       cst.BaseExpression)
Exemple #3
0
def _remove_types(oldtype: cst.BaseExpression,
                  values: Sequence[str]) -> cst.BaseExpression:
    """
    Given a BaseExpression from a type, return a new BaseExpression that does not
    refer to any types listed in values.
    """
    return ensure_type(oldtype.visit(RemoveTypesFromGeneric(values)),
                       cst.BaseExpression)
Exemple #4
0
def _convert_match_nodes_to_cst_nodes(
    matchtype: cst.BaseExpression, ) -> cst.BaseExpression:
    """
    Given a BaseExpression in a type, convert this to a new BaseExpression that refers
    to LibCST nodes instead of forward references to matcher nodes.
    """
    return ensure_type(matchtype.visit(MatcherClassToLibCSTClass()),
                       cst.BaseExpression)
def _add_match_if_true(
    oldtype: cst.BaseExpression, concrete_only_expr: cst.BaseExpression
) -> cst.BaseExpression:
    """
    Given a BaseExpression in a type, add MatchIfTrue to it. This either
    wraps in a Union type, or adds to the end of an existing Union type.
    """
    if isinstance(oldtype, cst.Subscript) and oldtype.value.deep_equals(
        cst.Name("Union")
    ):
        # Add to the end of the value
        return oldtype.with_changes(
            slice=[*oldtype.slice, _get_match_if_true(concrete_only_expr)]
        )

    # Just wrap in a union type
    return _get_wrapped_union_type(oldtype, _get_match_if_true(concrete_only_expr))