Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: flake8-eyeo/flake8_eyeo.py

Issue 29565854: Noissue - Improved accuracy of evaluated expressions for A103 and A207 (Closed)
Patch Set: Changed behavior of A207 Created Oct. 11, 2017, 6:25 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | flake8-eyeo/tests/A103.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: flake8-eyeo/flake8_eyeo.py
===================================================================
--- a/flake8-eyeo/flake8_eyeo.py
+++ b/flake8-eyeo/flake8_eyeo.py
@@ -45,15 +45,16 @@
VOLATILE = object()
-def evaluate(node):
+def evaluate(node, namespace):
try:
- return eval(compile(ast.Expression(node), '', 'eval'), {})
+ return eval(compile(ast.Expression(node), '', 'eval'), namespace)
except Exception:
return VOLATILE
def is_const(node):
- return evaluate(node) is not VOLATILE
+ namespace = {'__builtins__': {'True': True, 'False': False, 'None': None}}
+ return evaluate(node, namespace) is not VOLATILE
def get_identifier(node):
@@ -335,8 +336,9 @@
def _visit_hash_keys(self, nodes, what):
keys = []
+ namespace = collections.defaultdict(object, vars(builtins))
Vasily Kuznetsov 2017/10/11 19:29:02 This is clever!
for node in nodes:
- key = evaluate(node)
+ key = evaluate(node, namespace)
if key is VOLATILE:
continue
« no previous file with comments | « no previous file | flake8-eyeo/tests/A103.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld