LibCST icon indicating copy to clipboard operation
LibCST copied to clipboard

Many `visit_<Node>_<Attr>` methods don't seem to be called automatically; how do they get called?

Open bzoracler opened this issue 1 year ago • 0 comments

How do I get `value` visited to display below?

import libcst as cst

class A(cst.CSTVisitor):
    def visit_Name_value(self, node: cst.Name) -> None:
        print("`value` visited")

    def visit_Name_lpar(self, node: cst.Name) -> None:
        print("`lpar` visited")
>>> cst.parse_module("asdf = 3").visit(A())
`lpar` visited

I understand why visit_Name_value is skipped:

https://github.com/Instagram/LibCST/blob/9c263aa8977962a870ce2770d2aa18ee0dacb344/libcst/_nodes/expression.py#L345-L350

visit_sequence will make sure that CSTVisitor.on_visit_attribute (and thus visit_Name_lpar) is called, whereas no such equivalent is present for the attribute Name.value. Is there a way to make a visitor automatically call these attribute visit methods using the public API, or do we need to transform the tree with subclassed nodes which override _visit_and_replace_children? I didn't see anything here:

https://libcst.readthedocs.io/en/latest/visitors.html#visit-and-leave-helper-functions

bzoracler avatar Sep 03 '23 02:09 bzoracler