From 62b5191a07978303de47e5cd3f888e0447420def Mon Sep 17 00:00:00 2001 From: Tobias Diez Date: Sun, 28 Sep 2025 11:37:58 +0800 Subject: [PATCH 1/2] Add typing info for _sage_input_ --- src/sage/geometry/cone.py | 7 +++- src/sage/geometry/fan.py | 7 +++- src/sage/geometry/lattice_polytope.py | 27 ++++++------- src/sage/geometry/point_collection.pyx | 4 +- src/sage/geometry/polyhedron/base0.py | 7 +++- src/sage/geometry/toric_lattice.py | 7 +++- src/sage/matrix/matrix1.pyx | 3 +- src/sage/misc/explain_pickle.py | 38 +++++++++---------- src/sage/misc/sage_input.py | 21 +++++----- src/sage/modules/free_module_element.pyx | 3 +- src/sage/rings/complex_interval.pyx | 3 +- src/sage/rings/complex_interval_field.py | 8 ++-- src/sage/rings/complex_mpfr.pyx | 5 ++- .../rings/finite_rings/finite_field_base.pyx | 3 +- src/sage/rings/finite_rings/integer_mod.pyx | 3 +- src/sage/rings/integer.pyx | 4 +- src/sage/rings/integer_ring.pyx | 4 +- .../rings/polynomial/polynomial_element.pyx | 3 +- src/sage/rings/polynomial/polynomial_ring.py | 8 ++-- src/sage/rings/qqbar.py | 15 +++++--- src/sage/rings/rational.pyx | 4 +- src/sage/rings/rational_field.py | 7 +++- src/sage/rings/real_double.pyx | 5 ++- src/sage/rings/real_mpfi.pyx | 5 ++- src/sage/rings/real_mpfr.pyx | 5 ++- src/sage/sets/real_set.py | 25 +++++++----- 26 files changed, 138 insertions(+), 93 deletions(-) diff --git a/src/sage/geometry/cone.py b/src/sage/geometry/cone.py index d1b61d41eb3..fe947945ff5 100644 --- a/src/sage/geometry/cone.py +++ b/src/sage/geometry/cone.py @@ -201,9 +201,9 @@ # (at your option) any later version. # https://www.gnu.org/licenses/ # **************************************************************************** - 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 +241,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 is_Cone(x): r""" @@ -1500,7 +1503,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 7af1fa122ee..cf215743f1e 100644 --- a/src/sage/geometry/fan.py +++ b/src/sage/geometry/fan.py @@ -233,9 +233,9 @@ # (at your option) any later version. # https://www.gnu.org/licenses/ # **************************************************************************** - 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 +262,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 is_Fan(x) -> bool: r""" @@ -1217,7 +1220,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 5b4a6ec5847..d4c315ea934 100644 --- a/src/sage/geometry/lattice_polytope.py +++ b/src/sage/geometry/lattice_polytope.py @@ -120,24 +120,24 @@ # (at your option) any later version. # https://www.gnu.org/licenses/ # **************************************************************************** +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 TYPE_CHECKING, Literal +import sage.geometry.abc from sage.arith.misc import GCD as gcd 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 @@ -149,11 +149,9 @@ from sage.rings.rational_field import QQ from sage.sets.set import Set_generic from sage.structure.element import Matrix -from sage.structure.richcmp import richcmp_method, richcmp +from sage.structure.richcmp import richcmp, richcmp_method from sage.structure.sage_object import SageObject from sage.structure.sequence import Sequence -import sage.geometry.abc - lazy_import("sage.combinat.posets.posets", 'FinitePoset') lazy_import("sage.geometry.hasse_diagram", 'lattice_from_incidences') @@ -167,6 +165,9 @@ lazy_import('ppl', 'point', as_='PPL_point', feature=PythonModule("ppl", spkg='pplpy', type='standard')) +if TYPE_CHECKING: + from sage.misc.sage_input import SageInputBuilder, SageInputExpression + class SetOfAllLatticePolytopesClass(Set_generic): def _repr_(self) -> str: @@ -570,7 +571,7 @@ def __init__(self, points=None, compute_vertices=None, self._ambient_facet_indices = tuple(ambient_facet_indices) self._vertices = ambient.vertices(self._ambient_vertex_indices) - def _sage_input_(self, sib, coerced): + def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression: """ Return Sage command to reconstruct ``self``. @@ -4443,7 +4444,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 c710c5dfa62..fdbb7874b7a 100644 --- a/src/sage/geometry/polyhedron/base0.py +++ b/src/sage/geometry/polyhedron/base0.py @@ -29,12 +29,15 @@ # (at your option) any later version. # https://www.gnu.org/licenses/ # **************************************************************************** - +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 +299,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 53346206c05..223dc646ecf 100644 --- a/src/sage/geometry/toric_lattice.py +++ b/src/sage/geometry/toric_lattice.py @@ -144,7 +144,7 @@ # # https://www.gnu.org/licenses/ # **************************************************************************** - +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 +162,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 + def is_ToricLattice(x): r""" @@ -897,7 +900,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..c6de3ec22b9 100644 --- a/src/sage/misc/explain_pickle.py +++ b/src/sage/misc/explain_pickle.py @@ -152,23 +152,24 @@ # (at your option) any later version. # http://www.gnu.org/licenses/ # ***************************************************************************** - - +import bz2 as comp_other import pickletools import re import sys import types - import zlib as comp -import bz2 as comp_other - from pickletools import genops - -from sage.misc.sage_input import SageInputBuilder, SageInputExpression +from typing import Literal + +from sage.misc.persist import ( + SageUnpickler, + dumps, + register_unpickle_override, + unpickle_global, + unpickle_override, +) from sage.misc.sage_eval import sage_eval -from sage.misc.persist import (unpickle_override, unpickle_global, dumps, - register_unpickle_override, SageUnpickler) - +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 +359,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. @@ -845,14 +846,13 @@ def _APPENDS_helper(self, lst, slice): else: for s in slice: self.sib.command(lst, self.sib.name('list').append(lst, self.sib(s))) + elif self.pedantic: + app = self.sib(lst).append + for s in slice: + self.sib.command(lst, app(self.sib(s))) else: - if self.pedantic: - app = self.sib(lst).append - for s in slice: - self.sib.command(lst, app(self.sib(s))) - else: - for s in slice: - self.sib.command(lst, self.sib(lst).append(self.sib(s))) + for s in slice: + self.sib.command(lst, self.sib(lst).append(self.sib(s))) else: self.sib.command(lst, self.sib.name('unpickle_appends')(self.sib(lst), slice_exp)) self.push(lst) @@ -2509,7 +2509,7 @@ def unpickle_extension(code): sage: remove_extension('sage.misc.explain_pickle', 'EmptyNewstyleClass', 42) """ - from copyreg import _inverted_registry, _extension_cache + from copyreg import _extension_cache, _inverted_registry # copied from .get_extension() in pickle.py nil = [] obj = _extension_cache.get(code, nil) diff --git a/src/sage/misc/sage_input.py b/src/sage/misc/sage_input.py index 20a382f8829..6c2a72e7052 100644 --- a/src/sage/misc/sage_input.py +++ b/src/sage/misc/sage_input.py @@ -172,6 +172,7 @@ # (at your option) any later version. # https://www.gnu.org/licenses/ # **************************************************************************** +from typing import Literal from sage.misc.lazy_import import lazy_import @@ -343,7 +344,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). @@ -477,11 +478,10 @@ def __call__(self, x, coerced=False): return SIE_literal_stringrep(self, str(x) + 'r') elif self._preparse is False: return self.int(x) + elif x < 0: + return -self.name('int')(self.int(-x)) else: - if x < 0: - return -self.name('int')(self.int(-x)) - else: - return self.name('int')(self.int(x)) + return self.name('int')(self.int(x)) if isinstance(x, float): # floats could often have prettier output, @@ -499,8 +499,8 @@ def __call__(self, x, coerced=False): return -SIE_literal_stringrep(self, str(-x)) else: return SIE_literal_stringrep(self, str(x)) - from sage.rings.real_mpfr import RR from sage.rings.integer_ring import ZZ + from sage.rings.real_mpfr import RR rrx = RR(x) if rrx in ZZ and abs(rrx) < (1 << 53): return self.name('float')(self.int(ZZ(rrx))) @@ -593,7 +593,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. @@ -2228,11 +2228,10 @@ def _sie_format(self, sif): values = [sif.format(val, 0) for val in self._sie_values] if self._sie_is_list: return '[%s]' % ', '.join(values), _prec_atomic + elif len(values) == 1: + return '(%s,)' % values[0], _prec_atomic else: - if len(values) == 1: - return '(%s,)' % values[0], _prec_atomic - else: - return '(%s)' % ', '.join(values), _prec_atomic + return '(%s)' % ', '.join(values), _prec_atomic class SIE_dict(SageInputExpression): diff --git a/src/sage/modules/free_module_element.pyx b/src/sage/modules/free_module_element.pyx index b16e66f1289..62460e7aad8 100644 --- a/src/sage/modules/free_module_element.pyx +++ b/src/sage/modules/free_module_element.pyx @@ -128,6 +128,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) @@ -1258,7 +1259,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 b0b9f14263b..bdda23de169 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..d98b0c333cf 100644 --- a/src/sage/rings/complex_interval_field.py +++ b/src/sage/rings/complex_interval_field.py @@ -34,8 +34,7 @@ # (at your option) any later version. # https://www.gnu.org/licenses/ # **************************************************************************** - - +from typing import TYPE_CHECKING, Literal import weakref import sage.rings.abc @@ -48,6 +47,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 +297,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 3eed6a7e4b8..87ad6bcd430 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 @@ -263,7 +264,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 8fbcea08502..95514577a88 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/integer.pyx b/src/sage/rings/integer.pyx index 5fd948c2a83..38a6758b667 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() @@ -6396,7 +6398,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/polynomial/polynomial_element.pyx b/src/sage/rings/polynomial/polynomial_element.pyx index 1a2e1257cb6..f6508b470ec 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 08c5a47c252..dc97089cf7e 100644 --- a/src/sage/rings/polynomial/polynomial_ring.py +++ b/src/sage/rings/polynomial/polynomial_ring.py @@ -137,9 +137,8 @@ # (at your option) any later version. # https://www.gnu.org/licenses/ # **************************************************************************** - - import sys +from typing import TYPE_CHECKING, Literal from sage.misc.superseded import deprecation from sage.structure.element import Element @@ -180,6 +179,9 @@ import sage.interfaces.abc +if TYPE_CHECKING: + from sage.misc.sage_input import SageInputBuilder, SageInputExpression + def is_PolynomialRing(x): """ @@ -968,7 +970,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 6e0aadb56f8..7be97c4ddd2 100644 --- a/src/sage/rings/qqbar.py +++ b/src/sage/rings/qqbar.py @@ -579,9 +579,9 @@ - Carl Witty (2007-01-27): initial version - Carl Witty (2007-10-29): massive rewrite to support complex as well as real numbers """ - import itertools import operator +from typing import TYPE_CHECKING, Literal import sage.rings.abc import sage.rings.number_field.number_field_base @@ -629,6 +629,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 +1203,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. @@ -1708,7 +1711,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. @@ -3915,7 +3918,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. @@ -3963,7 +3966,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 @@ -6978,7 +6981,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 02fe3b23af0..a13e0bf716f 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 = () @@ -3845,7 +3845,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 c8c84901607..4a88dd28b09 100644 --- a/src/sage/rings/rational_field.py +++ b/src/sage/rings/rational_field.py @@ -52,7 +52,7 @@ - Anna Haensch (2018-03): Added function ``quadratic_defect()`` """ - +from typing import TYPE_CHECKING, Literal from sage.rings.integer import Integer from sage.rings.rational import Rational @@ -64,6 +64,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""" @@ -1594,7 +1597,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 cac66b4ac75..8f2379389eb 100644 --- a/src/sage/rings/real_double.pyx +++ b/src/sage/rings/real_double.pyx @@ -60,6 +60,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 * @@ -172,7 +173,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. @@ -981,7 +982,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 7f909c69655..5b6a5f95196 100644 --- a/src/sage/sets/real_set.py +++ b/src/sage/sets/real_set.py @@ -97,17 +97,21 @@ class RealSet. # https://www.gnu.org/licenses/ # **************************************************************************** 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 +if TYPE_CHECKING: + from sage.misc.sage_input import SageInputBuilder, SageInputExpression + @richcmp_method class InternalRealInterval(UniqueRepresentation, Parent): @@ -456,6 +460,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(), @@ -1209,7 +1214,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): """ @@ -1258,7 +1263,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()) @@ -2628,7 +2635,7 @@ def are_pairwise_disjoint(*real_set_collection): overlap_generator = RealSet._scan_to_intervals(scan, lambda i: i > 1) return next(overlap_generator, None) is None - 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. @@ -2668,11 +2675,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(): @@ -2742,6 +2748,7 @@ def _sympy_(self): False """ from sympy import Reals, Union + from sage.interfaces.sympy import sympy_init sympy_init() if self.is_universe(): From c76581b84472e50ed2bafac1cbfa8fa57739b095 Mon Sep 17 00:00:00 2001 From: Tobias Diez Date: Sat, 4 Oct 2025 22:45:34 +0800 Subject: [PATCH 2/2] Add future annotations import --- src/sage/geometry/cone.py | 2 ++ src/sage/geometry/fan.py | 2 ++ src/sage/geometry/lattice_polytope.py | 2 ++ src/sage/geometry/polyhedron/base0.py | 2 ++ src/sage/geometry/toric_lattice.py | 2 ++ src/sage/misc/explain_pickle.py | 2 ++ src/sage/misc/sage_input.py | 2 ++ src/sage/rings/complex_interval_field.py | 2 ++ src/sage/rings/polynomial/polynomial_ring.py | 2 ++ src/sage/rings/qqbar.py | 2 ++ src/sage/rings/rational_field.py | 2 ++ src/sage/sets/real_set.py | 2 ++ 12 files changed, 24 insertions(+) diff --git a/src/sage/geometry/cone.py b/src/sage/geometry/cone.py index fe947945ff5..c276722d4aa 100644 --- a/src/sage/geometry/cone.py +++ b/src/sage/geometry/cone.py @@ -201,6 +201,8 @@ # (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 diff --git a/src/sage/geometry/fan.py b/src/sage/geometry/fan.py index cf215743f1e..44be8f765e1 100644 --- a/src/sage/geometry/fan.py +++ b/src/sage/geometry/fan.py @@ -233,6 +233,8 @@ # (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 diff --git a/src/sage/geometry/lattice_polytope.py b/src/sage/geometry/lattice_polytope.py index d4c315ea934..6fdfd27530f 100644 --- a/src/sage/geometry/lattice_polytope.py +++ b/src/sage/geometry/lattice_polytope.py @@ -120,6 +120,8 @@ # (at your option) any later version. # https://www.gnu.org/licenses/ # **************************************************************************** +from __future__ import annotations + import os import shlex from collections.abc import Hashable diff --git a/src/sage/geometry/polyhedron/base0.py b/src/sage/geometry/polyhedron/base0.py index fdbb7874b7a..eb93ddb0334 100644 --- a/src/sage/geometry/polyhedron/base0.py +++ b/src/sage/geometry/polyhedron/base0.py @@ -29,6 +29,8 @@ # (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 diff --git a/src/sage/geometry/toric_lattice.py b/src/sage/geometry/toric_lattice.py index 223dc646ecf..8835c2a1f13 100644 --- a/src/sage/geometry/toric_lattice.py +++ b/src/sage/geometry/toric_lattice.py @@ -144,6 +144,8 @@ # # 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 diff --git a/src/sage/misc/explain_pickle.py b/src/sage/misc/explain_pickle.py index c6de3ec22b9..9c9604e7b28 100644 --- a/src/sage/misc/explain_pickle.py +++ b/src/sage/misc/explain_pickle.py @@ -152,6 +152,8 @@ # (at your option) any later version. # http://www.gnu.org/licenses/ # ***************************************************************************** +from __future__ import annotations + import bz2 as comp_other import pickletools import re diff --git a/src/sage/misc/sage_input.py b/src/sage/misc/sage_input.py index 6c2a72e7052..8d8d5d24533 100644 --- a/src/sage/misc/sage_input.py +++ b/src/sage/misc/sage_input.py @@ -172,6 +172,8 @@ # (at your option) any later version. # https://www.gnu.org/licenses/ # **************************************************************************** +from __future__ import annotations + from typing import Literal from sage.misc.lazy_import import lazy_import diff --git a/src/sage/rings/complex_interval_field.py b/src/sage/rings/complex_interval_field.py index d98b0c333cf..193469cbddc 100644 --- a/src/sage/rings/complex_interval_field.py +++ b/src/sage/rings/complex_interval_field.py @@ -34,6 +34,8 @@ # (at your option) any later version. # https://www.gnu.org/licenses/ # **************************************************************************** +from __future__ import annotations + from typing import TYPE_CHECKING, Literal import weakref diff --git a/src/sage/rings/polynomial/polynomial_ring.py b/src/sage/rings/polynomial/polynomial_ring.py index dc97089cf7e..c45849c61d2 100644 --- a/src/sage/rings/polynomial/polynomial_ring.py +++ b/src/sage/rings/polynomial/polynomial_ring.py @@ -137,6 +137,8 @@ # (at your option) any later version. # https://www.gnu.org/licenses/ # **************************************************************************** +from __future__ import annotations + import sys from typing import TYPE_CHECKING, Literal diff --git a/src/sage/rings/qqbar.py b/src/sage/rings/qqbar.py index 7be97c4ddd2..144a6d531e5 100644 --- a/src/sage/rings/qqbar.py +++ b/src/sage/rings/qqbar.py @@ -579,6 +579,8 @@ - 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 diff --git a/src/sage/rings/rational_field.py b/src/sage/rings/rational_field.py index 4a88dd28b09..2bff8d61bf4 100644 --- a/src/sage/rings/rational_field.py +++ b/src/sage/rings/rational_field.py @@ -52,6 +52,8 @@ - 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 diff --git a/src/sage/sets/real_set.py b/src/sage/sets/real_set.py index 5b6a5f95196..dd8226a4094 100644 --- a/src/sage/sets/real_set.py +++ b/src/sage/sets/real_set.py @@ -96,6 +96,8 @@ 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