Skip to content

Commit

Permalink
Merge pull request #23122 from wermos/visit-constant
Browse files Browse the repository at this point in the history
Put the `visit_Num` function back into `ast_parser.py`
  • Loading branch information
oscarbenjamin committed Feb 21, 2022
2 parents dacb8f2 + a99a946 commit 1de2e07
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
11 changes: 11 additions & 0 deletions sympy/parsing/ast_parser.py
Expand Up @@ -33,6 +33,17 @@ def __init__(self, local_dict, global_dict):
self.global_dict = global_dict

def visit_Constant(self, node):
if isinstance(node.value, int):
return fix_missing_locations(Call(func=Name('Integer', Load()),
args=[node], keywords=[]))
elif isinstance(node.value, float):
return fix_missing_locations(Call(func=Name('Float', Load()),
args=[node], keywords=[]))
return node

def visit_Num(self, node):
"""This function exists for backwards compatibility with Python 3.7.
It should be removed when SymPy removes support for Python 3.7."""
if isinstance(node.n, int):
return fix_missing_locations(Call(func=Name('Integer', Load()),
args=[node], keywords=[]))
Expand Down
14 changes: 14 additions & 0 deletions sympy/parsing/tests/test_ast_parser.py
@@ -1,8 +1,10 @@
from sympy.core.numbers import Rational
from sympy.core.singleton import S
from sympy.core.symbol import symbols
from sympy.parsing.ast_parser import parse_expr
from sympy.testing.pytest import raises
from sympy.core.sympify import SympifyError
import warnings

def test_parse_expr():
a, b = symbols('a, b')
Expand All @@ -17,3 +19,15 @@ def test_parse_expr():
# tests Transform.visit_Name
assert parse_expr('Rational(1, 2)', {}) == S(1)/2
assert parse_expr('a', {'a': a}) == a

# tests issue_23092
with warnings.catch_warnings():
warnings.simplefilter('error')
assert parse_expr('6 * 7', {}) == S(42)
# Note: The test below can be removed when support for Python 3.7 is
# dropped. This test exists to ensure that the visit_Num function
# exists for Python 3.7, because in 3.7, Python didn't use the
# visit_Constant to create AST Nodes yet.
test_expr = parse_expr('1 / 3', {})
assert test_expr == S(1)/3 # sanity check
assert isinstance(test_expr, Rational)

0 comments on commit 1de2e07

Please sign in to comment.