Skip to content

Commit 9c57fd0

Browse files
committed
fix(core): don't parse strings in Basic.has
The Basic.has method is changed from using sympify to _sympify so that string input is no longer accepted and so that functions are no longer parsed as strings. A line in solveset needed to be fixed because it was erroneously calling expr.as_independent([x, y]) rather than expr.as_independent(x, y). BREAKING CHANGE Basic.has no longer accepts strings so e.g. Symbol('x').has('x') will raise an exception.
1 parent 5855696 commit 9c57fd0

File tree

4 files changed

+6
-7
lines changed

4 files changed

+6
-7
lines changed

sympy/core/basic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,7 @@ def _has(self, pattern):
12391239
return any(f.func == pattern or f == pattern
12401240
for f in self.atoms(Function, UndefinedFunction))
12411241

1242-
pattern = sympify(pattern)
1242+
pattern = _sympify(pattern)
12431243
if isinstance(pattern, BasicMeta):
12441244
return any(isinstance(arg, pattern)
12451245
for arg in preorder_traversal(self))

sympy/core/tests/test_basic.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
_atomic, _aresame)
99
from sympy.core.singleton import S
1010
from sympy.core.symbol import symbols, Symbol
11+
from sympy.core.sympify import SympifyError
1112
from sympy.core.function import Function, Lambda
1213
from sympy.core.compatibility import default_sort_key
1314

@@ -102,6 +103,7 @@ def test_has():
102103
assert b21.has(Basic)
103104
assert not b1.has(b21, b3)
104105
assert not b21.has()
106+
raises(SympifyError, lambda: Symbol("x").has("x"))
105107

106108

107109
def test_subs():

sympy/functions/elementary/miscellaneous.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,7 @@ def sqrt(arg, evaluate=None):
129129
>>> sqrt(x).has(sqrt)
130130
Traceback (most recent call last):
131131
...
132-
sympy.core.sympify.SympifyError: Sympify of expression 'could not parse
133-
'<function sqrt at 0x7f79ad860f80>'' failed, because of exception being
134-
raised:
135-
SyntaxError: invalid syntax
132+
sympy.core.sympify.SympifyError: SympifyError: <function sqrt at 0x10e8900d0>
136133
137134
To find ``sqrt`` look for ``Pow`` with an exponent of ``1/2``:
138135

sympy/solvers/solveset.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3003,8 +3003,8 @@ def _solve_using_known_values(result, solver):
30033003
# list.
30043004
result.remove(res)
30053005
continue # skip as it's independent of desired symbols
3006-
depen = (eq2.rewrite(Add)).as_independent(unsolved_syms)[0]
3007-
if depen.has(Abs) and solver == solveset_complex:
3006+
depen1, depen2 = (eq2.rewrite(Add)).as_independent(*unsolved_syms)
3007+
if (depen1.has(Abs) or depen2.has(Abs)) and solver == solveset_complex:
30083008
# Absolute values cannot be inverted in the
30093009
# complex domain
30103010
continue

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy