Skip to content

Commit 62b5191

Browse files
committed
Add typing info for _sage_input_
1 parent f4adc25 commit 62b5191

26 files changed

+138
-93
lines changed

src/sage/geometry/cone.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,9 @@
201201
# (at your option) any later version.
202202
# https://www.gnu.org/licenses/
203203
# ****************************************************************************
204-
205204
from collections.abc import Hashable, Iterable, Container
206205
from copy import copy
206+
from typing import TYPE_CHECKING, Literal
207207
from warnings import warn
208208

209209
from sage.misc.lazy_import import lazy_import
@@ -241,6 +241,9 @@
241241
lazy_import('ppl', ['ray', 'point'], as_=['PPL_ray', 'PPL_point'],
242242
feature=PythonModule("ppl", spkg='pplpy', type='standard'))
243243

244+
if TYPE_CHECKING:
245+
from sage.misc.sage_input import SageInputBuilder, SageInputExpression
246+
244247

245248
def is_Cone(x):
246249
r"""
@@ -1500,7 +1503,7 @@ def __init__(self, rays=None, lattice=None,
15001503
if PPL is not None:
15011504
self._PPL_C_Polyhedron = PPL
15021505

1503-
def _sage_input_(self, sib, coerced):
1506+
def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression:
15041507
"""
15051508
Return Sage command to reconstruct ``self``.
15061509

src/sage/geometry/fan.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,9 @@
233233
# (at your option) any later version.
234234
# https://www.gnu.org/licenses/
235235
# ****************************************************************************
236-
237236
from collections.abc import Callable, Container
238237
from copy import copy
238+
from typing import TYPE_CHECKING, Literal
239239
from warnings import warn
240240

241241
import sage.geometry.abc
@@ -262,6 +262,9 @@
262262
from sage.rings.integer_ring import ZZ
263263
from sage.rings.rational_field import QQ
264264

265+
if TYPE_CHECKING:
266+
from sage.misc.sage_input import SageInputBuilder, SageInputExpression
267+
265268

266269
def is_Fan(x) -> bool:
267270
r"""
@@ -1217,7 +1220,7 @@ def __init__(self, cones, rays, lattice,
12171220
if virtual_rays is not None:
12181221
self._virtual_rays = PointCollection(virtual_rays, self.lattice())
12191222

1220-
def _sage_input_(self, sib, coerced):
1223+
def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression:
12211224
"""
12221225
Return Sage command to reconstruct ``self``.
12231226

src/sage/geometry/lattice_polytope.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -120,24 +120,24 @@
120120
# (at your option) any later version.
121121
# https://www.gnu.org/licenses/
122122
# ****************************************************************************
123+
import os
124+
import shlex
123125
from collections.abc import Hashable
124126
from copyreg import constructor as copyreg_constructor
125127
from functools import reduce
126128
from io import IOBase, StringIO
127-
from subprocess import Popen, PIPE
128-
from warnings import warn
129-
import os
130-
import shlex
129+
from subprocess import PIPE, Popen
130+
from typing import TYPE_CHECKING, Literal
131131

132+
import sage.geometry.abc
132133
from sage.arith.misc import GCD as gcd
133134
from sage.features import PythonModule
134-
from sage.features.palp import PalpExecutable
135135
from sage.features.databases import DatabaseReflexivePolytopes
136+
from sage.features.palp import PalpExecutable
136137
from sage.geometry.cone import _ambient_space_point, integral_length
137-
from sage.geometry.point_collection import (PointCollection,
138-
read_palp_point_collection)
139-
from sage.geometry.toric_lattice import ToricLattice, ToricLattice_generic
140138
from sage.geometry.convex_set import ConvexSet_compact
139+
from sage.geometry.point_collection import PointCollection, read_palp_point_collection
140+
from sage.geometry.toric_lattice import ToricLattice, ToricLattice_generic
141141
from sage.matrix.constructor import matrix
142142
from sage.misc.cachefunc import cached_method
143143
from sage.misc.flatten import flatten
@@ -149,11 +149,9 @@
149149
from sage.rings.rational_field import QQ
150150
from sage.sets.set import Set_generic
151151
from sage.structure.element import Matrix
152-
from sage.structure.richcmp import richcmp_method, richcmp
152+
from sage.structure.richcmp import richcmp, richcmp_method
153153
from sage.structure.sage_object import SageObject
154154
from sage.structure.sequence import Sequence
155-
import sage.geometry.abc
156-
157155

158156
lazy_import("sage.combinat.posets.posets", 'FinitePoset')
159157
lazy_import("sage.geometry.hasse_diagram", 'lattice_from_incidences')
@@ -167,6 +165,9 @@
167165
lazy_import('ppl', 'point', as_='PPL_point',
168166
feature=PythonModule("ppl", spkg='pplpy', type='standard'))
169167

168+
if TYPE_CHECKING:
169+
from sage.misc.sage_input import SageInputBuilder, SageInputExpression
170+
170171

171172
class SetOfAllLatticePolytopesClass(Set_generic):
172173
def _repr_(self) -> str:
@@ -570,7 +571,7 @@ def __init__(self, points=None, compute_vertices=None,
570571
self._ambient_facet_indices = tuple(ambient_facet_indices)
571572
self._vertices = ambient.vertices(self._ambient_vertex_indices)
572573

573-
def _sage_input_(self, sib, coerced):
574+
def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression:
574575
"""
575576
Return Sage command to reconstruct ``self``.
576577
@@ -4443,7 +4444,7 @@ def _repr_(self):
44434444
pass
44444445
return result
44454446

4446-
def _sage_input_(self, sib, coerced):
4447+
def _sage_input_(self, sib: SageInputBuilder, coerced: bool) -> SageInputExpression:
44474448
"""
44484449
Return Sage command to reconstruct ``self``.
44494450

src/sage/geometry/point_collection.pyx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ from sage.geometry.toric_lattice import ToricLattice
8383
from sage.matrix.constructor import matrix
8484
from sage.misc.latex import latex
8585

86+
from sage.misc.sage_input import SageInputBuilder, SageInputExpression
87+
8688

8789
def is_PointCollection(x):
8890
r"""
@@ -172,7 +174,7 @@ cdef class PointCollection(SageObject):
172174
self._points = tuple(points)
173175
self._module = self._points[0].parent() if module is None else module
174176

175-
def _sage_input_(self, sib, coerced):
177+
def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression:
176178
r"""
177179
Return Sage command to reconstruct ``self``.
178180

src/sage/geometry/polyhedron/base0.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@
2929
# (at your option) any later version.
3030
# https://www.gnu.org/licenses/
3131
# ****************************************************************************
32-
32+
from typing import TYPE_CHECKING, Literal
3333
from sage.misc.cachefunc import cached_method
3434
from sage.misc.abstract_method import abstract_method
3535
from sage.structure.element import Element
3636
import sage.geometry.abc
3737

38+
if TYPE_CHECKING:
39+
from sage.misc.sage_input import SageInputBuilder, SageInputExpression
40+
3841

3942
class Polyhedron_base0(Element, sage.geometry.abc.Polyhedron):
4043
"""
@@ -296,7 +299,7 @@ def _delete(self):
296299
"""
297300
self.parent().recycle(self)
298301

299-
def _sage_input_(self, sib, coerced):
302+
def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression:
300303
"""
301304
Return Sage command to reconstruct ``self``.
302305

src/sage/geometry/toric_lattice.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@
144144
#
145145
# https://www.gnu.org/licenses/
146146
# ****************************************************************************
147-
147+
from typing import TYPE_CHECKING, Literal
148148
from sage.geometry.toric_lattice_element import ToricLatticeElement
149149
from sage.misc.lazy_import import lazy_import
150150
lazy_import('sage.geometry.toric_plotter', 'ToricPlotter')
@@ -162,6 +162,9 @@
162162
from sage.rings.rational_field import QQ
163163
from sage.structure.factory import UniqueFactory
164164

165+
if TYPE_CHECKING:
166+
from sage.misc.sage_input import SageInputBuilder, SageInputExpression
167+
165168

166169
def is_ToricLattice(x):
167170
r"""
@@ -897,7 +900,7 @@ def __init__(self, rank, name, dual_name, latex_name, latex_dual_name):
897900
self._latex_name = latex_name
898901
self._latex_dual_name = latex_dual_name
899902

900-
def _sage_input_(self, sib, coerced):
903+
def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression:
901904
r"""
902905
Return Sage command to reconstruct ``self``.
903906

src/sage/matrix/matrix1.pyx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ from cpython.sequence cimport PySequence_Fast
2424
import sage.modules.free_module
2525
from sage.structure.coerce cimport coercion_model
2626

27+
from sage.misc.sage_input import SageInputBuilder, SageInputExpression
2728

2829
cdef class Matrix(Matrix0):
2930
###################################################
@@ -622,7 +623,7 @@ cdef class Matrix(Matrix0):
622623
matrix._sage_object = self
623624
return matrix
624625

625-
def _sage_input_(self, sib, coerce):
626+
def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression:
626627
r"""
627628
Produce an expression which will reproduce this value when evaluated.
628629

src/sage/misc/explain_pickle.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -152,23 +152,24 @@
152152
# (at your option) any later version.
153153
# http://www.gnu.org/licenses/
154154
# *****************************************************************************
155-
156-
155+
import bz2 as comp_other
157156
import pickletools
158157
import re
159158
import sys
160159
import types
161-
162160
import zlib as comp
163-
import bz2 as comp_other
164-
165161
from pickletools import genops
166-
167-
from sage.misc.sage_input import SageInputBuilder, SageInputExpression
162+
from typing import Literal
163+
164+
from sage.misc.persist import (
165+
SageUnpickler,
166+
dumps,
167+
register_unpickle_override,
168+
unpickle_global,
169+
unpickle_override,
170+
)
168171
from sage.misc.sage_eval import sage_eval
169-
from sage.misc.persist import (unpickle_override, unpickle_global, dumps,
170-
register_unpickle_override, SageUnpickler)
171-
172+
from sage.misc.sage_input import SageInputBuilder, SageInputExpression
172173

173174
# Python 3 does not have a "ClassType". Instead, we ensure that
174175
# isinstance(foo, ClassType) will always return False.
@@ -358,7 +359,7 @@ def __init__(self, value, expression):
358359
self.expression = expression
359360
self.immutable = False
360361

361-
def _sage_input_(self, sib, coerced):
362+
def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression:
362363
r"""
363364
Extracts the expression from a PickleObject, and sets the immutable
364365
flag.
@@ -845,14 +846,13 @@ def _APPENDS_helper(self, lst, slice):
845846
else:
846847
for s in slice:
847848
self.sib.command(lst, self.sib.name('list').append(lst, self.sib(s)))
849+
elif self.pedantic:
850+
app = self.sib(lst).append
851+
for s in slice:
852+
self.sib.command(lst, app(self.sib(s)))
848853
else:
849-
if self.pedantic:
850-
app = self.sib(lst).append
851-
for s in slice:
852-
self.sib.command(lst, app(self.sib(s)))
853-
else:
854-
for s in slice:
855-
self.sib.command(lst, self.sib(lst).append(self.sib(s)))
854+
for s in slice:
855+
self.sib.command(lst, self.sib(lst).append(self.sib(s)))
856856
else:
857857
self.sib.command(lst, self.sib.name('unpickle_appends')(self.sib(lst), slice_exp))
858858
self.push(lst)
@@ -2509,7 +2509,7 @@ def unpickle_extension(code):
25092509
<class 'sage.misc.explain_pickle.EmptyNewstyleClass'>
25102510
sage: remove_extension('sage.misc.explain_pickle', 'EmptyNewstyleClass', 42)
25112511
"""
2512-
from copyreg import _inverted_registry, _extension_cache
2512+
from copyreg import _extension_cache, _inverted_registry
25132513
# copied from .get_extension() in pickle.py
25142514
nil = []
25152515
obj = _extension_cache.get(code, nil)

src/sage/misc/sage_input.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@
172172
# (at your option) any later version.
173173
# https://www.gnu.org/licenses/
174174
# ****************************************************************************
175+
from typing import Literal
175176

176177
from sage.misc.lazy_import import lazy_import
177178

@@ -343,7 +344,7 @@ def __init__(self, allow_locals=False, preparse=True):
343344
self._next_local = 1
344345
self._locals = {}
345346

346-
def __call__(self, x, coerced=False):
347+
def __call__(self, x, coerced: bool | Literal[2] = False):
347348
r"""
348349
Try to convert an arbitrary value ``x`` into a
349350
:class:`SageInputExpression` (an SIE).
@@ -477,11 +478,10 @@ def __call__(self, x, coerced=False):
477478
return SIE_literal_stringrep(self, str(x) + 'r')
478479
elif self._preparse is False:
479480
return self.int(x)
481+
elif x < 0:
482+
return -self.name('int')(self.int(-x))
480483
else:
481-
if x < 0:
482-
return -self.name('int')(self.int(-x))
483-
else:
484-
return self.name('int')(self.int(x))
484+
return self.name('int')(self.int(x))
485485

486486
if isinstance(x, float):
487487
# floats could often have prettier output,
@@ -499,8 +499,8 @@ def __call__(self, x, coerced=False):
499499
return -SIE_literal_stringrep(self, str(-x))
500500
else:
501501
return SIE_literal_stringrep(self, str(x))
502-
from sage.rings.real_mpfr import RR
503502
from sage.rings.integer_ring import ZZ
503+
from sage.rings.real_mpfr import RR
504504
rrx = RR(x)
505505
if rrx in ZZ and abs(rrx) < (1 << 53):
506506
return self.name('float')(self.int(ZZ(rrx)))
@@ -593,7 +593,7 @@ def float_str(self, n):
593593
"""
594594
return SIE_literal_stringrep(self, n)
595595

596-
def name(self, n):
596+
def name(self, n) -> SageInputExpression:
597597
r"""
598598
Given a string representing a Python name,
599599
produces a :class:`SageInputExpression` for that name.
@@ -2228,11 +2228,10 @@ def _sie_format(self, sif):
22282228
values = [sif.format(val, 0) for val in self._sie_values]
22292229
if self._sie_is_list:
22302230
return '[%s]' % ', '.join(values), _prec_atomic
2231+
elif len(values) == 1:
2232+
return '(%s,)' % values[0], _prec_atomic
22312233
else:
2232-
if len(values) == 1:
2233-
return '(%s,)' % values[0], _prec_atomic
2234-
else:
2235-
return '(%s)' % ', '.join(values), _prec_atomic
2234+
return '(%s)' % ', '.join(values), _prec_atomic
22362235

22372236

22382237
class SIE_dict(SageInputExpression):

src/sage/modules/free_module_element.pyx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ from sage.rings.abc import RealDoubleField, ComplexDoubleField
128128

129129
from sage.rings.integer cimport Integer, smallInteger
130130
from sage.arith.numerical_approx cimport digits_to_bits
131+
from sage.misc.sage_input import SageInputBuilder, SageInputExpression
131132

132133
# For the norm function, we cache Sage integers 1 and 2
133134
__one__ = smallInteger(1)
@@ -1258,7 +1259,7 @@ cdef class FreeModuleElement(Vector): # abstract base class
12581259
return self
12591260
return self.change_ring(R)
12601261

1261-
def _sage_input_(self, sib, coerce):
1262+
def _sage_input_(self, sib: SageInputBuilder, coerced: bool | Literal[2]) -> SageInputExpression:
12621263
r"""
12631264
Produce an expression which will reproduce this value when evaluated.
12641265

0 commit comments

Comments
 (0)