From 19dfd3215f823586d7f94a3200ef514270ab3f87 Mon Sep 17 00:00:00 2001 From: Feanil Patel Date: Wed, 3 Apr 2024 12:26:15 -0400 Subject: [PATCH] feat: Add python 3.11 support. Update evaluation functions to not coerce integers to floating point numbers when not necessary. --- calc/__init__.py | 2 +- calc/calc.py | 10 +++++++--- tests/test_calc.py | 4 ++-- tox.ini | 4 +++- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/calc/__init__.py b/calc/__init__.py index 434fcd9..1a9b5fa 100644 --- a/calc/__init__.py +++ b/calc/__init__.py @@ -6,4 +6,4 @@ from .calc import * -__version__ = '3.0.1' +__version__ = '3.1.0' diff --git a/calc/calc.py b/calc/calc.py index bee9310..1fea477 100644 --- a/calc/calc.py +++ b/calc/calc.py @@ -127,7 +127,11 @@ def eval_number(parse_result): e.g. [ '7.13', 'e', '3' ] -> 7130 Calls super_float above. """ - return super_float("".join(parse_result)) + for item in parse_result: + if "." in item or "e" in item or "E" in item: + return super_float("".join(parse_result)) + + return int("".join(parse_result)) def eval_atom(parse_result): @@ -185,7 +189,7 @@ def eval_sum(parse_result): Allow a leading + or -. """ - total = 0.0 + total = 0 current_op = operator.add for token in parse_result: if token == '+': @@ -203,7 +207,7 @@ def eval_product(parse_result): [ 1, '*', 2, '/', 3 ] -> 0.66 """ - prod = 1.0 + prod = 1 current_op = operator.mul for token in parse_result: if token == '*': diff --git a/tests/test_calc.py b/tests/test_calc.py index bb980b4..59c223f 100644 --- a/tests/test_calc.py +++ b/tests/test_calc.py @@ -348,9 +348,9 @@ def test_other_functions(self): self.assert_function_values('factorial', fact_inputs, fact_values) self.assertRaises(ValueError, calc.evaluator, {}, {}, "fact(-1)") - self.assertRaises(ValueError, calc.evaluator, {}, {}, "fact(0.5)") + self.assertRaises((ValueError, TypeError), calc.evaluator, {}, {}, "fact(0.5)") self.assertRaises(ValueError, calc.evaluator, {}, {}, "factorial(-1)") - self.assertRaises(ValueError, calc.evaluator, {}, {}, "factorial(0.5)") + self.assertRaises((ValueError, TypeError), calc.evaluator, {}, {}, "factorial(0.5)") def test_constants(self): """ diff --git a/tox.ini b/tox.ini index 4e25818..3d77849 100644 --- a/tox.ini +++ b/tox.ini @@ -1,10 +1,11 @@ [tox] -envlist = py38,quality +envlist = py{38,311},quality [testenv] allowlist_externals = touch deps = + setuptools -r requirements/test.txt commands = coverage run setup.py test @@ -12,6 +13,7 @@ commands = [testenv:quality] deps = + setuptools -r requirements/test.txt commands = pycodestyle calc symmath tests