Skip to content

Commit

Permalink
Support for "in" comparison operator
Browse files Browse the repository at this point in the history
  • Loading branch information
atait committed Jan 2, 2021
1 parent dd4a549 commit 9fe14bc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pragma/core/resolve/__init__.py
Expand Up @@ -75,6 +75,8 @@ def resolve_name_or_attribute(node, ctxt):
ast.LtE: lambda a, b: a <= b,
ast.Gt: lambda a, b: a > b,
ast.GtE: lambda a, b: a >= b,
ast.In: lambda a, b: a in b,
ast.NotIn: lambda a, b: a not in b,
}

try:
Expand Down
26 changes: 26 additions & 0 deletions tests/test_collapse_literals.py
Expand Up @@ -637,3 +637,29 @@ def f(x):
yield 0
'''
self.assertSourceEqual(f, result)

def test_collapse_InOp(self):
lst = ['a', 'b', object()]
dct = dict(a=1, b=2)

@pragma.collapse_literals()
def f():
if 'a' in lst:
yield 0
if 'v' in lst:
unreachable
if 'b' not in lst:
unreachable
yield dct['a']
if 'b' in dct:
yield 2
# if 2 in dct.values(): # TODO: support this. Problem is that values is not a pure function.
# yield 2

result = '''
def f():
yield 0
yield 1
yield 2
'''
self.assertSourceEqual(f, result)

0 comments on commit 9fe14bc

Please sign in to comment.