Instagram / Fixit

Advanced Python linting framework with auto-fixes and hierarchical configuration that makes it easy to write custom in-repo lint rules.
https://fixit.rtfd.io/en/latest/
Other
666 stars 62 forks source link

Parser error and crash when linting a file #377

Closed Pierre-Sassoulas closed 10 months ago

Pierre-Sassoulas commented 1 year ago
pylint/checkers/base/docstring_checker.py: EXCEPTION: Syntax Error @ 1:1.

parser error: error at 1:5: expected one of *, +, -, ..., AWAIT, False, NUMBER, None, True, lambda, not, ~

class

^

Traceback (most recent call last):

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/fixit/api.py", line 91, in fixit_bytes

    for violation in runner.collect_violations(rules, config):

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/fixit/engine.py", line 76, in collect_violations

    wrapper.visit_batched(rules)

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/metadata/wrapper.py", line 221, in visit_batched

    return visit_batched(self.module, visitors, before_visit, after_leave)

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_batched_visitor.py", line 86, in visit_batched

    return cast(CSTNodeT, node.visit(batched_visitor))

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_nodes/module.py", line 90, in visit

    result = super(Module, self).visit(visitor)

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_nodes/base.py", line 227, in visit

    _CSTNodeSelfT, self._visit_and_replace_children(visitor)

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_nodes/module.py", line 74, in _visit_and_replace_children

    body=visit_body_sequence(self, "body", self.body, visitor),

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_nodes/internal.py", line 227, in visit_body_sequence

    return tuple(visit_body_iterable(parent, fieldname, children, visitor))

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_nodes/internal.py", line 193, in visit_body_iterable

    new_child = child.visit(visitor)

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_nodes/base.py", line 227, in visit

    _CSTNodeSelfT, self._visit_and_replace_children(visitor)

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_nodes/statement.py", line 1931, in _visit_and_replace_children

    body=visit_required(self, "body", self.body, visitor),

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_nodes/internal.py", line 81, in visit_required

    result = node.visit(visitor)

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_nodes/base.py", line 227, in visit

    _CSTNodeSelfT, self._visit_and_replace_children(visitor)

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_nodes/statement.py", line 697, in _visit_and_replace_children

    body=visit_body_sequence(self, "body", self.body, visitor),

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_nodes/internal.py", line 227, in visit_body_sequence

    return tuple(visit_body_iterable(parent, fieldname, children, visitor))

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_nodes/internal.py", line 193, in visit_body_iterable

    new_child = child.visit(visitor)

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_nodes/base.py", line 227, in visit

    _CSTNodeSelfT, self._visit_and_replace_children(visitor)

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_nodes/statement.py", line 1783, in _visit_and_replace_children

    params=visit_required(self, "params", self.params, visitor),

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_nodes/internal.py", line 81, in visit_required

    result = node.visit(visitor)

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_nodes/base.py", line 227, in visit

    _CSTNodeSelfT, self._visit_and_replace_children(visitor)

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_nodes/expression.py", line 1978, in _visit_and_replace_children

    params=visit_sequence(self, "params", self.params, visitor),

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_nodes/internal.py", line 177, in visit_sequence

    return tuple(visit_iterable(parent, fieldname, children, visitor))

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_nodes/internal.py", line 159, in visit_iterable

    new_child = child.visit(visitor)

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_nodes/base.py", line 227, in visit

    _CSTNodeSelfT, self._visit_and_replace_children(visitor)

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_nodes/expression.py", line 1816, in _visit_and_replace_children

    annotation=visit_optional(self, "annotation", self.annotation, visitor),

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_nodes/internal.py", line 110, in visit_optional

    result = node.visit(visitor)

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_nodes/base.py", line 227, in visit

    _CSTNodeSelfT, self._visit_and_replace_children(visitor)

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_nodes/expression.py", line 1673, in _visit_and_replace_children

    annotation=visit_required(self, "annotation", self.annotation, visitor),

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_nodes/internal.py", line 81, in visit_required

    result = node.visit(visitor)

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_nodes/base.py", line 227, in visit

    _CSTNodeSelfT, self._visit_and_replace_children(visitor)

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_nodes/expression.py", line 1604, in _visit_and_replace_children

    slice=visit_sequence(self, "slice", self.slice, visitor),

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_nodes/internal.py", line 177, in visit_sequence

    return tuple(visit_iterable(parent, fieldname, children, visitor))

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_nodes/internal.py", line 159, in visit_iterable

    new_child = child.visit(visitor)

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_nodes/base.py", line 227, in visit

    _CSTNodeSelfT, self._visit_and_replace_children(visitor)

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_nodes/expression.py", line 1549, in _visit_and_replace_children

    slice=visit_required(self, "slice", self.slice, visitor),

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_nodes/internal.py", line 81, in visit_required

    result = node.visit(visitor)

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_nodes/base.py", line 227, in visit

    _CSTNodeSelfT, self._visit_and_replace_children(visitor)

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_nodes/expression.py", line 1463, in _visit_and_replace_children

    value=visit_required(self, "value", self.value, visitor),

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_nodes/internal.py", line 81, in visit_required

    result = node.visit(visitor)

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_nodes/base.py", line 218, in visit

    should_visit_children = visitor.on_visit(self)

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_batched_visitor.py", line 134, in on_visit

    v(node)

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/fixit/rule.py", line 100, in wrapper

    return func(node)

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/fixit/rules/no_string_type_annotation.py", line 303, in visit_SimpleString

    replacement=cst.parse_expression(node.evaluated_value),

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_parser/entrypoints.py", line 160, in parse_expression

    result = _parse(

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/libcst/_parser/entrypoints.py", line 55, in _parse

    return parse(source_str)

libcst._exceptions.ParserSyntaxError: Syntax Error @ 1:1.

parser error: error at 1:5: expected one of *, +, -, ..., AWAIT, False, NUMBER, None, True, lambda, not, ~

class

^

See https://github.com/pylint-dev/pylint/blob/fc97635b9515b650be2ce0e75a36e47a45094ccf/pylint/checkers/base/docstring_checker.py#L1 for the source file generating the error, I don't see any ~ inside it though (and line 1 col 5 is not particularly out of the ordinary).

thatch commented 1 year ago

The relevant part of that traceback is

  File "/home/pierre/pylint/venv/lib/python3.10/site-packages/fixit/rules/no_string_type_annotation.py", line 303, in visit_SimpleString

    replacement=cst.parse_expression(node.evaluated_value),

I'm guessing (based on which types have the string "class in them) that this is from https://github.com/pylint-dev/pylint/blob/fc97635b9515b650be2ce0e75a36e47a45094ccf/pylint/checkers/base/docstring_checker.py#L154

Don't know if this is a bug in no_string_type_annotation or not, but HTH

Pierre-Sassoulas commented 1 year ago

Fixit suggest to remove the quote around strings in typing.Litteral in another check (this is definitely a bug as the litteral string is not defined). Maybe a wrong assumption is being made here too ?

zsol commented 1 year ago

Yeah looks like no_string_type_annotation guards against typing_extension.Literal but not typing.Literal: https://github.com/Instagram/Fixit/blob/79bed4cfda699ca2d2e8283f139860997ef5cd66/src/fixit/rules/no_string_type_annotation.py#L276

But also the rule should probably not crash when parse_expression raises an exception