Skip to content

Commit cd61411

Browse files
author
Release Manager
committed
gh-37232: Still less use of isinstance of old-parents this is removing many calls to isinstance of old-style parent-like classes one should use instead the corresponding containment in categories the case of `Finitefield` is kept for another time ### 📝 Checklist - [x] The title is concise, informative, and self-explanatory. - [x] The description explains in detail what this PR is about. URL: #37232 Reported by: Frédéric Chapoton Reviewer(s): Martin Rubey, Matthias Köppe
2 parents 3d38254 + 5d6033d commit cd61411

File tree

14 files changed

+50
-61
lines changed

14 files changed

+50
-61
lines changed

src/sage/algebras/algebra.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
# http://www.gnu.org/licenses/
1919
#*****************************************************************************
2020

21-
from sage.rings.ring import Algebra
2221
from sage.categories.algebras import Algebras
2322

23+
2424
def is_Algebra(x):
2525
r"""
2626
Return True if x is an Algebra.
@@ -37,7 +37,4 @@ def is_Algebra(x):
3737
"""
3838
from sage.misc.superseded import deprecation
3939
deprecation(35253, "the function is_Algebra is deprecated; use '... in Algebras(base_ring)' instead")
40-
try:
41-
return isinstance(x, Algebra) or x in Algebras(x.base_ring())
42-
except Exception:
43-
return False
40+
return x in Algebras(x.base_ring())

src/sage/algebras/all.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
"""
22
Algebras
33
"""
4-
5-
#*****************************************************************************
4+
# ****************************************************************************
65
# Copyright (C) 2005 William Stein <[email protected]>
76
#
87
# Distributed under the terms of the GNU General Public License (GPL)
@@ -14,10 +13,13 @@
1413
#
1514
# The full text of the GPL is available at:
1615
#
17-
# http://www.gnu.org/licenses/
18-
#*****************************************************************************
16+
# https://www.gnu.org/licenses/
17+
# ****************************************************************************
1918
from sage.misc.lazy_import import lazy_import
2019

20+
# old-style class for associative algebras, use Parent instead
21+
from sage.rings.ring import Algebra
22+
2123
import sage.algebras.catalog as algebras
2224

2325
from .quatalg.all import *
@@ -28,11 +30,9 @@
2830
from .lie_conformal_algebras.all import *
2931

3032
# Algebra base classes
31-
from .algebra import Algebra
3233
from .free_algebra import FreeAlgebra
3334
from .free_algebra_quotient import FreeAlgebraQuotient
3435

35-
3636
from .finite_dimensional_algebras.all import FiniteDimensionalAlgebra
3737

3838
lazy_import('sage.algebras.group_algebra', 'GroupAlgebra')

src/sage/categories/algebras.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,7 @@ def __contains__(self, x):
7373
sage: QQ['x'] in Algebras(CDF) # needs sage.rings.complex_double
7474
False
7575
"""
76-
if super().__contains__(x):
77-
return True
78-
from sage.rings.ring import Algebra
79-
return isinstance(x, Algebra) and x.base_ring() == self.base_ring()
76+
return super().__contains__(x)
8077

8178
# def extra_super_categories(self):
8279
# """

src/sage/matrix/matrix0.pyx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ from sage.misc.misc_c cimport normalize_index
3939

4040
from sage.categories.fields import Fields
4141
from sage.categories.integral_domains import IntegralDomains
42+
from sage.categories.commutative_rings import CommutativeRings
43+
from sage.categories.rings import Rings
4244

43-
from sage.rings.ring cimport CommutativeRing
44-
from sage.rings.ring import is_Ring
4545
import sage.rings.abc
4646
from sage.rings.integer_ring import is_IntegerRing
4747

@@ -1660,7 +1660,7 @@ cdef class Matrix(sage.structure.element.Matrix):
16601660
[---]
16611661
[2 4]
16621662
"""
1663-
if not is_Ring(ring):
1663+
if ring not in Rings():
16641664
raise TypeError("ring must be a ring")
16651665

16661666
if ring is self._base_ring:
@@ -5323,7 +5323,7 @@ cdef class Matrix(sage.structure.element.Matrix):
53235323
[ -x*y*x*y x*y*x + x*y^2 x*y*x - x*y^2]
53245324
"""
53255325
# derived classes over a commutative base *just* overload _lmul_ (!!)
5326-
if isinstance(self._base_ring, CommutativeRing):
5326+
if self._base_ring in CommutativeRings():
53275327
return self._lmul_(left)
53285328
cdef Py_ssize_t r,c
53295329
x = self._base_ring(left)

src/sage/modular/abvar/homology.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@
5252
# https://www.gnu.org/licenses/
5353
# ****************************************************************************
5454

55+
from sage.categories.commutative_rings import CommutativeRings
5556
from sage.modular.hecke.module import HeckeModule_free_module
5657
from sage.rings.integer import Integer
5758
from sage.rings.integer_ring import ZZ
5859
from sage.rings.rational_field import QQ
59-
from sage.rings.ring import CommutativeRing
6060
from sage.structure.richcmp import richcmp_method, richcmp, richcmp_not_equal
6161

6262
# TODO: we will probably also need homology that is *not* a Hecke module.
@@ -74,11 +74,9 @@ def hecke_polynomial(self, n, var='x'):
7474
7575
INPUT:
7676
77+
- ``n`` -- positive integer
7778
78-
- ``n`` - positive integer
79-
80-
- ``var`` - string (default: 'x') the variable name
81-
79+
- ``var`` -- string (default: 'x') the variable name
8280
8381
OUTPUT: a polynomial over ZZ in the given variable
8482
@@ -117,7 +115,7 @@ def __init__(self, abvar, base):
117115
sage: loads(dumps(H)) == H
118116
True
119117
"""
120-
if not isinstance(base, CommutativeRing):
118+
if base not in CommutativeRings():
121119
raise TypeError("base ring must be a commutative ring")
122120
HeckeModule_free_module.__init__(
123121
self, base, abvar.level(), weight=2)

src/sage/modular/hecke/module.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from sage.modules.module import Module
2323
from sage.rings.integer_ring import ZZ
2424
from sage.rings.rational_field import QQ
25-
from sage.rings.ring import CommutativeRing
25+
from sage.categories.commutative_rings import CommutativeRings
2626
from sage.structure.sequence import Sequence
2727

2828
from . import algebra
@@ -76,7 +76,7 @@ def __init__(self, base_ring, level, category=None):
7676
sage: ModularForms(3, 3).category()
7777
Category of Hecke modules over Rational Field
7878
"""
79-
if not isinstance(base_ring, CommutativeRing):
79+
if base_ring not in CommutativeRings():
8080
raise TypeError("base_ring must be commutative ring")
8181

8282
from sage.categories.hecke_modules import HeckeModules

src/sage/modular/modform/constructor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import sage.modular.dirichlet as dirichlet
3838
from sage.rings.integer import Integer
3939
from sage.rings.rational_field import Q as QQ
40-
from sage.rings.ring import CommutativeRing
40+
from sage.categories.commutative_rings import CommutativeRings
4141

4242
from .ambient_eps import ModularFormsAmbient_eps
4343
from .ambient_g0 import ModularFormsAmbient_g0_Q
@@ -122,11 +122,11 @@ def canonical_parameters(group, level, weight, base_ring):
122122
except TypeError:
123123
raise TypeError("group of unknown type.")
124124
level = Integer(level)
125-
if ( m != level ):
125+
if m != level:
126126
raise ValueError("group and level do not match.")
127127
group = arithgroup.Gamma0(m)
128128

129-
if not isinstance(base_ring, CommutativeRing):
129+
if base_ring not in CommutativeRings():
130130
raise TypeError("base_ring (=%s) must be a commutative ring" % base_ring)
131131

132132
# it is *very* important to include the level as part of the data

src/sage/modular/modsym/modsym.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,11 @@
106106

107107
import weakref
108108

109+
from sage.categories.commutative_rings import CommutativeRings
110+
from sage.categories.fields import Fields
109111
import sage.modular.arithgroup.all as arithgroup
110112
import sage.modular.dirichlet as dirichlet
111113
from sage.rings.integer import Integer
112-
from sage.rings.ring import CommutativeRing
113114
from sage.rings.rational_field import RationalField
114115

115116

@@ -153,10 +154,10 @@ def canonical_parameters(group, weight, sign, base_ring):
153154
if base_ring is None:
154155
base_ring = RationalField()
155156

156-
if not isinstance(base_ring, CommutativeRing):
157+
elif base_ring not in CommutativeRings():
157158
raise TypeError(f"base_ring (={base_ring}) must be a commutative ring")
158159

159-
if not base_ring.is_field():
160+
elif base_ring not in Fields():
160161
raise TypeError(f"(currently) base_ring (={base_ring}) must be a field")
161162

162163
return group, weight, sign, base_ring

src/sage/modular/quatalg/brandt.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@
203203
# ****************************************************************************
204204

205205
from sage.arith.misc import gcd, factor, prime_divisors, kronecker, next_prime
206+
from sage.categories.commutative_rings import CommutativeRings
206207
from sage.matrix.constructor import matrix
207208
from sage.matrix.matrix_space import MatrixSpace
208209
from sage.misc.cachefunc import cached_method
@@ -219,7 +220,6 @@
219220
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
220221
from sage.rings.power_series_ring import PowerSeriesRing
221222
from sage.rings.rational_field import QQ
222-
from sage.rings.ring import CommutativeRing
223223
from sage.structure.richcmp import richcmp, richcmp_method
224224

225225
lazy_import('sage.algebras.quatalg.quaternion_algebra', ['QuaternionAlgebra', 'basis_for_quaternion_lattice'])
@@ -300,7 +300,7 @@ def BrandtModule(N, M=1, weight=2, base_ring=QQ, use_cache=True):
300300
raise ValueError("M must be coprime to N")
301301
if weight < 2:
302302
raise ValueError("weight must be at least 2")
303-
if not isinstance(base_ring, CommutativeRing):
303+
if base_ring not in CommutativeRings():
304304
raise TypeError("base_ring must be a commutative ring")
305305
key = (N, M, weight, base_ring)
306306
if use_cache:

src/sage/rings/commutative_algebra.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
# http://www.gnu.org/licenses/
1818
#*****************************************************************************
1919

20-
from sage.rings.ring import CommutativeAlgebra
20+
from sage.categories.commutative_algebras import CommutativeAlgebras
21+
2122

2223
def is_CommutativeAlgebra(x):
2324
"""
@@ -35,4 +36,4 @@ def is_CommutativeAlgebra(x):
3536
"""
3637
from sage.misc.superseded import deprecation
3738
deprecation(35253, "the function is_CommutativeAlgebra is deprecated; use '... in Algebras(base_ring).Commutative()' instead")
38-
return isinstance(x, CommutativeAlgebra)
39+
return x in CommutativeAlgebras(x.base_ring())

0 commit comments

Comments
 (0)