Skip to content

Commit d5a3fd1

Browse files
author
Release Manager
committed
gh-35884: `sage.{modular,schemes}`: Modularization fixes for imports; update `sage -fiximports`, add relint pattern <!-- Please provide a concise, informative and self-explanatory title. --> <!-- Don't put issue numbers in the title. Put it in the Description below. --> <!-- For example, instead of "Fixes #12345", use "Add a new method to multiply two integers" --> ### 📚 Description <!-- Describe your changes here in detail. --> <!-- Why is this change required? What problem does it solve? --> <!-- If this PR resolves an open issue, please link to it here. For example "Fixes #12345". --> - Part of: #29705 - Cherry-picked from: #35095 <!-- If your change requires a documentation PR, please link it appropriately. --> ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. It should be `[x]` not `[x ]`. --> - [x] The title is concise, informative, and self-explanatory. - [ ] The description explains in detail what this PR is about. - [ ] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation accordingly. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on - #12345: short description why this is a dependency - #34567: ... --> - Depends on #35974 (to soothe the linter) <!-- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> URL: #35884 Reported by: Matthias Köppe Reviewer(s): Kwankyu Lee, Matthias Köppe
2 parents 0fd5967 + bcd1d6e commit d5a3fd1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+489
-356
lines changed

src/.relint.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,18 @@
5151
# Keep in sync with SAGE_ROOT/src/sage/misc/replace_dot_all.py
5252
pattern: 'from\s+sage(|[.](arith|categories|combinat|crypto|databases|data_structures|dynamics|ext|game_theory|games|geometry|graphs|groups|interfaces|manifolds|matrix|matroids|misc|modules|monoids|numerical|probability|quadratic_forms|quivers|rings|sat|schemes|sets|stats|symbolic|tensor)[a-z0-9_.]*|[.]libs)[.]all\s+import'
5353
# imports from .all are allowed in all.py; also allow in some modules that need sage.all
54-
filePattern: '(.*/|)(?!(all|benchmark|dev_tools|parsing|sage_eval))[^/.]*[.](py|pyx|pxi)$'
54+
filePattern: '(.*/|)(?!(all|benchmark|dev_tools|parsing|sage_eval|explain_pickle|.*_test))[^/.]*[.](py|pyx|pxi)$'
55+
56+
- name: 'namespace_pkg_all_import_2: Module-level import of .all of a namespace package'
57+
hint: |
58+
Sage library code should not import sage.PAC.KAGE.all when sage.PAC.KAGE is an implicit
59+
namespace package. Type import_statements("SOME_IDENTIFIER") to find a more specific import,
60+
and rewrite the import statement as "from sage.PAC.KAGE.MODULE import ..."
61+
or "lazy_import('sage.PAC.KAGE.MODULE', '...')".
62+
# Keep in sync with above; but for now we ignore sage.{arith,categories}
63+
pattern: '^import\s+sage(|[.](combinat|crypto|databases|data_structures|dynamics|ext|game_theory|games|geometry|graphs|groups|interfaces|manifolds|matrix|matroids|misc|modules|monoids|numerical|probability|quadratic_forms|quivers|rings|sat|schemes|sets|stats|symbolic|tensor)[a-z0-9_.]*|[.]libs)[.]all'
64+
# imports from .all are allowed in all.py; also allow in some modules that need sage.all
65+
filePattern: '(.*/|)(?!(all|benchmark|dev_tools|parsing|sage_eval|explain_pickle|.*_test))[^/.]*[.](py|pyx|pxi)$'
5566

5667
# Magic doctest comments
5768

src/sage/lfunctions/lcalc.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,14 @@
3030
import os
3131

3232
from sage.structure.sage_object import SageObject
33+
from sage.misc.lazy_import import lazy_import
3334
from sage.misc.pager import pager
34-
import sage.rings.all
35-
import sage.schemes.elliptic_curves.ell_generic
35+
from sage.rings.integer_ring import ZZ
36+
from sage.rings.rational_field import QQ
37+
38+
lazy_import('sage.rings.complex_mpfr', 'ComplexField')
39+
lazy_import('sage.rings.real_mpfr', 'RealField')
40+
lazy_import('sage.schemes.elliptic_curves.ell_generic', 'EllipticCurve_generic', as_='EllipticCurve')
3641

3742
prec = 32
3843

@@ -75,9 +80,8 @@ def _compute_L(self, L):
7580
if L == 'tau':
7681
return '--tau'
7782
return L
78-
import sage.schemes.all
79-
if sage.schemes.elliptic_curves.ell_generic.is_EllipticCurve(L):
80-
if L.base_ring() == sage.rings.all.RationalField():
83+
if isinstance(L, EllipticCurve):
84+
if L.base_ring() == QQ:
8185
L = L.minimal_model()
8286
return '-e --a1 %s --a2 %s --a3 %s --a4 %s --a6 %s' % tuple(L.a_invariants())
8387
raise TypeError("$L$-function of %s not known" % L)
@@ -127,7 +131,7 @@ def zeros(self, n, L=''):
127131
[0.000000000, 5.00317001, 6.87039122]
128132
"""
129133
L = self._compute_L(L)
130-
RR = sage.rings.all.RealField(prec)
134+
RR = RealField(prec)
131135
X = self('-z %s %s' % (int(n), L))
132136
return [RR(z) for z in X.split()]
133137

@@ -162,7 +166,7 @@ def zeros_in_interval(self, x, y, stepsize, L=''):
162166
[(14.1347251, 0.184672916), (21.0220396, -0.0677893290), (25.0108576, -0.0555872781)]
163167
"""
164168
L = self._compute_L(L)
165-
RR = sage.rings.all.RealField(prec)
169+
RR = RealField(prec)
166170
X = self('--zeros-interval -x %s -y %s --stepsize=%s %s' % (
167171
float(x), float(y), float(stepsize), L))
168172
return [tuple([RR(z) for z in t.split()]) for t in X.split('\n')]
@@ -193,7 +197,7 @@ def value(self, s, L=''):
193197
2.69261988568132 - 0.0203860296025982*I
194198
"""
195199
L = self._compute_L(L)
196-
CC = sage.rings.all.ComplexField(prec)
200+
CC = ComplexField(prec)
197201
s = CC(s)
198202
x, y = self('-v -x %s -y %s %s' % (s.real(), s.imag(), L)).split()
199203
return CC((float(x), float(y)))
@@ -275,7 +279,7 @@ def values_along_line(self, s0, s1, number_samples, L=''):
275279
276280
"""
277281
L = self._compute_L(L)
278-
CC = sage.rings.all.ComplexField(prec)
282+
CC = ComplexField(prec)
279283
s0 = CC(s0)
280284
s1 = CC(s1)
281285
v = self('--value-line-segment -x %s -y %s -X %s -Y %s --number-samples %s %s' % (
@@ -343,8 +347,7 @@ def twist_values(self, s, dmin, dmax, L=''):
343347
0.373691713 + 0.0*I
344348
"""
345349
L = self._compute_L(L)
346-
CC = sage.rings.all.ComplexField(prec)
347-
Z = sage.rings.all.Integer
350+
CC = ComplexField(prec)
348351
s = CC(s)
349352
typ = '--twist-quadratic'
350353
dmin = int(dmin)
@@ -358,7 +361,7 @@ def twist_values(self, s, dmin, dmax, L=''):
358361
return w
359362
for a in v.split('\n'):
360363
d, x, y = a.split()
361-
w.append((Z(d), CC(x, y)))
364+
w.append((ZZ(d), CC(x, y)))
362365
return w
363366

364367
def twist_zeros(self, n, dmin, dmax, L=''):
@@ -393,8 +396,7 @@ def twist_zeros(self, n, dmin, dmax, L=''):
393396
{-3: [8.03973716, 11.2492062, 15.7046192], 5: [6.64845335, 9.83144443, 11.9588456]}
394397
"""
395398
L = self._compute_L(L)
396-
RR = sage.rings.all.RealField(prec)
397-
Z = sage.rings.all.Integer
399+
RR = RealField(prec)
398400
typ = '--twist-quadratic'
399401
n = int(n)
400402
v = self('-z %s %s --start %s --finish %s %s' % (
@@ -405,7 +407,7 @@ def twist_zeros(self, n, dmin, dmax, L=''):
405407
for a in v.split('\n'):
406408
d, x = a.split()
407409
x = RR(x)
408-
d = Z(d)
410+
d = ZZ(d)
409411
if d in w:
410412
w[d].append(x)
411413
else:
@@ -439,10 +441,9 @@ def analytic_rank(self, L=''):
439441
1
440442
"""
441443
L = self._compute_L(L)
442-
Z = sage.rings.all.Integer
443444
s = self('--rank-compute %s' % L)
444445
i = s.find('equals')
445-
return Z(s[i + 6:])
446+
return ZZ(s[i + 6:])
446447

447448

448449
# An instance

src/sage/lfunctions/sympow.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
from sage.structure.sage_object import SageObject
5252
from sage.misc.pager import pager
5353
from sage.misc.verbose import verbose
54-
import sage.rings.all
54+
from sage.rings.integer import Integer
5555

5656

5757
class Sympow(SageObject):
@@ -237,7 +237,7 @@ def modular_degree(self, E):
237237
if i == -1:
238238
print(self._fix_err(v))
239239
raise RuntimeError("failed to compute modular degree")
240-
return sage.rings.all.Integer(v[i + len(s):])
240+
return Integer(v[i + len(s):])
241241

242242
def analytic_rank(self, E):
243243
r"""
@@ -296,7 +296,7 @@ def analytic_rank(self, E):
296296
print(self._fix_err(v))
297297
raise RuntimeError("failed to compute analytic rank")
298298
j = v.rfind(':')
299-
r = sage.rings.all.Integer(v[i + len(s):j])
299+
r = Integer(v[i + len(s):j])
300300
i = v.rfind(' ')
301301
L = v[i + 1:]
302302
return r, L

src/sage/misc/replace_dot_all.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,8 @@ def make_replacements_in_file(location, package_regex=None, verbose=False, outpu
385385
write_file.write(replaced_content) # overwriting the old file contents with the new/replaced content
386386

387387

388-
def walkdir_replace_dot_all(dir, file_regex=r'.*[.](py|pyx|pxi)$', package_regex=None, verbose=False):
388+
def walkdir_replace_dot_all(dir, file_regex=r'.*[.](py|pyx|pxi)$', package_regex=None, verbose=False, *,
389+
excluded_file_regex=r'auto-methods|replace_dot_all'):
389390
r"""
390391
Replace ``import`` statements in the files in directory ``dir`` matching the regex pattern ``file_regex``.
391392
@@ -396,6 +397,7 @@ def walkdir_replace_dot_all(dir, file_regex=r'.*[.](py|pyx|pxi)$', package_regex
396397
- ``package_regex`` -- (default: :obj:`default_package_regex`) a regular expression matching
397398
the ``sage.PAC.KAGE.all`` package names from which we do not want to import.
398399
- ``verbose`` -- if True, print statements when interesting examples are found
400+
- ``excluded_file_regex`` -- a regular expression matching the file names to exclude
399401
400402
EXAMPLES::
401403
@@ -404,14 +406,14 @@ def walkdir_replace_dot_all(dir, file_regex=r'.*[.](py|pyx|pxi)$', package_regex
404406
"""
405407
global numberFiles, numberFilesMatchingRegex
406408
file_regex = re.compile(file_regex)
409+
excluded_file_regex = re.compile(excluded_file_regex)
407410
for root, dirs, files in os.walk(dir, topdown=False):
408411
for name in files:
409412
numberFiles += 1
410-
if file_regex.search(name):
413+
if file_regex.search(name) and not excluded_file_regex.search(name):
411414
numberFilesMatchingRegex += 1
412415
location = os.path.join(root, name)
413-
if location.find('replace_dot_all') == -1: # to avoid changing anything in this file itself
414-
make_replacements_in_file(location, package_regex, verbose)
416+
make_replacements_in_file(location, package_regex, verbose)
415417

416418

417419
# ******************************************************** EXECUTES MAIN FUNCTION **********************************************************************

src/sage/modular/abvar/abvar.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
from copy import copy
3434
from random import randrange
3535

36+
import sage.rings.abc
37+
3638
from sage.arith.functions import lcm as LCM
3739
from sage.arith.misc import divisors, next_prime, is_prime
3840
from sage.categories.modular_abelian_varieties import ModularAbelianVarieties
@@ -55,7 +57,6 @@
5557
from sage.rings.integer import Integer
5658
from sage.rings.integer_ring import ZZ
5759
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
58-
from sage.rings.qqbar import QQbar
5960
from sage.rings.rational_field import QQ
6061
from sage.rings.ring import is_Ring
6162
from sage.schemes.elliptic_curves.constructor import EllipticCurve
@@ -67,6 +68,7 @@
6768
lazy_import('sage.databases.cremona',
6869
['cremona_letter_code', 'CremonaDatabase'])
6970

71+
7072
from . import homspace
7173
from . import lseries
7274
from .morphism import HeckeOperator, Morphism, DegeneracyMap
@@ -798,8 +800,8 @@ def _Hom_(self, B, cat=None):
798800
L = B.base_field()
799801
if K == L:
800802
F = K
801-
elif K == QQbar or L == QQbar:
802-
F = QQbar
803+
elif isinstance(K, sage.rings.abc.AlgebraicField) or isinstance(L, sage.rings.abc.AlgebraicField):
804+
from sage.rings.qqbar import QQbar as F
803805
else:
804806
# TODO -- improve this
805807
raise ValueError("please specify a category")
@@ -997,7 +999,7 @@ def intersection(self, other):
997999
for v in V.coordinate_module(S).basis()]
9981000

9991001
if A.dimension() > 0:
1000-
finitegroup_base_field = QQbar
1002+
from sage.rings.qqbar import QQbar as finitegroup_base_field
10011003
else:
10021004
finitegroup_base_field = self.base_field()
10031005
G = self.finite_subgroup(gens, field_of_definition=finitegroup_base_field)
@@ -3126,7 +3128,7 @@ def finite_subgroup(self, X, field_of_definition=None, check=True):
31263128
raise ValueError("X must be a subgroup of self.")
31273129

31283130
if field_of_definition is None:
3129-
field_of_definition = QQbar
3131+
from sage.rings.qqbar import QQbar as field_of_definition
31303132

31313133
return FiniteSubgroup_lattice(
31323134
self, X, field_of_definition=field_of_definition, check=check)

src/sage/modular/abvar/finite_subgroup.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@
9797
# http://www.gnu.org/licenses/
9898
#*****************************************************************************
9999

100+
import sage.rings.abc
101+
102+
from sage.misc.lazy_import import lazy_import
100103
from sage.modular.abvar.torsion_point import TorsionPoint
101104
from sage.modules.module import Module
102105
from sage.modules.free_module import is_FreeModule
@@ -105,7 +108,6 @@
105108
from sage.structure.richcmp import richcmp_method, richcmp
106109
from sage.rings.integer import Integer
107110
from sage.rings.integer_ring import ZZ
108-
from sage.rings.qqbar import QQbar
109111
from sage.rings.rational_field import QQ
110112
from sage.arith.functions import lcm
111113
from sage.misc.misc_c import prod
@@ -521,7 +523,7 @@ def _repr_(self):
521523
'Finite subgroup with invariants [3, 3, 3, 3, 3, 3, 3, 3, 3, 3] over QQ of Abelian variety J0(42) of dimension 5'
522524
"""
523525
K = self.__field_of_definition
524-
if K == QQbar:
526+
if isinstance(K, sage.rings.abc.AlgebraicField):
525527
field = "QQbar"
526528
elif K == QQ:
527529
field = "QQ"
@@ -750,6 +752,8 @@ def subgroup(self, gens):
750752
sage: H == G.subgroup([[1/11,0,0,0]])
751753
True
752754
"""
755+
from sage.rings.qqbar import QQbar
756+
753757
if not isinstance(gens, (tuple, list)):
754758
raise TypeError("gens must be a list or tuple")
755759
A = self.abelian_variety()
@@ -815,7 +819,7 @@ def invariants(self):
815819

816820

817821
class FiniteSubgroup_lattice(FiniteSubgroup):
818-
def __init__(self, abvar, lattice, field_of_definition=QQbar, check=True):
822+
def __init__(self, abvar, lattice, field_of_definition=None, check=True):
819823
"""
820824
A finite subgroup of a modular abelian variety that is defined by a
821825
given lattice.
@@ -841,6 +845,8 @@ def __init__(self, abvar, lattice, field_of_definition=QQbar, check=True):
841845
sage: G = J.finite_subgroup([[1/3,0], [0,1/5]]); G
842846
Finite subgroup with invariants [15] over QQbar of Abelian variety J0(11) of dimension 1
843847
"""
848+
if field_of_definition is None:
849+
from sage.rings.qqbar import QQbar as field_of_definition
844850
if check:
845851
from .abvar import is_ModularAbelianVariety
846852
if not is_FreeModule(lattice) or lattice.base_ring() != ZZ:

src/sage/modular/abvar/homspace.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,14 @@
186186
from . import morphism
187187

188188
import sage.rings.integer_ring
189-
import sage.rings.all
189+
from sage.rings.infinity import Infinity
190190

191191
from sage.rings.ring import Ring
192192
from sage.matrix.matrix_space import MatrixSpace
193193
from sage.matrix.constructor import Matrix, identity_matrix
194194
from sage.structure.element import is_Matrix
195195

196-
ZZ = sage.rings.integer_ring.ZZ
196+
from sage.rings.integer_ring import ZZ
197197

198198

199199
class Homspace(HomsetWithBase):
@@ -882,7 +882,7 @@ def index_in(self, other, check=True):
882882
M = self.free_module()
883883
N = other.free_module()
884884
if M.rank() < N.rank():
885-
return sage.rings.all.Infinity
885+
return Infinity
886886
return M.index_in(N)
887887

888888
def index_in_saturation(self):

src/sage/modular/arithgroup/all.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
from .congroup_gamma import Gamma_constructor as Gamma, is_Gamma
1010
from .congroup_sl2z import SL2Z, is_SL2Z
1111

12-
from .arithgroup_perm import ArithmeticSubgroup_Permutation
12+
from sage.misc.lazy_import import lazy_import
13+
lazy_import('sage.modular.arithgroup.arithgroup_perm', 'ArithmeticSubgroup_Permutation')
1314

1415
from .congroup import (degeneracy_coset_representatives_gamma0,
1516
degeneracy_coset_representatives_gamma1)

src/sage/modular/arithgroup/arithgroup_generic.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -885,20 +885,21 @@ def index(self):
885885

886886
def generalised_level(self):
887887
r"""
888-
Return the generalised level of self, i.e. the least common multiple of
888+
Return the generalised level of ``self``, i.e., the least common multiple of
889889
the widths of all cusps.
890890
891-
If self is *even*, Wohlfart's theorem tells us that this is equal to
892-
the (conventional) level of self when self is a congruence subgroup.
893-
This can fail if self is odd, but the actual level is at most twice the
891+
If ``self`` is *even*, Wohlfart's theorem tells us that this is equal to
892+
the (conventional) level of ``self`` when ``self`` is a congruence subgroup.
893+
This can fail if ``self`` is odd, but the actual level is at most twice the
894894
generalised level. See the paper by Kiming, Schuett and Verrill for
895895
more examples.
896896
897897
EXAMPLES::
898898
899899
sage: Gamma0(18).generalised_level()
900900
18
901-
sage: sage.modular.arithgroup.arithgroup_perm.HsuExample18().generalised_level()
901+
sage: from sage.modular.arithgroup.arithgroup_perm import HsuExample18
902+
sage: HsuExample18().generalised_level()
902903
24
903904
904905
In the following example, the actual level is twice the generalised

0 commit comments

Comments
 (0)