Instagram / Fixit

Advanced Python linting framework with auto-fixes and hierarchical configuration that makes it easy to write custom in-repo lint rules.

Home Page:https://fixit.rtfd.io/en/latest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

duzumaki opened this issue · comments

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

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

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

Just linking the PR to the issue here.
#368