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
669 stars 63 forks source link

bug: using leave/visit_Module with report() results in libcst metadata key error #367

Closed duzumaki closed 1 year ago

duzumaki commented 1 year ago

Doing this

class RuleName(LintRule):
    VALID = [Valid(example)]
    INVALID = [Invalid(example)]

    def leave_Module(self, node: cst.Module) -> None:
        self.report(node)

same with

    def visit_Module(self, node: cst.Module) -> None:
        self.report(node)

or you can do (which is what report() ends up calling)

class RuleName(LintRule):
    VALID = [Valid(example)]
    INVALID = [Invalid(example)]

    METADATA_DEPENDENCIES = (ParentNodeProvider,)

    def leave_Module(self, node: cst.Module) -> None:
        problem_is_get_metadata = self.get_metadata(ParentNodeProvider, node)

results in a metadata libcst error this:

python3.10/site-packages/fixit/rule.py:151: in node_comments
    parent = self.get_metadata(ParentNodeProvider, node)
python3.10/site-packages/libcst/_metadata_dependent.py:136: in get_metadata
    value = self.metadata[key][node]
E   KeyError: Module(
E       body=[
E           SimpleStatem
-the rest of the error is just the rest of the module tree-

I've tried it with other nodes and module seems to be the only one causing this

libcst version: 1.0.1
fixit version: 2.0.0.post1
python: 3.10.6
ahter commented 1 year ago

@duzumaki are you able to submit a PR that fixes this?

duzumaki commented 1 year ago

My guess is the fix is to not run a ParentNodeProvider on a Module node(which is what self.report() ends up doing if you follow the calls) because what's the parent of a Module node, nothing I assume.

and making sure to still yield the module's header comments

I'll get a pr up and see what the fixit team thinks about it

ahter commented 1 year ago

Just linking the PR to the issue here. https://github.com/Instagram/Fixit/pull/368