Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
isVoid opened this issue Mar 4, 2024 · 0 comments
Open

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

isVoid opened this issue Mar 4, 2024 · 0 comments

Comments

@isVoid
Copy link

isVoid commented Mar 4, 2024

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:

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 <module>
    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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant