Skip to content

Commit

Permalink
Fix bug when TypeOf is one of options in OneOf / AllOf (#756)
Browse files Browse the repository at this point in the history
* Fix a bug when one of the option of `OneOf` is a `TypeOf`

* Disallow `TypeOf` in `AllOf`, analogous to how `OneOf` is disallowed in `AllOf`
  • Loading branch information
MapleCCC authored Aug 26, 2022
1 parent 5fc69d6 commit fc622ce
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
4 changes: 3 additions & 1 deletion libcst/matchers/_matcher_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def __init__(self, *options: Union[_MatcherT, "OneOf[_MatcherT]"]) -> None:
for option in options:
if isinstance(option, AllOf):
raise Exception("Cannot use AllOf and OneOf in combination!")
elif isinstance(option, OneOf):
elif isinstance(option, (OneOf, TypeOf)):
actual_options.extend(option.options)
else:
actual_options.append(option)
Expand Down Expand Up @@ -302,6 +302,8 @@ def __init__(self, *options: Union[_MatcherT, "AllOf[_MatcherT]"]) -> None:
for option in options:
if isinstance(option, OneOf):
raise Exception("Cannot use AllOf and OneOf in combination!")
elif isinstance(option, TypeOf):
raise Exception("Cannot use AllOf and TypeOf in combination!")
elif isinstance(option, AllOf):
actual_options.extend(option.options)
else:
Expand Down
7 changes: 7 additions & 0 deletions libcst/matchers/tests/test_matchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,13 @@ def test_or_matcher_true(self) -> None:
self.assertTrue(
matches(cst.Name("True"), m.OneOf(m.Name("True"), m.Name("False")))
)
# Match when one of the option is a TypeOf
self.assertTrue(
matches(
cst.Name("True"),
m.OneOf(m.TypeOf(m.Name, m.NameItem)("True"), m.Name("False")),
)
)
# Match any assignment that assigns a value of True or False to an
# unspecified target.
self.assertTrue(
Expand Down

0 comments on commit fc622ce

Please sign in to comment.