Instagram / Instagram/LibCST

Using inverse of matcher errors for visit decorators

Open
#544 1 comment 0 reactions 0 assignees View on GitHub
bug machinery
Dominant language
Python
Stars
1.9k
Forks
229
PR merge metrics
No merged PRs in 30d

Description

Sorry if this comes across as a newbie question!

I'd like to call a visit function whenever I visit a node that __isn't__ whitespace, like so:

```python
import libcst as cst
import libcst.matchers as m

whitespace = (m.Comment() | m.EmptyLine() | m.Newline()
| m.ParenthesizedWhitespace() | m.SimpleWhitespace()
| m.TrailingWhitespace() | m.BaseParenthesizableWhitespace())

class LoggingVisitor(m.MatcherDecoratableVisitor):
@m.visit(~whitespace)
def visit_not_whitespace(self, node):
print('hello')
```

However, this code errors:

```
File "/Users/sam/anaconda3/lib/python3.8/site-packages/libcst/matchers/_visitors.py", line 680, in __init__
_check_types(
File "/Users/sam/anaconda3/lib/python3.8/site-packages/libcst/matchers/_visitors.py", line 222, in _check_types
possible_match_classes = _get_possible_match_classes(matcher)
File "/Users/sam/anaconda3/lib/python3.8/site-packages/libcst/matchers/_visitors.py", line 77, in _get_possible_match_classes
return [getattr(cst, m.__class__.__name__) for m in matcher.options]
File "/Users/sam/anaconda3/lib/python3.8/site-packages/libcst/matchers/_visitors.py", line 77, in
return [getattr(cst, m.__class__.__name__) for m in matcher.options]
AttributeError: module 'libcst' has no attribute '_InverseOf'
```

Is this intended behavior? Or is there a workaround for this that I'm not seeing?

I suspect this is happening because inverting a matcher creates an `_InverseOf` object, but this object doesn't correspond directly to any concrete nodes from `libcst`.

(I'm using Python 3.8 and libcst v0.3.18.)

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.