Skip to content

Commit 9b7009f

Browse files
committed
Handle set() as literal as in ast.literal_eval
1 parent 13d4671 commit 9b7009f

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

bpython/simpleeval.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def simple_eval(node_or_string, namespace=None):
9595
def _convert(node):
9696
if py3 and isinstance(node, ast.Constant):
9797
return node.value
98-
if isinstance(node, _string_type_nodes):
98+
elif isinstance(node, _string_type_nodes):
9999
return node.s
100100
elif isinstance(node, ast.Num):
101101
return node.n
@@ -110,6 +110,13 @@ def _convert(node):
110110
)
111111
elif isinstance(node, ast.Set):
112112
return set(map(_convert, node.elts))
113+
elif (
114+
isinstance(node, Call)
115+
and isinstance(node.func, Name)
116+
and node.func.id == "set"
117+
and node.args == node.keywords == []
118+
):
119+
return set()
113120

114121
# this is a deviation from literal_eval: we allow non-literals
115122
elif isinstance(node, _name_type_nodes):
@@ -256,4 +263,3 @@ def evaluate_current_attribute(cursor_offset, line, namespace=None):
256263
raise EvaluationError(
257264
"can't lookup attribute %s on %r" % (attr.word, obj)
258265
)
259-

bpython/test/test_simpleeval.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def test_matches_stdlib(self):
2525
def test_matches_stdlib_py3(self):
2626
"""Should match the stdlib literal_eval if no names or indexing"""
2727
self.assertMatchesStdlib("{1, 2}")
28+
self.assertMatchesStdlib("set()")
2829

2930
def test_indexing(self):
3031
"""Literals can be indexed into"""

0 commit comments

Comments
 (0)