-
Notifications
You must be signed in to change notification settings - Fork 205
Description
Since 3414c6e from gh-701 import sympy fails:
>>> import sympy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/oscar/current/active/sympy/sympy/__init__.py", line 30, in <module>
from sympy.core.cache import lazy_function
File "/home/oscar/current/active/sympy/sympy/core/__init__.py", line 9, in <module>
from .expr import Expr, AtomicExpr, UnevaluatedExpr
File "/home/oscar/current/active/sympy/sympy/core/expr.py", line 4159, in <module>
from .mul import Mul
File "/home/oscar/current/active/sympy/sympy/core/mul.py", line 2193, in <module>
from .numbers import Rational
File "/home/oscar/current/active/sympy/sympy/core/numbers.py", line 30, in <module>
from mpmath.ctx_mp import mpnumeric
ImportError: cannot import name 'mpnumeric' from 'mpmath.ctx_mp' (/home/oscar/current/active/mpmath/mpmath/ctx_mp.py)The mpnumeric class is the base class for _mpf and _mpc and is used by sympy for detecting when an object is an instance of mpf or mpc. This is needed since _mpf and _mpc seem to be designated private and mpf and mpc are dynamically generated types (gh-696, gh-532).
The fix for that is straight-forward:
diff --git a/mpmath/ctx_mp.py b/mpmath/ctx_mp.py
index c17017e..6cf8e33 100644
--- a/mpmath/ctx_mp.py
+++ b/mpmath/ctx_mp.py
@@ -31,6 +31,9 @@
from .ctx_mp_python import PythonMPContext as BaseMPContext
+from .ctx_mp_python import _mpf, _mpc, mpnumeric
+
+
class MPContext(BaseMPContext, StandardBaseContext):
"""
Context for multiprecision arithmetic with a global precision.However then a new error arises:
>>> import sympy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/oscar/current/active/sympy/sympy/__init__.py", line 30, in <module>
from sympy.core.cache import lazy_function
File "/home/oscar/current/active/sympy/sympy/core/__init__.py", line 9, in <module>
from .expr import Expr, AtomicExpr, UnevaluatedExpr
File "/home/oscar/current/active/sympy/sympy/core/expr.py", line 4159, in <module>
from .mul import Mul
File "/home/oscar/current/active/sympy/sympy/core/mul.py", line 2193, in <module>
from .numbers import Rational
File "/home/oscar/current/active/sympy/sympy/core/numbers.py", line 4554, in <module>
_sympy_converter[type(mpmath.rational.mpq(1, 2))] = sympify_mpmath_mpq
^^^^^^^^^^^^^^^
AttributeError: module 'mpmath' has no attribute 'rational'This is because the mpmath.rational module was removed in gh-691. I'm not sure if there ever is a reason that sympy would need to convert an mpmath mpq. If there is a reason though then it is not clear what the replacement should be since gh-691 describes it as private.
This is enough to make import sympy work:
diff --git a/mpmath/__init__.py b/mpmath/__init__.py
index eb7004d..49acc4c 100644
--- a/mpmath/__init__.py
+++ b/mpmath/__init__.py
@@ -2,6 +2,8 @@
from .usertools import monitor, timing
+from . import rational
+
from .ctx_fp import FPContext
from .ctx_mp import MPContext
from .ctx_iv import MPIntervalContext
diff --git a/mpmath/ctx_mp.py b/mpmath/ctx_mp.py
index c17017e..6cf8e33 100644
--- a/mpmath/ctx_mp.py
+++ b/mpmath/ctx_mp.py
@@ -31,6 +31,9 @@
from .ctx_mp_python import PythonMPContext as BaseMPContext
+from .ctx_mp_python import _mpf, _mpc, mpnumeric
+
+
class MPContext(BaseMPContext, StandardBaseContext):
"""
Context for multiprecision arithmetic with a global precision.
diff --git a/mpmath/rational.py b/mpmath/rational.py
new file mode 100644
index 0000000..7fbc2ca
--- /dev/null
+++ b/mpmath/rational.py
@@ -0,0 +1 @@
+from .libmp import MPQ as mpqI wonder more generally though what should be expected in terms of backwards compatibility here. Since mpmath has not changed much in a long time there will be a lot of downstream code that depends on being able to import different things from different modules and it is not clear exactly what should be considered public or private. In gh-691 then mpq type is described as being "private" but actually it could be imported in a way that does not look like accessing anything private:
from mpmath.rational import mpqIt is not hard to make changes in sympy that would accommodate these changes in mpmath but it does make me wonder how much other code downstream from mpmath might be broken by these rearrangements of what is publicly importable. It can be difficult to see exactly how some change affects what is importable because things might even be implicitly imported (like the mpmath.rational module which was not explicitly imported in mpmath/__init__.py).