Skip to content

Commit

Permalink
Merge pull request #147 from danthedeckie/catch-warnings
Browse files Browse the repository at this point in the history
Catch warnings
  • Loading branch information
danthedeckie authored Oct 4, 2024
2 parents b86d36f + c14afc0 commit 9c90b10
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test:
python test_simpleeval.py
python -Werror test_simpleeval.py

autotest:
find . -name \*.py -not -path .\/.v\* | entr make test
Expand Down
19 changes: 11 additions & 8 deletions simpleeval.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
- koenigsley (Mikhail Yeremeyev) documentation typos correction.
- kurtmckee (Kurt McKee) Infrastructure updates
- edgarrmondragon (Edgar Ramírez-Mondragón) Address Python 3.12+ deprecation warnings
- cedk (Cédric Krier) <[email protected]> Allow running tests with Werror
-------------------------------------
Basic Usage:
Expand Down Expand Up @@ -370,16 +371,18 @@ def __init__(self, operators=None, functions=None, names=None):
ast.Constant: self._eval_constant,
}

# py3.12 deprecated ast.Num, ast.Str, ast.NameConstant
# https://docs.python.org/3.12/whatsnew/3.12.html#deprecated
if Num := getattr(ast, "Num"):
self.nodes[Num] = self._eval_num
with warnings.catch_warnings():
warnings.simplefilter("ignore")
# py3.12 deprecated ast.Num, ast.Str, ast.NameConstant
# https://docs.python.org/3.12/whatsnew/3.12.html#deprecated
if Num := getattr(ast, "Num"):
self.nodes[Num] = self._eval_num

if Str := getattr(ast, "Str"):
self.nodes[Str] = self._eval_str
if Str := getattr(ast, "Str"):
self.nodes[Str] = self._eval_str

if NameConstant := getattr(ast, "NameConstant"):
self.nodes[NameConstant] = self._eval_constant
if NameConstant := getattr(ast, "NameConstant"):
self.nodes[NameConstant] = self._eval_constant

# Defaults:

Expand Down
8 changes: 8 additions & 0 deletions test_simpleeval.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ class TestEvaluator(DRYTest):
def test_only_evalutate_first_statement(self):
# it only evaluates the first statement:
with warnings.catch_warnings(record=True) as ws:
warnings.simplefilter("always")
self.t("11; x = 21; x + x", 11)
self.assertIsInstance(ws[0].message, simpleeval.MultipleExpressions)

Expand Down Expand Up @@ -791,6 +792,7 @@ def test_none(self):
# or if you attempt to assign an unknown name to another
with self.assertRaises(NameNotDefined):
with warnings.catch_warnings(record=True) as ws:
warnings.simplefilter("always")
self.t("s += a", 21)
self.assertIsInstance(ws[0].message, simpleeval.AssignmentAttempted)

Expand All @@ -816,6 +818,7 @@ def test_dict(self):

# however, you can't assign to those names:
with warnings.catch_warnings(record=True) as ws:
warnings.simplefilter("always")
self.t("a = 200", 200)
self.assertIsInstance(ws[0].message, simpleeval.AssignmentAttempted)

Expand All @@ -826,6 +829,7 @@ def test_dict(self):
self.s.names["b"] = [0]

with warnings.catch_warnings(record=True) as ws:
warnings.simplefilter("always")
self.t("b[0] = 11", 11)
self.assertIsInstance(ws[0].message, simpleeval.AssignmentAttempted)

Expand All @@ -849,6 +853,7 @@ def test_dict(self):
# you still can't assign though:

with warnings.catch_warnings(record=True) as ws:
warnings.simplefilter("always")
self.t("c['b'] = 99", 99)
self.assertIsInstance(ws[0].message, simpleeval.AssignmentAttempted)

Expand All @@ -859,6 +864,7 @@ def test_dict(self):
self.s.names["c"]["c"] = {"c": 11}

with warnings.catch_warnings(record=True) as ws:
warnings.simplefilter("always")
self.t("c['c']['c'] = 21", 21)
self.assertIsInstance(ws[0].message, simpleeval.AssignmentAttempted)

Expand All @@ -874,13 +880,15 @@ def test_dict_attr_access(self):
self.t("a.b.c*2", 84)

with warnings.catch_warnings(record=True) as ws:
warnings.simplefilter("always")
self.t("a.b.c = 11", 11)
self.assertIsInstance(ws[0].message, simpleeval.AssignmentAttempted)

self.assertEqual(self.s.names["a"]["b"]["c"], 42)

# TODO: Wat?
with warnings.catch_warnings(record=True) as ws:
warnings.simplefilter("always")
self.t("a.d = 11", 11)

with self.assertRaises(KeyError):
Expand Down

0 comments on commit 9c90b10

Please sign in to comment.