wemake-python-styleguide icon indicating copy to clipboard operation
wemake-python-styleguide copied to clipboard

Consider using libcst

Open sobolevn opened this issue 5 years ago • 2 comments

We can try it: https://github.com/Instagram/LibCST

This might solve a lot of our problems. Like:

  1. New lines in brackets
  2. Brackets around operators (like #385 and #214 and #454)

sobolevn avatar Dec 10 '19 19:12 sobolevn

Example:

>>> a = cst.parse_module("""
... print(
...     1,
...     2,
... )
... """
... )
>>> a
Module(
    body=[
        SimpleStatementLine(
            body=[
                Expr(
                    value=Call(
                        func=Name(
                            value='print',
                            lpar=[],
                            rpar=[],
                        ),
                        args=[
                            Arg(
                                value=Integer(
                                    value='1',
                                    lpar=[],
                                    rpar=[],
                                ),
                                keyword=None,
                                equal=MaybeSentinel.DEFAULT,
                                comma=Comma(
                                    whitespace_before=SimpleWhitespace(
                                        value='',
                                    ),
                                    whitespace_after=ParenthesizedWhitespace(
                                        first_line=TrailingWhitespace(
                                            whitespace=SimpleWhitespace(
                                                value='',
                                            ),
                                            comment=None,
                                            newline=Newline(
                                                value=None,
                                            ),
                                        ),
                                        empty_lines=[],
                                        indent=True,
                                        last_line=SimpleWhitespace(
                                            value='    ',
                                        ),
                                    ),
                                ),
                                star='',
                                whitespace_after_star=SimpleWhitespace(
                                    value='',
                                ),
                                whitespace_after_arg=SimpleWhitespace(
                                    value='',
                                ),
                            ),
                            Arg(
                                value=Integer(
                                    value='2',
                                    lpar=[],
                                    rpar=[],
                                ),
                                keyword=None,
                                equal=MaybeSentinel.DEFAULT,
                                comma=Comma(
                                    whitespace_before=SimpleWhitespace(
                                        value='',
                                    ),
                                    whitespace_after=ParenthesizedWhitespace(
                                        first_line=TrailingWhitespace(
                                            whitespace=SimpleWhitespace(
                                                value='',
                                            ),
                                            comment=None,
                                            newline=Newline(
                                                value=None,
                                            ),
                                        ),
                                        empty_lines=[],
                                        indent=True,
                                        last_line=SimpleWhitespace(
                                            value='',
                                        ),
                                    ),
                                ),
                                star='',
                                whitespace_after_star=SimpleWhitespace(
                                    value='',
                                ),
                                whitespace_after_arg=SimpleWhitespace(
                                    value='',
                                ),
                            ),
                        ],
                        lpar=[],
                        rpar=[],
                        whitespace_after_func=SimpleWhitespace(
                            value='',
                        ),
                        whitespace_before_args=ParenthesizedWhitespace(
                            first_line=TrailingWhitespace(
                                whitespace=SimpleWhitespace(
                                    value='',
                                ),
                                comment=None,
                                newline=Newline(
                                    value=None,
                                ),
                            ),
                            empty_lines=[],
                            indent=True,
                            last_line=SimpleWhitespace(
                                value='    ',
                            ),
                        ),
                    ),
                    semicolon=MaybeSentinel.DEFAULT,
                ),
            ],
            leading_lines=[],
            trailing_whitespace=TrailingWhitespace(
                whitespace=SimpleWhitespace(
                    value='',
                ),
                comment=None,
                newline=Newline(
                    value=None,
                ),
            ),
        ),
    ],
    header=[
        EmptyLine(
            indent=True,
            whitespace=SimpleWhitespace(
                value='',
            ),
            comment=None,
            newline=Newline(
                value=None,
            ),
        ),
    ],
    footer=[],
    encoding='utf-8',
    default_indent='    ',
    default_newline='\n',
    has_trailing_newline=True,
)

sobolevn avatar Dec 11 '19 10:12 sobolevn

Implementation: https://github.com/wemake-services/wemake-python-styleguide/pull/1147

sobolevn avatar Mar 25 '20 08:03 sobolevn