Instagram / Instagram/LibCST

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

Open
#1,009 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
1.9k
Forks
229
PR merge metrics
No merged PRs in 30d

Description

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

```python
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")
```
```pycon
>>> 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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.