How to use the libcst._nodes.op.Comma function in libcst

To help you get started, we’ve selected a few libcst examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Instagram / LibCST / libcst / _parser / conversions / expression.py View on Github external
value = next(children_iter)
        element = DictElement(
            key.value,
            value.value,
            whitespace_before_colon=parse_parenthesizable_whitespace(
                config, colon_tok.whitespace_before
            ),
            whitespace_after_colon=parse_parenthesizable_whitespace(
                config, colon_tok.whitespace_after
            ),
        )
    # Handle the trailing comma (if there is one)
    try:
        comma_token = next(children_iter)
        element = element.with_changes(
            comma=Comma(
                whitespace_before=parse_parenthesizable_whitespace(
                    config, comma_token.whitespace_before
                ),
                # Only compute whitespace_after if we're not a trailing comma.
                # If we're a trailing comma, that whitespace should be consumed by the
                # RightBracket.
                whitespace_after=(
                    parse_parenthesizable_whitespace(
                        config, comma_token.whitespace_after
                    )
                    if comma_token is not last_child
                    else SimpleWhitespace("")
                ),
            )
        )
    except StopIteration:
github Instagram / LibCST / libcst / _parser / conversions / statement.py View on Github external
def _construct_nameitems(config: ParserConfig, names: Sequence[Any]) -> List[NameItem]:
    nameitems: List[NameItem] = []
    for name, maybe_comma in grouper(names, 2):
        if maybe_comma is None:
            nameitems.append(NameItem(Name(name.string)))
        else:
            nameitems.append(
                NameItem(
                    Name(name.string),
                    comma=Comma(
                        whitespace_before=parse_simple_whitespace(
                            config, maybe_comma.whitespace_before
                        ),
                        whitespace_after=parse_simple_whitespace(
                            config, maybe_comma.whitespace_after
                        ),
                    ),
                )
            )
    return nameitems
github Instagram / LibCST / libcst / _parser / conversions / expression.py View on Github external
single_child_is_sequence: bool,
    sequence_type: typing.Union[
        typing.Type[Tuple], typing.Type[List], typing.Type[Set]
    ],
) -> typing.Any:
    if not single_child_is_sequence and len(children) == 1:
        return children[0]
    # N.B. The parent node (e.g. atom) is responsible for computing and attaching
    # whitespace information on any parenthesis, square brackets, or curly braces
    elements = []
    for wrapped_expr_or_starred_element, comma_token in grouper(children, 2):
        expr_or_starred_element = wrapped_expr_or_starred_element.value
        if comma_token is None:
            comma = MaybeSentinel.DEFAULT
        else:
            comma = Comma(
                whitespace_before=parse_parenthesizable_whitespace(
                    config, comma_token.whitespace_before
                ),
                # Only compute whitespace_after if we're not a trailing comma.
                # If we're a trailing comma, that whitespace should be consumed by the
                # TrailingWhitespace, parenthesis, etc.
                whitespace_after=(
                    parse_parenthesizable_whitespace(
                        config, comma_token.whitespace_after
                    )
                    if comma_token is not children[-1]
                    else SimpleWhitespace("")
                ),
            )

        if isinstance(expr_or_starred_element, StarredElement):
github Instagram / LibCST / libcst / _nodes / expression.py View on Github external
@add_slots
@dataclass(frozen=True)
class ParamStar(CSTNode):
    """
    A sentinel indicator on a :class:`Parameter` list to denote that the subsequent
    params are keyword-only args.

    This syntax is described in `PEP 3102`_.

    .. _PEP 3102: https://www.python.org/dev/peps/pep-3102/#specification
    """

    # Comma that comes after the star.
    comma: Comma = Comma.field(whitespace_after=SimpleWhitespace(" "))

    def _visit_and_replace_children(self, visitor: CSTVisitorT) -> "ParamStar":
        return ParamStar(comma=visit_required(self, "comma", self.comma, visitor))

    def _codegen_impl(self, state: CodegenState) -> None:
        state.add_token("*")
        self.comma._codegen(state)


@add_slots
@dataclass(frozen=True)
class Param(CSTNode):
    """
    A positional or keyword argument in a :class:`Parameter` list. May contain an
    :class:`Annotation` and, in some cases, a ``default``.
    """
github Instagram / LibCST / libcst / _parser / conversions / statement.py View on Github external
(assert_token, test) = children
        assert_node = Assert(
            whitespace_after_assert=parse_simple_whitespace(
                config, test.whitespace_before
            ),
            test=test.value,
            msg=None,
        )
    else:
        (assert_token, test, comma_token, msg) = children
        assert_node = Assert(
            whitespace_after_assert=parse_simple_whitespace(
                config, test.whitespace_before
            ),
            test=test.value,
            comma=Comma(
                whitespace_before=parse_simple_whitespace(
                    config, comma_token.whitespace_before
                ),
                whitespace_after=parse_simple_whitespace(config, msg.whitespace_before),
            ),
            msg=msg.value,
        )

    return WithLeadingWhitespace(assert_node, assert_token.whitespace_before)
github Instagram / LibCST / libcst / _parser / conversions / params.py View on Github external
if isinstance(parameter, ParamStarPartial):
                # Example:
                #     def fn(abc, *): ...
                #
                # There's also the case where we have bare * with a trailing comma.
                # That's handled later.
                #
                # It's not valid to construct a ParamStar object without a comma, so we
                # need to catch the non-comma case separately.
                raise PartialParserSyntaxError(
                    "Named (keyword) arguments must follow a bare *."
                )
            else:
                current = add_param(current, parameter)
        else:
            comma = Comma(
                whitespace_before=parse_parenthesizable_whitespace(
                    config, comma.whitespace_before
                ),
                whitespace_after=parse_parenthesizable_whitespace(
                    config, comma.whitespace_after
                ),
            )
            if isinstance(parameter, ParamStarPartial):
                current = add_param(current, ParamStar(comma=comma))
            else:
                current = add_param(current, parameter.with_changes(comma=comma))

    if isinstance(star_arg, ParamStar) and len(kwonly_params) == 0:
        # Example:
        #     def fn(abc, *,): ...
        #
github Instagram / LibCST / libcst / _parser / conversions / expression.py View on Github external
def convert_subscriptlist(
    config: ParserConfig, children: typing.Sequence[typing.Any]
) -> typing.Any:
    # This is a list of SubscriptElement, so construct as such by grouping every
    # subscript with an optional comma and adding to a list.
    elements = []
    for slice, comma in grouper(children, 2):
        if comma is None:
            elements.append(SubscriptElement(slice=slice.value))
        else:
            elements.append(
                SubscriptElement(
                    slice=slice.value,
                    comma=Comma(
                        whitespace_before=parse_parenthesizable_whitespace(
                            config, comma.whitespace_before
                        ),
                        whitespace_after=parse_parenthesizable_whitespace(
                            config, comma.whitespace_after
                        ),
                    ),
                )
            )
    return WithLeadingWhitespace(elements, children[0].whitespace_before)
github Instagram / LibCST / libcst / matchers / _return_types.py View on Github external
AugAssign: Union[BaseSmallStatement, RemovalSentinel],
    Await: BaseExpression,
    BinaryOperation: BaseExpression,
    BitAnd: BaseBinaryOp,
    BitAndAssign: BaseAugOp,
    BitInvert: BaseUnaryOp,
    BitOr: BaseBinaryOp,
    BitOrAssign: BaseAugOp,
    BitXor: BaseBinaryOp,
    BitXorAssign: BaseAugOp,
    BooleanOperation: BaseExpression,
    Break: Union[BaseSmallStatement, RemovalSentinel],
    Call: BaseExpression,
    ClassDef: Union[BaseStatement, RemovalSentinel],
    Colon: Union[Colon, MaybeSentinel],
    Comma: Union[Comma, MaybeSentinel],
    Comment: Comment,
    CompFor: CompFor,
    CompIf: CompIf,
    Comparison: BaseExpression,
    ComparisonTarget: Union[ComparisonTarget, RemovalSentinel],
    ConcatenatedString: BaseExpression,
    Continue: Union[BaseSmallStatement, RemovalSentinel],
    Decorator: Union[Decorator, RemovalSentinel],
    Del: Union[BaseSmallStatement, RemovalSentinel],
    Dict: BaseExpression,
    DictComp: BaseExpression,
    DictElement: Union[BaseDictElement, RemovalSentinel],
    Divide: BaseBinaryOp,
    DivideAssign: BaseAugOp,
    Dot: Union[Dot, RemovalSentinel],
    Element: Union[BaseElement, RemovalSentinel],