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

[BUG] OneOf attribute example is not working #345

Closed
sk- opened this issue Jul 19, 2020 · 1 comment · Fixed by #529
Closed

[BUG] OneOf attribute example is not working #345

sk- opened this issue Jul 19, 2020 · 1 comment · Fixed by #529

Comments

@sk-
Copy link
Contributor

sk- commented Jul 19, 2020

In the OneOf docs it says:

Note that a OneOf matcher can be used anywhere you are defining a matcher attribute. So, an alternate form to the first example looks like:

m.Name(m.OneOf("True", "False"))

However, I'm not able to use that matcher, as I get the following error:

Traceback (most recent call last):
  File "cst/matcher_bug2.py", line 8, in <module>
    print(m.findall(node, matcher_oneof))
  File "/Users/user/.virtualenvs/cst/lib/python3.6/site-packages/libcst/matchers/_matcher_base.py", line 1574, in findall
    nodes, _ = _find_or_extract_all(tree, matcher, metadata_resolver=metadata_resolver)
  File "/Users/user/.virtualenvs/cst/lib/python3.6/site-packages/libcst/matchers/_matcher_base.py", line 1539, in _find_or_extract_all
    tree.visit(finder)
  File "/Users/user/.virtualenvs/cst/lib/python3.6/site-packages/libcst/_nodes/base.py", line 225, in visit
    _CSTNodeSelfT, self._visit_and_replace_children(visitor)
  File "/Users/user/.virtualenvs/cst/lib/python3.6/site-packages/libcst/_nodes/statement.py", line 591, in _visit_and_replace_children
    test=visit_required(self, "test", self.test, visitor),
  File "/Users/user/.virtualenvs/cst/lib/python3.6/site-packages/libcst/_nodes/internal.py", line 81, in visit_required
    result = node.visit(visitor)
  File "/Users/user/.virtualenvs/cst/lib/python3.6/site-packages/libcst/_nodes/base.py", line 216, in visit
    should_visit_children = visitor.on_visit(self)
  File "/Users/user/.virtualenvs/cst/lib/python3.6/site-packages/libcst/matchers/_matcher_base.py", line 1483, in on_visit
    match = _matches(node, self.matcher, self.metadata_lookup)
  File "/Users/user/.virtualenvs/cst/lib/python3.6/site-packages/libcst/matchers/_matcher_base.py", line 1352, in _matches
    return _node_matches(node, matcher, metadata_lookup)
  File "/Users/user/.virtualenvs/cst/lib/python3.6/site-packages/libcst/matchers/_matcher_base.py", line 1299, in _node_matches
    attribute_capture = _attribute_matches(actual, desired, metadata_lookup)
  File "/Users/user/.virtualenvs/cst/lib/python3.6/site-packages/libcst/matchers/_matcher_base.py", line 1113, in _attribute_matches
    sequence_capture = _sequence_matches(node, m, metadata_lookup)
  File "/Users/user/.virtualenvs/cst/lib/python3.6/site-packages/libcst/matchers/_matcher_base.py", line 1044, in _sequence_matches
    match_capture = _matches(node, matcher, metadata_lookup)
  File "/Users/user/.virtualenvs/cst/lib/python3.6/site-packages/libcst/matchers/_matcher_base.py", line 1352, in _matches
    return _node_matches(node, matcher, metadata_lookup)
  File "/Users/user/.virtualenvs/cst/lib/python3.6/site-packages/libcst/matchers/_matcher_base.py", line 1282, in _node_matches
    for field in fields(matcher):
  File "/Users/user/.virtualenvs/cst/lib/python3.6/site-packages/dataclasses.py", line 972, in fields
    raise TypeError('must be called with a dataclass type or instance')
TypeError: must be called with a dataclass type or instance

This is the code I'm using to test it:

import libcst.matchers as m
import libcst

node = libcst.parse_statement("if True: continue")
matcher_oneof = m.OneOf(m.Name("True"), m.Name("False"))
matcher_oneof_attribute = m.Name(m.OneOf('True', 'False'))
matcher_matchiftrue = m.Name(m.MatchIfTrue(lambda value: value in ('True', 'False')))
print(m.findall(node, matcher_matchiftrue))
print(m.findall(node, matcher_oneof))
print(m.findall(node, matcher_oneof_attribute))

Note that the matcher_matchiftrue and the matcher_oneof matchers work fine in the example above.

@jimmylai
Copy link
Contributor

The documentation is wrong. The options of OneOf need to be matchers (either concrete matcher node or some matcher helper node like OneOf/AllOf) instead of arbitrary type (like the str in the example).
We should fix the document.

Zac-HD added a commit to Zac-HD/LibCST that referenced this issue Oct 10, 2021
stroxler pushed a commit that referenced this issue Oct 21, 2021
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

Successfully merging a pull request may close this issue.

2 participants