diff --git a/src/sage/geometry/cone.py b/src/sage/geometry/cone.py index dc195231441..e307ded95d9 100644 --- a/src/sage/geometry/cone.py +++ b/src/sage/geometry/cone.py @@ -201,9 +201,11 @@ # (at your option) any later version. # https://www.gnu.org/licenses/ # **************************************************************************** +from __future__ import annotations from collections.abc import Hashable, Iterable, Container from copy import copy +from typing import TYPE_CHECKING, Literal from warnings import warn from sage.misc.lazy_import import lazy_import @@ -241,6 +243,9 @@ lazy_import('ppl', ['ray', 'point'], as_=['PPL_ray', 'PPL_point'], feature=PythonModule("ppl", spkg='pplpy', type='standard')) +if TYPE_CHECKING: + from sage.misc.sage_input import SageInputBuilder, SageInputExpression + def Cone(rays, lattice=None, check=True, normalize=True): r""" @@ -1481,7 +1486,7 @@ def __init__(self, rays=None, lattice=None, if PPL is not None: self._PPL_C_Polyhedron = PPL - def _sage_input_(self, sib, coerced): + def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression: """ Return Sage command to reconstruct ``self``. diff --git a/src/sage/geometry/fan.py b/src/sage/geometry/fan.py index ab17d495703..6093848b46f 100644 --- a/src/sage/geometry/fan.py +++ b/src/sage/geometry/fan.py @@ -233,9 +233,11 @@ # (at your option) any later version. # https://www.gnu.org/licenses/ # **************************************************************************** +from __future__ import annotations from collections.abc import Callable, Container from copy import copy +from typing import TYPE_CHECKING, Literal from warnings import warn import sage.geometry.abc @@ -262,6 +264,9 @@ from sage.rings.integer_ring import ZZ from sage.rings.rational_field import QQ +if TYPE_CHECKING: + from sage.misc.sage_input import SageInputBuilder, SageInputExpression + def Fan(cones, rays=None, lattice=None, check=True, normalize=True, is_complete=None, virtual_rays=None, discard_faces=False, @@ -1187,7 +1192,7 @@ def __init__(self, cones, rays, lattice, if virtual_rays is not None: self._virtual_rays = PointCollection(virtual_rays, self.lattice()) - def _sage_input_(self, sib, coerced): + def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression: """ Return Sage command to reconstruct ``self``. diff --git a/src/sage/geometry/lattice_polytope.py b/src/sage/geometry/lattice_polytope.py index 799b26766b1..c032219d3d4 100644 --- a/src/sage/geometry/lattice_polytope.py +++ b/src/sage/geometry/lattice_polytope.py @@ -120,42 +120,43 @@ # (at your option) any later version. # https://www.gnu.org/licenses/ # **************************************************************************** +from __future__ import annotations + +import os +import shlex from collections.abc import Hashable from copyreg import constructor as copyreg_constructor from functools import reduce from io import IOBase, StringIO -from subprocess import Popen, PIPE -from warnings import warn -import os -import shlex +from subprocess import PIPE, Popen +from typing import Literal +import sage.geometry.abc from sage.arith.misc import GCD as gcd from sage.categories.monoids import Monoids from sage.features import PythonModule -from sage.features.palp import PalpExecutable from sage.features.databases import DatabaseReflexivePolytopes +from sage.features.palp import PalpExecutable from sage.geometry.cone import _ambient_space_point, integral_length -from sage.geometry.point_collection import (PointCollection, - read_palp_point_collection) -from sage.geometry.toric_lattice import ToricLattice, ToricLattice_generic from sage.geometry.convex_set import ConvexSet_compact +from sage.geometry.point_collection import PointCollection, read_palp_point_collection +from sage.geometry.toric_lattice import ToricLattice, ToricLattice_generic from sage.matrix.constructor import matrix from sage.misc.cachefunc import cached_method from sage.misc.flatten import flatten from sage.misc.lazy_import import lazy_import +from sage.misc.sage_input import SageInputBuilder, SageInputExpression from sage.misc.temporary_file import tmp_filename from sage.modules.free_module_element import vector from sage.rings.integer import Integer from sage.rings.integer_ring import ZZ from sage.rings.rational_field import QQ -from sage.structure.element import Matrix, Element -from sage.structure.richcmp import richcmp_method, richcmp -from sage.structure.parent import Parent +from sage.structure.element import Element, Matrix +from sage.structure.parent import Parent, Set_generic +from sage.structure.richcmp import richcmp, richcmp_method from sage.structure.sage_object import SageObject from sage.structure.sequence import Sequence from sage.structure.unique_representation import UniqueRepresentation -import sage.geometry.abc - lazy_import("sage.combinat.posets.posets", 'FinitePoset') lazy_import("sage.geometry.hasse_diagram", 'lattice_from_incidences') @@ -170,6 +171,34 @@ feature=PythonModule("ppl", spkg='pplpy', type='standard')) +class SetOfAllLatticePolytopesClass(Set_generic): + def _repr_(self) -> str: + r""" + Return a string representation. + + TESTS:: + + sage: lattice_polytope.SetOfAllLatticePolytopesClass()._repr_() + 'Set of all Lattice Polytopes' + """ + return "Set of all Lattice Polytopes" + + def __call__(self, x): + r""" + TESTS:: + + sage: o = lattice_polytope.cross_polytope(3) + sage: lattice_polytope.SetOfAllLatticePolytopesClass().__call__(o) + 3-d reflexive polytope in 3-d lattice M + """ + if isinstance(x, LatticePolytopeClass): + return x + raise TypeError + + +SetOfAllLatticePolytopes = SetOfAllLatticePolytopesClass() + + def LatticePolytope(data, compute_vertices=True, n=0, lattice=None): r""" Construct a lattice polytope. @@ -517,7 +546,7 @@ def __init__(self, parent, points=None, compute_vertices=None, self._vertices = ambient.vertices(self._ambient_vertex_indices) Element.__init__(self, parent) - def _sage_input_(self, sib, coerced): + def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression: """ Return Sage command to reconstruct ``self``. @@ -4469,7 +4498,7 @@ def _repr_(self): pass return result - def _sage_input_(self, sib, coerced): + def _sage_input_(self, sib: SageInputBuilder, coerced: bool) -> SageInputExpression: """ Return Sage command to reconstruct ``self``. diff --git a/src/sage/geometry/point_collection.pyx b/src/sage/geometry/point_collection.pyx index 8cebd2fd2d8..752586eb6c5 100644 --- a/src/sage/geometry/point_collection.pyx +++ b/src/sage/geometry/point_collection.pyx @@ -83,6 +83,8 @@ from sage.geometry.toric_lattice import ToricLattice from sage.matrix.constructor import matrix from sage.misc.latex import latex +from sage.misc.sage_input import SageInputBuilder, SageInputExpression + def is_PointCollection(x): r""" @@ -172,7 +174,7 @@ cdef class PointCollection(SageObject): self._points = tuple(points) self._module = self._points[0].parent() if module is None else module - def _sage_input_(self, sib, coerced): + def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression: r""" Return Sage command to reconstruct ``self``. diff --git a/src/sage/geometry/polyhedron/base0.py b/src/sage/geometry/polyhedron/base0.py index 0d75e9cab82..7914b979478 100644 --- a/src/sage/geometry/polyhedron/base0.py +++ b/src/sage/geometry/polyhedron/base0.py @@ -29,12 +29,17 @@ # (at your option) any later version. # https://www.gnu.org/licenses/ # **************************************************************************** +from __future__ import annotations +from typing import TYPE_CHECKING, Literal from sage.misc.cachefunc import cached_method from sage.misc.abstract_method import abstract_method from sage.structure.element import Element import sage.geometry.abc +if TYPE_CHECKING: + from sage.misc.sage_input import SageInputBuilder, SageInputExpression + class Polyhedron_base0(Element, sage.geometry.abc.Polyhedron): """ @@ -296,7 +301,7 @@ def _delete(self): """ self.parent().recycle(self) - def _sage_input_(self, sib, coerced): + def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression: """ Return Sage command to reconstruct ``self``. diff --git a/src/sage/geometry/toric_lattice.py b/src/sage/geometry/toric_lattice.py index e4f606c76aa..715e93683fd 100644 --- a/src/sage/geometry/toric_lattice.py +++ b/src/sage/geometry/toric_lattice.py @@ -144,7 +144,9 @@ # # https://www.gnu.org/licenses/ # **************************************************************************** +from __future__ import annotations +from typing import TYPE_CHECKING, Literal from sage.geometry.toric_lattice_element import ToricLatticeElement from sage.misc.lazy_import import lazy_import lazy_import('sage.geometry.toric_plotter', 'ToricPlotter') @@ -162,6 +164,9 @@ from sage.rings.rational_field import QQ from sage.structure.factory import UniqueFactory +if TYPE_CHECKING: + from sage.misc.sage_input import SageInputBuilder, SageInputExpression + class ToricLatticeFactory(UniqueFactory): r""" @@ -825,7 +830,7 @@ def __init__(self, rank, name, dual_name, latex_name, latex_dual_name): self._latex_name = latex_name self._latex_dual_name = latex_dual_name - def _sage_input_(self, sib, coerced): + def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression: r""" Return Sage command to reconstruct ``self``. diff --git a/src/sage/matrix/matrix1.pyx b/src/sage/matrix/matrix1.pyx index fe4586d5176..a326808868b 100644 --- a/src/sage/matrix/matrix1.pyx +++ b/src/sage/matrix/matrix1.pyx @@ -24,6 +24,7 @@ from cpython.sequence cimport PySequence_Fast import sage.modules.free_module from sage.structure.coerce cimport coercion_model +from sage.misc.sage_input import SageInputBuilder, SageInputExpression cdef class Matrix(Matrix0): ################################################### @@ -622,7 +623,7 @@ cdef class Matrix(Matrix0): matrix._sage_object = self return matrix - def _sage_input_(self, sib, coerce): + def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression: r""" Produce an expression which will reproduce this value when evaluated. diff --git a/src/sage/misc/explain_pickle.py b/src/sage/misc/explain_pickle.py index bd378b35d8f..a5966a91985 100644 --- a/src/sage/misc/explain_pickle.py +++ b/src/sage/misc/explain_pickle.py @@ -152,13 +152,14 @@ # (at your option) any later version. # http://www.gnu.org/licenses/ # ***************************************************************************** - +from __future__ import annotations import pickletools import re import sys import types +from typing import TYPE_CHECKING, Literal import zlib as comp import bz2 as comp_other @@ -169,6 +170,9 @@ from sage.misc.persist import (unpickle_override, unpickle_global, dumps, register_unpickle_override, SageUnpickler) +if TYPE_CHECKING: + from sage.misc.sage_input import SageInputBuilder, SageInputExpression + # Python 3 does not have a "ClassType". Instead, we ensure that # isinstance(foo, ClassType) will always return False. @@ -358,7 +362,7 @@ def __init__(self, value, expression): self.expression = expression self.immutable = False - def _sage_input_(self, sib, coerced): + def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression: r""" Extracts the expression from a PickleObject, and sets the immutable flag. diff --git a/src/sage/misc/sage_input.py b/src/sage/misc/sage_input.py index 39909c0d87b..f3fca18070e 100644 --- a/src/sage/misc/sage_input.py +++ b/src/sage/misc/sage_input.py @@ -172,6 +172,9 @@ # https://www.gnu.org/licenses/ # **************************************************************************** +from __future__ import annotations +from typing import Literal + from sage.misc.lazy_import import lazy_import lazy_import('sage.rings.real_mpfi', 'RealIntervalFieldElement') @@ -342,7 +345,7 @@ def __init__(self, allow_locals=False, preparse=True): self._next_local = 1 self._locals = {} - def __call__(self, x, coerced=False): + def __call__(self, x, coerced: bool | Literal[2] = False): r""" Try to convert an arbitrary value ``x`` into a :class:`SageInputExpression` (an SIE). @@ -460,6 +463,7 @@ def __call__(self, x, coerced=False): return x if hasattr(x, '_sage_input_'): + #print(coerced, type(coerced)) return x._sage_input_(self, coerced) if x is None: @@ -592,7 +596,7 @@ def float_str(self, n): """ return SIE_literal_stringrep(self, n) - def name(self, n): + def name(self, n) -> SageInputExpression: r""" Given a string representing a Python name, produces a :class:`SageInputExpression` for that name. diff --git a/src/sage/modules/free_module_element.pyx b/src/sage/modules/free_module_element.pyx index 5bd5b408417..262d8f86618 100644 --- a/src/sage/modules/free_module_element.pyx +++ b/src/sage/modules/free_module_element.pyx @@ -129,6 +129,7 @@ from sage.rings.abc import RealDoubleField, ComplexDoubleField from sage.rings.integer cimport Integer, smallInteger from sage.arith.numerical_approx cimport digits_to_bits +from sage.misc.sage_input import SageInputBuilder, SageInputExpression # For the norm function, we cache Sage integers 1 and 2 __one__ = smallInteger(1) @@ -1259,7 +1260,7 @@ cdef class FreeModuleElement(Vector): # abstract base class return self return self.change_ring(R) - def _sage_input_(self, sib, coerce): + def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression: r""" Produce an expression which will reproduce this value when evaluated. diff --git a/src/sage/rings/complex_interval.pyx b/src/sage/rings/complex_interval.pyx index 6ef90638d3b..fd95ec9f4c3 100644 --- a/src/sage/rings/complex_interval.pyx +++ b/src/sage/rings/complex_interval.pyx @@ -64,6 +64,7 @@ from sage.libs.flint.fmpz cimport * from sage.libs.mpfr cimport MPFR_RNDU from sage.arith.constants cimport LOG_TEN_TWO_PLUS_EPSILON +from sage.misc.sage_input import SageInputBuilder, SageInputExpression from sage.structure.element cimport FieldElement from sage.structure.parent cimport Parent from sage.rings.complex_mpfr cimport ComplexNumber @@ -1002,7 +1003,7 @@ cdef class ComplexIntervalFieldElement(FieldElement): """ raise TypeError - def _sage_input_(self, sib, coerce): + def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression: r""" Produce an expression which will reproduce this value when evaluated. diff --git a/src/sage/rings/complex_interval_field.py b/src/sage/rings/complex_interval_field.py index c090d6cdae2..193469cbddc 100644 --- a/src/sage/rings/complex_interval_field.py +++ b/src/sage/rings/complex_interval_field.py @@ -34,8 +34,9 @@ # (at your option) any later version. # https://www.gnu.org/licenses/ # **************************************************************************** +from __future__ import annotations - +from typing import TYPE_CHECKING, Literal import weakref import sage.rings.abc @@ -48,6 +49,9 @@ from sage.rings.ring import Field from sage.structure.parent import Parent +if TYPE_CHECKING: + from sage.misc.sage_input import SageInputBuilder, SageInputExpression + cache = {} @@ -295,7 +299,7 @@ def _magma_init_(self, magma): """ return "ComplexField(%s : Bits := true)" % self.prec() - def _sage_input_(self, sib, coerce): + def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression: r""" Produce an expression which will reproduce this value when evaluated. diff --git a/src/sage/rings/complex_mpfr.pyx b/src/sage/rings/complex_mpfr.pyx index 11f58008fcf..8e96db12d24 100644 --- a/src/sage/rings/complex_mpfr.pyx +++ b/src/sage/rings/complex_mpfr.pyx @@ -33,6 +33,7 @@ import re import weakref import sage.misc.misc +from sage.misc.sage_input import SageInputBuilder, SageInputExpression from sage.libs.mpfr cimport * @@ -645,7 +646,7 @@ class ComplexField_class(sage.rings.abc.ComplexField): """ return "\\Bold{C}" - def _sage_input_(self, sib, coerce): + def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression: r""" Produce an expression which will reproduce this value when evaluated. @@ -1071,7 +1072,7 @@ cdef class ComplexNumber(sage.structure.element.FieldElement): else: return numpy_object_interface - def _sage_input_(self, sib, coerced): + def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression: r""" Produce an expression which will reproduce this value when evaluated. diff --git a/src/sage/rings/finite_rings/finite_field_base.pyx b/src/sage/rings/finite_rings/finite_field_base.pyx index 8e41ddd04f1..594d929c828 100644 --- a/src/sage/rings/finite_rings/finite_field_base.pyx +++ b/src/sage/rings/finite_rings/finite_field_base.pyx @@ -39,6 +39,7 @@ from sage.categories.finite_fields import FiniteFields from sage.misc.persist import register_unpickle_override from sage.misc.cachefunc import cached_method from sage.misc.prandom import randrange +from sage.misc.sage_input import SageInputBuilder, SageInputExpression from sage.rings.integer cimport Integer import sage.rings.abc @@ -269,7 +270,7 @@ cdef class FiniteField(Field): return "GF(%s,Variable=>symbol %s)" % (self.order(), self.variable_name()) - def _sage_input_(self, sib, coerced): + def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression: r""" Produce an expression which will reproduce this value when evaluated. diff --git a/src/sage/rings/finite_rings/integer_mod.pyx b/src/sage/rings/finite_rings/integer_mod.pyx index 768f2a75927..a565f16976b 100644 --- a/src/sage/rings/finite_rings/integer_mod.pyx +++ b/src/sage/rings/finite_rings/integer_mod.pyx @@ -110,6 +110,7 @@ from sage.categories.morphism cimport Morphism from sage.categories.map cimport Map from sage.misc.persist import register_unpickle_override +from sage.misc.sage_input import SageInputBuilder, SageInputExpression from sage.structure.parent cimport Parent @@ -611,7 +612,7 @@ cdef class IntegerMod_abstract(FiniteRingElement): _fricas_init_ = _axiom_init_ - def _sage_input_(self, sib, coerced): + def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression: r""" Produce an expression which will reproduce this value when evaluated. diff --git a/src/sage/rings/infinity.py b/src/sage/rings/infinity.py index 427a34ecf04..9c70e7a8432 100644 --- a/src/sage/rings/infinity.py +++ b/src/sage/rings/infinity.py @@ -214,22 +214,29 @@ # (at your option) any later version. # https://www.gnu.org/licenses/ # **************************************************************************** +from __future__ import annotations from sys import maxsize +from typing import TYPE_CHECKING, Literal, NoReturn -import sage.rings.abc +from typing_extensions import Self -from sage.structure.parent import Parent +import sage.rings.abc from sage.categories.rings import Rings from sage.categories.semirings import Semirings from sage.misc.fast_methods import Singleton from sage.misc.lazy_import import lazy_import from sage.rings.ring import CommutativeRing -from sage.structure.element import RingElement, InfinityElement +from sage.structure.element import InfinityElement, RingElement +from sage.structure.parent import Parent from sage.structure.richcmp import rich_to_bool, richcmp lazy_import('sage.rings.integer', 'Integer') +if TYPE_CHECKING: + import sympy + + from sage.misc.sage_input import SageInputBuilder, SageInputExpression _obj = {} @@ -251,6 +258,9 @@ def __new__(cls, *args): class AnInfinity: + _sign: Literal[-1, 0, 1] + _sign_char: Literal['', '+', '-'] + """ TESTS:: @@ -354,7 +364,7 @@ def _latex_(self) -> str: """ return self._sign_char + "\\infty" - def __abs__(self): + def __abs__(self) -> AnInfinity: """ EXAMPLES:: @@ -363,7 +373,7 @@ def __abs__(self): """ return -self if self._sign < 0 else self - def _add_(self, other): + def _add_(self, other) -> Self: """ Add ``self`` to ``other``. @@ -400,7 +410,7 @@ def _add_(self, other): raise SignError("cannot add infinity to minus infinity") return self - def _sub_(self, other): + def _sub_(self, other) -> Self: """ EXAMPLES:: @@ -434,7 +444,7 @@ def _sub_(self, other): raise SignError("cannot add infinity to minus infinity") return self - def _mul_(self, other): + def _mul_(self, other) -> AnInfinity: """ EXAMPLES:: @@ -459,7 +469,7 @@ def _mul_(self, other): return self raise SignError("cannot multiply infinity by zero") - def _div_(self, other): + def _div_(self, other) -> AnInfinity: """ EXAMPLES:: @@ -485,7 +495,7 @@ def _div_(self, other): """ return self * ~other - def __float__(self): + def __float__(self) -> float: r""" Generate a floating-point infinity. @@ -519,7 +529,7 @@ def __float__(self): raise ValueError('unsigned infinity cannot be represented in a float') return float(self._sign_char + 'inf') - def lcm(self, x): + def lcm(self, x) -> AnInfinity | Literal[0]: """ Return the least common multiple of ``oo`` and ``x``, which is by definition oo unless ``x`` is 0. @@ -542,7 +552,7 @@ def lcm(self, x): else: return abs(self) - def _sage_input_(self, sib, coerced): + def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression: """ Produce an expression which will reproduce this value when evaluated. @@ -604,7 +614,7 @@ def ngens(self) -> int: """ return 1 - def gen(self, n=0): + def gen(self, n=0) -> UnsignedInfinity: """ The "generator" of ``self`` is the infinity object. @@ -626,7 +636,7 @@ def gen(self, n=0): else: raise IndexError("UnsignedInfinityRing only has one generator") - def gens(self) -> tuple: + def gens(self) -> tuple[UnsignedInfinity]: """ The "generator" of ``self`` is the infinity object. @@ -637,7 +647,7 @@ def gens(self) -> tuple: """ return (self.gen(),) - def less_than_infinity(self): + def less_than_infinity(self) -> LessThanInfinity: """ This is the element that represents a finite value. @@ -665,7 +675,7 @@ def _repr_(self) -> str: """ return "The Unsigned Infinity Ring" - def _element_constructor_(self, x): + def _element_constructor_(self, x) -> UnsignedInfinity | LessThanInfinity: """ The element constructor. @@ -792,7 +802,7 @@ def _latex_(self) -> str: """ return "(<\\infty)" - def _add_(self, other): + def _add_(self, other) -> UnsignedInfinity | Self: """ EXAMPLES:: @@ -805,7 +815,7 @@ def _add_(self, other): return other return self - def _sub_(self, other): + def _sub_(self, other) -> UnsignedInfinity | Self: """ EXAMPLES:: @@ -818,7 +828,7 @@ def _sub_(self, other): return other return self - def _mul_(self, other): + def _mul_(self, other) -> Self: """ EXAMPLES:: @@ -835,7 +845,7 @@ def _mul_(self, other): raise ValueError("oo times number < oo not defined") return self - def _div_(self, other): + def _div_(self, other) -> Integer: """ Can't eliminate possibility of zero division.... @@ -852,7 +862,7 @@ def _div_(self, other): return Integer(0) # noqa: F821 raise ValueError("quotient of number < oo by number < oo not defined") - def _richcmp_(self, other, op) -> bool: + def _richcmp_(self, other, op: int) -> bool: """ Compare ``self`` to ``other``. @@ -903,7 +913,7 @@ def __init__(self): """ InfinityElement.__init__(self, UnsignedInfinityRing) - def __hash__(self): + def __hash__(self) -> int: r""" TESTS:: @@ -913,7 +923,7 @@ def __hash__(self): """ return maxsize - 1 - def _mul_(self, other): + def _mul_(self, other) -> Self: """ Can't rule out an attempt at multiplication by 0. @@ -934,7 +944,7 @@ def _mul_(self, other): return self raise ValueError("unsigned oo times smaller number not defined") - def _sympy_(self): + def _sympy_(self) -> sympy.core.numbers.ComplexInfinity: """ Convert ``unsigned_infinity`` to sympy ``zoo``. @@ -952,7 +962,7 @@ def _sympy_(self): import sympy return sympy.zoo - def _richcmp_(self, other, op) -> bool: + def _richcmp_(self, other, op: int) -> bool: """ Compare ``self`` to ``other``. @@ -978,7 +988,10 @@ class SignError(ArithmeticError): class InfinityRing_class(Singleton, CommutativeRing): - def __init__(self) -> None: + _gen0: PlusInfinity | None = None + _gen1: MinusInfinity | None = None + + def __init__(self) -> None -> None: """ Initialize ``self``. @@ -996,7 +1009,7 @@ def __init__(self) -> None: """ CommutativeRing.__init__(self, self, names=('oo',), normalize=False) - def fraction_field(self): + def fraction_field(self) -> NoReturn: """ This isn't really a ring, let alone an integral domain. @@ -1022,7 +1035,7 @@ def ngens(self) -> int: """ return 2 - def gen(self, n=0): + def gen(self, n=0) -> PlusInfinity | MinusInfinity: """ The two generators are plus and minus infinity. @@ -1037,22 +1050,18 @@ def gen(self, n=0): ... IndexError: n must be 0 or 1 """ - try: - if n == 0: - return self._gen0 - elif n == 1: - return self._gen1 - else: - raise IndexError("n must be 0 or 1") - except AttributeError: - if n == 0: + if n == 0: + if self._gen0 is None: self._gen0 = PlusInfinity() - return self._gen0 - elif n == 1: + return self._gen0 + elif n == 1: + if self._gen1 is None: self._gen1 = MinusInfinity() - return self._gen1 + return self._gen1 + else: + raise IndexError("n must be 0 or 1") - def gens(self) -> tuple: + def gens(self) -> tuple[PlusInfinity, MinusInfinity]: """ The two generators are plus and minus infinity. @@ -1096,7 +1105,7 @@ def _repr_(self) -> str: """ return "The Infinity Ring" - def _element_constructor_(self, x): + def _element_constructor_(self, x) -> PlusInfinity | MinusInfinity | FiniteNumber: """ The element constructor. @@ -1288,7 +1297,7 @@ def __init__(self, parent, x): RingElement.__init__(self, parent) self.value = x - def _richcmp_(self, other, op) -> bool: + def _richcmp_(self, other, op: int) -> bool: """ Compare ``self`` and ``other``. @@ -1308,7 +1317,7 @@ def _richcmp_(self, other, op) -> bool: return rich_to_bool(op, 1) return richcmp(self.value, other.value, op) - def _add_(self, other): + def _add_(self, other) -> InfinityElement | FiniteNumber: """ EXAMPLES:: @@ -1350,7 +1359,7 @@ def _add_(self, other): raise SignError("cannot add positive finite value to negative finite value") return FiniteNumber(self.parent(), self.value + other.value) - def _mul_(self, other): + def _mul_(self, other) -> Integer | FiniteNumber | InfinityElement: """ EXAMPLES:: @@ -1389,7 +1398,7 @@ def _mul_(self, other): raise SignError("cannot multiply infinity by zero") return Integer(0) # noqa: F821 - def _div_(self, other): + def _div_(self, other) -> Integer | FiniteNumber | InfinityElement: """ EXAMPLES:: @@ -1405,7 +1414,7 @@ def _div_(self, other): """ return self * ~other - def __invert__(self): + def __invert__(self) -> Self: """ EXAMPLES:: @@ -1423,7 +1432,7 @@ def __invert__(self): raise ZeroDivisionError("Cannot divide by zero") return self - def _neg_(self): + def _neg_(self) -> FiniteNumber: """ EXAMPLES:: @@ -1472,7 +1481,7 @@ def _latex_(self) -> str: """ return self._repr_() - def __abs__(self): + def __abs__(self) -> FiniteNumber: """ EXAMPLES:: @@ -1487,7 +1496,7 @@ def __abs__(self): return FiniteNumber(self.parent(), 0) return FiniteNumber(self.parent(), 1) - def sign(self): + def sign(self) -> Literal[0, 1, -1]: """ Return the sign of ``self``. @@ -1515,7 +1524,7 @@ def sign(self): return 1 return -1 - def sqrt(self): + def sqrt(self) -> Self: """ EXAMPLES:: @@ -1549,7 +1558,7 @@ def __init__(self): """ InfinityElement.__init__(self, InfinityRing) - def __hash__(self): + def __hash__(self) -> int: r""" TESTS:: @@ -1559,7 +1568,7 @@ def __hash__(self): """ return ~maxsize - def _richcmp_(self, other, op) -> bool: + def _richcmp_(self, other, op: int) -> bool: """ Compare ``self`` and ``other``. @@ -1577,7 +1586,7 @@ def _richcmp_(self, other, op) -> bool: return rich_to_bool(op, 0) return rich_to_bool(op, -1) - def _neg_(self): + def _neg_(self) -> PlusInfinity: """ EXAMPLES:: @@ -1586,7 +1595,7 @@ def _neg_(self): """ return self.parent().gen(0) - def sqrt(self): + def sqrt(self) -> NoReturn: """ EXAMPLES:: @@ -1648,7 +1657,7 @@ def __init__(self): """ InfinityElement.__init__(self, InfinityRing) - def __hash__(self): + def __hash__(self) -> int: r""" TESTS:: @@ -1658,7 +1667,7 @@ def __hash__(self): """ return maxsize - def _richcmp_(self, other, op) -> bool: + def _richcmp_(self, other, op: int) -> bool: """ Compare ``self`` and ``other``. @@ -1676,7 +1685,7 @@ def _richcmp_(self, other, op) -> bool: return rich_to_bool(op, 0) return rich_to_bool(op, 1) - def _neg_(self): + def _neg_(self) -> MinusInfinity: """ TESTS:: @@ -1685,7 +1694,7 @@ def _neg_(self): """ return self.parent().gen(1) - def sqrt(self): + def sqrt(self) -> Self: """ The square root of ``self``. @@ -1731,128 +1740,6 @@ def _gap_init_(self) -> str: InfinityRing = InfinityRing_class() -infinity = InfinityRing.gen(0) -Infinity = infinity -minus_infinity = InfinityRing.gen(1) - - -def check_comparison(ring): - """ - Check comparison with infinity. - - INPUT: - - - ``ring`` -- a sub-ring of the real numbers - - OUTPUT: - - Various attempts are made to generate elements of ``ring``. An - assertion is triggered if one of these elements does not compare - correctly with plus/minus infinity. - - EXAMPLES:: - - sage: from sage.rings.infinity import check_comparison - sage: rings = [ZZ, QQ, RDF] - sage: rings += [RR, RealField(200)] # needs sage.rings.real_mpfr - sage: rings += [RLF, RIF] # needs sage.rings.real_interval_field - sage: for R in rings: - ....: print('testing {}'.format(R)) - ....: check_comparison(R) - testing Integer Ring - testing Rational Field - testing Real Double Field... - sage: check_comparison(AA) # needs sage.rings.number_field - - Comparison with number fields does not work:: - - sage: x = polygen(ZZ, 'x') - sage: K. = NumberField(x^2 - 3) # needs sage.rings.number_field - sage: (-oo < 1 + sqrt3) and (1 + sqrt3 < oo) # known bug # needs sage.rings.number_field - False - - The symbolic ring handles its own infinities, but answers - ``False`` (meaning: cannot decide) already for some very - elementary comparisons:: - - sage: check_comparison(SR) # known bug # needs sage.symbolic - Traceback (most recent call last): - ... - AssertionError: testing -1000.0 in Symbolic Ring: id = ... - """ - - from sage.rings.rational_field import QQ - elements = [-1e3, 99.9999, 0, 1, 100000] - try: - from sage.symbolic.ring import SR - except ImportError: - pass - else: - elements += [-SR(2).sqrt(), SR.pi(), 3 ** (-QQ.one() / 3)] - elements.append(ring.an_element()) - elements.extend(ring.some_elements()) - for z in elements: - try: - z = ring(z) - except (ValueError, TypeError): - continue # ignore if z is not in ring - msg = 'testing {} in {}: id = {}, {}, {}'.format(z, ring, id(z), id(infinity), id(minus_infinity)) - assert minus_infinity < z, msg - assert z > minus_infinity, msg - assert z < infinity, msg - assert infinity > z, msg - assert minus_infinity <= z, msg - assert z >= minus_infinity, msg - assert z <= infinity, msg - assert infinity >= z, msg - - -def check_signed_infinity(pos_inf): - """ - Test consistency of infinity representations. - - There are different possible representations of infinity in - Sage. These are all consistent with the infinity ring, that is, - compare with infinity in the expected way. See also :issue:`14045` - - INPUT: - - - ``pos_inf`` -- a representation of positive infinity - - OUTPUT: - - An assertion error is raised if the representation is not - consistent with the infinity ring. - - Check that :issue:`14045` is fixed:: - - sage: InfinityRing(float('+inf')) - +Infinity - sage: InfinityRing(float('-inf')) - -Infinity - sage: oo > float('+inf') - False - sage: oo == float('+inf') - True - - EXAMPLES:: - - sage: from sage.rings.infinity import check_signed_infinity - sage: check_signed_infinity(oo) - sage: check_signed_infinity(float('+inf')) - sage: check_signed_infinity(RLF(oo)) # needs sage.rings.real_interval_field - sage: check_signed_infinity(RIF(oo)) # needs sage.rings.real_interval_field - sage: check_signed_infinity(SR(oo)) # needs sage.symbolic - """ - msg = f'testing {pos_inf} ({type(pos_inf)})' - assert InfinityRing(pos_inf) is infinity, msg - assert InfinityRing(-pos_inf) is minus_infinity, msg - assert infinity == pos_inf, msg - assert not (infinity > pos_inf), msg - assert not (infinity < pos_inf), msg - assert minus_infinity == -pos_inf, msg - assert not (minus_infinity > -pos_inf), msg - assert not (minus_infinity < -pos_inf), msg - assert pos_inf > -pos_inf, msg - assert infinity > -pos_inf, msg - assert pos_inf > minus_infinity, msg +infinity: PlusInfinity = InfinityRing.gen(0) +Infinity: PlusInfinity = infinity +minus_infinity: MinusInfinity = InfinityRing.gen(1) diff --git a/src/sage/rings/infinity_test.py b/src/sage/rings/infinity_test.py new file mode 100644 index 00000000000..79fb368e2b6 --- /dev/null +++ b/src/sage/rings/infinity_test.py @@ -0,0 +1,114 @@ +import pytest + +from sage.rings.infinity import Infinity, InfinityRing, infinity, minus_infinity +from sage.rings.integer_ring import ZZ +from sage.rings.number_field.number_field import NumberField +from sage.rings.polynomial.polynomial_ring import polygen +from sage.rings.qqbar import AA +from sage.rings.rational_field import QQ +from sage.rings.real_double import RDF +from sage.rings.real_lazy import RLF +from sage.rings.real_mpfi import RIF +from sage.rings.real_mpfr import RR, RealField +from sage.symbolic.ring import SR + + +def generate_elements(ring): + elements = [-1e3, 99.9999, 0, 1, 100000] + elements += [-SR(2).sqrt(), SR.pi(), 3 ** (-QQ.one() / 3)] + elements.append(ring.an_element()) + elements.extend(ring.some_elements()) + + valid_elements = [] + for element in elements: + try: + valid_elements.append(ring(element)) + except (ValueError, TypeError): + continue + return valid_elements + + +@pytest.mark.parametrize( + "element", + [ + element + for ring in [ZZ, QQ, RDF, RR, RealField(200), RLF, RIF, AA] + for element in generate_elements(ring) + ], +) +def test_comparison(element): + """ + Check comparison with infinity. + + Various attempts are made to generate elements of ``ring``. An + assertion is triggered if one of these elements does not compare + correctly with plus/minus infinity. + """ + assert minus_infinity < element + assert element > minus_infinity + assert element < Infinity + assert infinity > element + assert minus_infinity <= element + assert element >= minus_infinity + assert element <= infinity + assert infinity >= element + + +def test_comparison_number_field(): + """ + Comparison with number fields does not work. + This is a known bug. + """ + x = polygen(ZZ, "x") + sqrt3 = NumberField(x**2 - 3, "sqrt3").gen() + element = 1 + sqrt3 + with pytest.raises(TypeError): + assert not (minus_infinity < element < infinity) + + +def test_comparison_symbolic_ring(): + """ + The symbolic ring handles its own infinities, but answers + ``False`` (meaning: cannot decide) already for some very + elementary comparisons. This is a known bug. + """ + element = SR.an_element() + assert not (minus_infinity < element) + assert not (element > minus_infinity) + assert not (element < infinity) + assert not (infinity > element) + assert not (minus_infinity <= element) + assert not (element >= minus_infinity) + assert not (element <= infinity) + assert not (infinity >= element) + + +@pytest.mark.parametrize( + "pos_inf", [infinity, float("+inf"), RLF(infinity), RIF(infinity), SR(infinity)] +) +def test_signed_infinity(pos_inf): + """ + Test consistency of infinity representations. + + There are different possible representations of infinity in + Sage. These are all consistent with the infinity ring, that is, + compare with infinity in the expected way. See also :issue:`14045` + """ + assert InfinityRing(pos_inf) is infinity + assert InfinityRing(-pos_inf) is minus_infinity + assert infinity == pos_inf + assert not (infinity > pos_inf) + assert not (infinity < pos_inf) + assert minus_infinity == -pos_inf + assert not (minus_infinity > -pos_inf) + assert not (minus_infinity < -pos_inf) + assert pos_inf > -pos_inf + assert infinity > -pos_inf + assert pos_inf > minus_infinity + + +def test_issue_14045(): + assert InfinityRing(float("+inf")) is infinity + assert InfinityRing(float("-inf")) is minus_infinity + assert not (infinity > float("+inf")) + assert infinity == float("+inf") diff --git a/src/sage/rings/integer.pyx b/src/sage/rings/integer.pyx index 3ebbd1a2ee7..614a985e73c 100644 --- a/src/sage/rings/integer.pyx +++ b/src/sage/rings/integer.pyx @@ -185,6 +185,8 @@ from sage.structure.richcmp cimport rich_to_bool_sgn from sage.rings import integer_ring +from sage.misc.sage_input import SageInputBuilder, SageInputExpression + cimport gmpy2 gmpy2.import_gmpy2() @@ -6472,7 +6474,7 @@ cdef class Integer(sage.structure.element.EuclideanDomainElement): return 'StringToInteger("%s",16)' % self.str(16) return str(self) - def _sage_input_(self, sib, coerced): + def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression: r""" Produce an expression which will reproduce this value when evaluated. diff --git a/src/sage/rings/integer_ring.pyx b/src/sage/rings/integer_ring.pyx index e6244e3fbc9..d77406b3483 100644 --- a/src/sage/rings/integer_ring.pyx +++ b/src/sage/rings/integer_ring.pyx @@ -65,9 +65,11 @@ from sage.structure.richcmp cimport rich_to_bool from sage.misc.misc_c import prod from sage.misc.randstate cimport randstate, current_randstate, SAGE_RAND_MAX +from sage.misc.sage_input import SageInputBuilder, SageInputExpression from sage.rings.integer cimport Integer + arith = None cdef void late_import() noexcept: # A hack to avoid circular imports. @@ -1525,7 +1527,7 @@ cdef class IntegerRing_class(CommutativeRing): sympy_init() return Integers - def _sage_input_(self, sib, coerced): + def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression: r""" Produce an expression which will reproduce this value when evaluated. diff --git a/src/sage/rings/meson.build b/src/sage/rings/meson.build index ec65cf584b5..165f9301c16 100644 --- a/src/sage/rings/meson.build +++ b/src/sage/rings/meson.build @@ -42,6 +42,7 @@ py.install_sources( 'ideal_monoid.py', 'imaginary_unit.py', 'infinity.py', + 'infinity_test.py', 'integer.pxd', 'integer.pyx', 'integer_fake.h', diff --git a/src/sage/rings/polynomial/polynomial_element.pyx b/src/sage/rings/polynomial/polynomial_element.pyx index d040cc19858..24a01489d71 100644 --- a/src/sage/rings/polynomial/polynomial_element.pyx +++ b/src/sage/rings/polynomial/polynomial_element.pyx @@ -128,6 +128,7 @@ from sage.misc.cachefunc import cached_function from sage.categories.map cimport Map from sage.categories.morphism cimport Morphism +from sage.misc.sage_input import SageInputBuilder, SageInputExpression from sage.misc.superseded import deprecation_cython as deprecation, deprecated_function_alias from sage.misc.cachefunc import cached_method @@ -3298,7 +3299,7 @@ cdef class Polynomial(CommutativePolynomial): return "0" return s[1:].lstrip().rstrip() - def _sage_input_(self, sib, coerced): + def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression: r""" Produce an expression which will reproduce this value when evaluated. diff --git a/src/sage/rings/polynomial/polynomial_ring.py b/src/sage/rings/polynomial/polynomial_ring.py index 0da5acc6908..764b267f41e 100644 --- a/src/sage/rings/polynomial/polynomial_ring.py +++ b/src/sage/rings/polynomial/polynomial_ring.py @@ -137,9 +137,10 @@ # (at your option) any later version. # https://www.gnu.org/licenses/ # **************************************************************************** - +from __future__ import annotations import sys +from typing import TYPE_CHECKING, Literal from sage.misc.superseded import deprecation from sage.structure.element import Element @@ -180,6 +181,9 @@ import sage.interfaces.abc +if TYPE_CHECKING: + from sage.misc.sage_input import SageInputBuilder, SageInputExpression + def is_PolynomialRing(x): """ @@ -968,7 +972,7 @@ def _gap_init_(self) -> str: base_ring = self.base_ring()._gap_init_() return 'PolynomialRing(%s, ["%s"])' % (base_ring, self.variable_name()) - def _sage_input_(self, sib, coerced): + def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression: r""" Produce an expression which will reproduce this value when evaluated. diff --git a/src/sage/rings/qqbar.py b/src/sage/rings/qqbar.py index 996cea78a4f..cadacea0e92 100644 --- a/src/sage/rings/qqbar.py +++ b/src/sage/rings/qqbar.py @@ -579,9 +579,11 @@ - Carl Witty (2007-01-27): initial version - Carl Witty (2007-10-29): massive rewrite to support complex as well as real numbers """ +from __future__ import annotations import itertools import operator +from typing import TYPE_CHECKING, Literal import sage.rings.abc import sage.rings.number_field.number_field_base @@ -629,6 +631,9 @@ ) from sage.structure.sage_object import SageObject +if TYPE_CHECKING: + from sage.misc.sage_input import SageInputBuilder, SageInputExpression + class AlgebraicField_common(sage.rings.abc.AlgebraicField_common): r""" @@ -1200,7 +1205,7 @@ def _latex_(self): """ return "\\mathbf{A}" - def _sage_input_(self, sib, coerce): + def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression: r""" Produce an expression which will reproduce this value when evaluated. @@ -1686,7 +1691,7 @@ def _latex_(self): """ return "\\overline{\\QQ}" - def _sage_input_(self, sib, coerce): + def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression: r""" Produce an expression which will reproduce this value when evaluated. @@ -3870,7 +3875,7 @@ def _latex_(self): return latex(radical) return repr(self).replace('*I', r' \sqrt{-1}') - def _sage_input_(self, sib, coerce): + def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression: r""" Produce an expression which will reproduce this value when evaluated. @@ -3918,7 +3923,7 @@ def _sage_input_(self, sib, coerce): {call: {getattr: {atomic:QQbar}.polynomial_root}({call: {getattr: {atomic:AA}.common_polynomial}({binop:- {binop:** {gen:x {constr_parent: {subscr: {atomic:QQbar}[{atomic:'x'}]} with gens: ('x',)}} {atomic:2}} {atomic:7}})}, {call: {atomic:CIF}({call: {atomic:RIF}({call: {atomic:RR}({atomic:2.6457513110645903})}, {call: {atomic:RR}({atomic:2.6457513110645907})})}, {call: {atomic:RIF}({call: {atomic:RR}({atomic:0})})})})} """ (v, complicated) = \ - self._descr.handle_sage_input(sib, coerce, self.parent() is QQbar) + self._descr.handle_sage_input(sib, coerced, self.parent() is QQbar) if complicated or True: sib.id_cache(self, v, 'v') return v @@ -6933,7 +6938,7 @@ def __reduce__(self): """ return (AlgebraicPolynomialTracker, (self._poly, )) - def _sage_input_(self, sib, coerce): + def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression: r""" Produce an expression which will reproduce this value when evaluated. diff --git a/src/sage/rings/rational.pyx b/src/sage/rings/rational.pyx index 105a4c69207..59e07f1a36e 100644 --- a/src/sage/rings/rational.pyx +++ b/src/sage/rings/rational.pyx @@ -77,7 +77,7 @@ from sage.structure.coerce cimport coercion_model, is_numpy_type from sage.structure.element cimport Element from sage.structure.parent cimport Parent from sage.structure.richcmp cimport rich_to_bool_sgn - +from sage.misc.sage_input import SageInputBuilder, SageInputExpression RealNumber_classes = () @@ -3853,7 +3853,7 @@ cdef class Rational(sage.structure.element.FieldElement): """ return '%s/%s' % (self.numerator(), self.denominator()) - def _sage_input_(self, sib, coerced): + def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression: r""" Produce an expression which will reproduce this value when evaluated. diff --git a/src/sage/rings/rational_field.py b/src/sage/rings/rational_field.py index 582862f120a..7b87f454e40 100644 --- a/src/sage/rings/rational_field.py +++ b/src/sage/rings/rational_field.py @@ -52,7 +52,9 @@ - Anna Haensch (2018-03): Added function ``quadratic_defect()`` """ +from __future__ import annotations +from typing import TYPE_CHECKING, Literal from sage.rings.integer import Integer from sage.rings.rational import Rational @@ -64,6 +66,9 @@ from sage.structure.parent import Parent from sage.structure.sequence import Sequence +if TYPE_CHECKING: + from sage.misc.sage_input import SageInputBuilder, SageInputExpression + class RationalField(Singleton, number_field_base.NumberField): r""" @@ -1588,7 +1593,7 @@ def _sympy_(self): sympy_init() return Rationals - def _sage_input_(self, sib, coerced): + def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression: r""" Produce an expression which will reproduce this value when evaluated. diff --git a/src/sage/rings/real_double.pyx b/src/sage/rings/real_double.pyx index 543802dbba0..d04d542f22e 100644 --- a/src/sage/rings/real_double.pyx +++ b/src/sage/rings/real_double.pyx @@ -63,6 +63,7 @@ from sage.rings.integer_ring import ZZ from sage.categories.morphism cimport Morphism from sage.structure.coerce cimport is_numpy_type from sage.misc.randstate cimport randstate, current_randstate +from sage.misc.sage_input import SageInputBuilder, SageInputExpression from sage.structure.richcmp cimport rich_to_bool from sage.arith.constants cimport * @@ -175,7 +176,7 @@ cdef class RealDoubleField_class(sage.rings.abc.RealDoubleField): """ return "\\Bold{R}" - def _sage_input_(self, sib, coerced): + def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression: r""" Produce an expression which will reproduce this value when evaluated. @@ -984,7 +985,7 @@ cdef class RealDoubleElement(FieldElement): from sage.rings.real_mpfr import RR return RR(self._value)._mathematica_init_() - def _sage_input_(self, sib, coerced): + def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression: r""" Produce an expression which will reproduce this value when evaluated. diff --git a/src/sage/rings/real_mpfi.pyx b/src/sage/rings/real_mpfi.pyx index 4d8dafa0515..56bd5351ed0 100644 --- a/src/sage/rings/real_mpfi.pyx +++ b/src/sage/rings/real_mpfi.pyx @@ -308,6 +308,7 @@ import operator from sage.cpython.string cimport char_to_str, bytes_to_str from sage.misc.superseded import deprecation +from sage.misc.sage_input import SageInputBuilder, SageInputExpression import sage.rings.infinity # **************************************************************************** @@ -655,7 +656,7 @@ cdef class RealIntervalField_class(sage.rings.abc.RealIntervalField): """ return "\\Bold{I} \\Bold{R}" - def _sage_input_(self, sib, coerce): + def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression: r""" Produce an expression which will reproduce this value when evaluated. @@ -1392,7 +1393,7 @@ cdef class RealIntervalFieldElement(RingElement): """ raise TypeError - def _sage_input_(self, sib, coerce): + def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression: r""" Produce an expression which will reproduce this value when evaluated. diff --git a/src/sage/rings/real_mpfr.pyx b/src/sage/rings/real_mpfr.pyx index 10ff6b8baae..f7fecaf64a9 100644 --- a/src/sage/rings/real_mpfr.pyx +++ b/src/sage/rings/real_mpfr.pyx @@ -142,6 +142,7 @@ from sage.libs.gmp.pylong cimport mpz_set_pylong from sage.libs.mpfr cimport * from sage.libs.mpmath.utils cimport mpfr_to_mpfval from sage.misc.randstate cimport randstate, current_randstate +from sage.misc.sage_input import SageInputBuilder, SageInputExpression from sage.structure.element cimport Element from sage.structure.parent cimport Parent @@ -595,7 +596,7 @@ cdef class RealField_class(sage.rings.abc.RealField): """ return "\\Bold{R}" - def _sage_input_(self, sib, coerce): + def _sage_input_(self, sib: SageInputBuilder, coerced: bool) -> SageInputExpression: r""" Produce an expression which will reproduce this value when evaluated. @@ -1685,7 +1686,7 @@ cdef class RealNumber(sage.structure.element.RingElement): """ return self.str(10, e='*^') - def _sage_input_(self, sib, coerced): + def _sage_input_(self, sib: SageInputBuilder, coerced: bool) -> SageInputExpression: r""" Produce an expression which will reproduce this value when evaluated. diff --git a/src/sage/sets/real_set.py b/src/sage/sets/real_set.py index 966ddceac71..2d36edf29a7 100644 --- a/src/sage/sets/real_set.py +++ b/src/sage/sets/real_set.py @@ -96,19 +96,25 @@ class RealSet. # (at your option) any later version. # https://www.gnu.org/licenses/ # **************************************************************************** +from __future__ import annotations + from heapq import merge +from typing import TYPE_CHECKING, Literal from sage.categories.sets_cat import EmptySetError from sage.categories.topological_spaces import TopologicalSpaces from sage.rings.infinity import infinity, minus_infinity from sage.rings.integer_ring import ZZ -from sage.rings.real_lazy import LazyFieldElement, RLF -from sage.sets.set import Set_base, Set_boolean_operators, Set_add_sub_operators +from sage.rings.real_lazy import RLF, LazyFieldElement +from sage.sets.set import Set_add_sub_operators, Set_base, Set_boolean_operators from sage.structure.parent import Parent from sage.structure.richcmp import richcmp, richcmp_method from sage.structure.unique_representation import UniqueRepresentation from sage.symbolic.ring import SR +if TYPE_CHECKING: + from sage.misc.sage_input import SageInputBuilder, SageInputExpression + @richcmp_method class InternalRealInterval(UniqueRepresentation, Parent): @@ -457,6 +463,7 @@ def _sympy_(self): Interval.open(0, oo) """ from sympy import Interval + from sage.interfaces.sympy import sympy_init sympy_init() return Interval(self.lower(), self.upper(), @@ -1210,7 +1217,7 @@ def __classcall__(cls, *args, **kwds): elif isinstance(arg, RealSet): intervals.extend(arg._intervals) elif isinstance(arg, Expression) and arg.is_relational(): - from operator import eq, ne, lt, gt, le, ge + from operator import eq, ge, gt, le, lt, ne def rel_to_interval(op, val): """ @@ -1255,7 +1262,9 @@ def rel_to_interval(op, val): else: raise ValueError(str(arg) + ' does not determine real interval') else: - from sage.manifolds.differentiable.examples.real_line import OpenInterval + from sage.manifolds.differentiable.examples.real_line import ( + OpenInterval, + ) from sage.manifolds.subsets.closure import ManifoldSubsetClosure if isinstance(arg, OpenInterval): lower, upper = RealSet._prep(arg.lower_bound(), arg.upper_bound()) @@ -2678,8 +2687,8 @@ def simplest_rational(self): if self.is_empty(): raise EmptySetError - from sage.rings.real_mpfi import RealIntervalField from sage.rings.rational_field import QQ + from sage.rings.real_mpfi import RealIntervalField RIF = RealIntervalField() candidates = [] @@ -2708,7 +2717,7 @@ def simplest_rational(self): # positive value preferred over negative return min(candidates, key=lambda x: (x.denominator(), x.abs(), -x)) - def _sage_input_(self, sib, coerced): + def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression: """ Produce an expression which will reproduce this value when evaluated. @@ -2748,11 +2757,10 @@ def interval_input(i): t = 'RealSet.closed' else: t = 'RealSet.closed_open' + elif i.upper_closed(): + t = 'RealSet.open_closed' else: - if i.upper_closed(): - t = 'RealSet.open_closed' - else: - t = 'RealSet.open' + t = 'RealSet.open' return sib.name(t)(sib(lower), sib(upper)) if self.is_empty(): @@ -2822,6 +2830,7 @@ def _sympy_(self): False """ from sympy import Reals, Union + from sage.interfaces.sympy import sympy_init sympy_init() if self.is_universe():