Instagram / Instagram/LibCST

AttributeError: module 'libcst' has no attribute '_InverseOf'

Open
#1,115 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

I'm attempting to visit the name nodes inside the import nodes using `MatcherDecoratableTransformer`. For each of node I'm visiting, I'd like to check against a list of items to skip the visit. I'm trying to use the following code to achieve this:

```python
source = "import a, b, c, d"

import libcst as cst
import libcst.matchers as m

class InvertTransformer(m.MatcherDecoratableTransformer):
@m.call_if_inside(m.Import(names=[m.ZeroOrMore(m.ImportAlias(name=m.Name()))]))
@m.leave(~m.Name("c"))
def update(self, original: cst.Name, updated: cst.Name) -> cst.Name:
print(f"{updated.value=}")
return updated

trans = InvertTransformer()
tree = cst.parse_module(source)
converted = tree.visit(trans)
```

An exception is thrown:
```
Traceback (most recent call last):
File "do_not_match.py", line 15, in
trans = InvertTransformer()
^^^^^^^^^^^^^^^^^^^
File "libcst/matchers/_visitors.py", line 482, in __init__
_check_types(
File "libcst/matchers/_visitors.py", line 227, in _check_types
possible_match_classes = _get_possible_match_classes(matcher)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "libcst/matchers/_visitors.py", line 87, in _get_possible_match_classes
return [getattr(cst, matcher.__class__.__name__)]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'libcst' has no attribute '_InverseOf'
```

Comparing to using `if m.matches(m.Name("b")): return updated` inside the function body, which is the more preferred way to achieve the goal?

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.