diff --git a/Makefile b/Makefile index c33fabf..bcc6ba1 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ test: - python test_simpleeval.py + python -Werror test_simpleeval.py autotest: find . -name \*.py -not -path .\/.v\* | entr make test diff --git a/simpleeval.py b/simpleeval.py index 2839129..57e27b8 100644 --- a/simpleeval.py +++ b/simpleeval.py @@ -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) Allow running tests with Werror ------------------------------------- Basic Usage: @@ -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: diff --git a/test_simpleeval.py b/test_simpleeval.py index 6152706..26c0d3a 100644 --- a/test_simpleeval.py +++ b/test_simpleeval.py @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -874,6 +880,7 @@ 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) @@ -881,6 +888,7 @@ def test_dict_attr_access(self): # TODO: Wat? with warnings.catch_warnings(record=True) as ws: + warnings.simplefilter("always") self.t("a.d = 11", 11) with self.assertRaises(KeyError):