001ReviewmidPython / SymPy真实来源改编 SymPy

SymPy Point:修复 evaluate(False) 下的坐标校验误报

审查一个 SymPy 几何模块的补丁:作者修复 evaluate(False) 环境下构造 Point 被误报 Imaginary coordinates are not permitted 的问题。

ai-pr.diffdiff · 44 lines
diff --git a/sympy/geometry/point.py b/sympy/geometry/point.pyindex 73c20334d..5c8636ee6 100644--- a/sympy/geometry/point.py+++ b/sympy/geometry/point.py@@ -149,9 +149,12 @@ def __new__(cls, *args, **kwargs):         if any(coords[dim:]):             raise ValueError('Nonzero coordinates cannot be removed.')-        if any(a.is_number and im(a) for a in coords):+        # With automatic evaluation disabled, im(a) stays unevaluated (e.g.+        # im(1)), which made perfectly real coordinates look imaginary+        # (issue #22684). Only run this validation when evaluation is on.+        if evaluate and any(a.is_number and im(a) for a in coords):             raise ValueError('Imaginary coordinates are not permitted.')         if not all(isinstance(a, Expr) for a in coords):             raise TypeError('Coordinates must be valid SymPy expressions.')          # pad with zeros appropriately         coords = coords[:dim] + (S.Zero,)*(dim - len(coords))diff --git a/sympy/geometry/tests/test_point.py b/sympy/geometry/tests/test_point.pyindex 930d7fec2..8a1c2b3d4 100644--- a/sympy/geometry/tests/test_point.py+++ b/sympy/geometry/tests/test_point.py@@ -1,5 +1,6 @@ from sympy.core.basic import Basic from sympy.core.numbers import (I, Float, Rational, pi)+from sympy.core.parameters import evaluate from sympy.core.singleton import S from sympy.core.symbol import Symbol from sympy.core.sympify import sympify@@ -452,6 +453,13 @@ def test__normalize_dimension():     assert Point._normalize_dimension(Point(1, 2), Point(3, 4, 0)) == [         Point(1, 2, 0), Point(3, 4, 0)] ++def test_issue_22684():+    # Used to raise 'Imaginary coordinates are not permitted.'+    with evaluate(False):+        p = Point(1, 2)+    assert p == Point(1, 2)+  def test_direction_cosine():     p1 = Point3D(0, 0, 0)