Skip to content

Commit 5e79eea

Browse files
author
Release Manager
committed
gh-40255: some care for ruff RET in algebras/ This is about simplifying code, following `ruff check --select=RET src/sage/algebras` ### 📝 Checklist - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. URL: #40255 Reported by: Frédéric Chapoton Reviewer(s): David Coudert, Frédéric Chapoton
2 parents e61174d + 9a2ae89 commit 5e79eea

18 files changed

+69
-89
lines changed

src/sage/algebras/cluster_algebra.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1988,8 +1988,7 @@ def F_polynomial(self, g_vector):
19881988
msg += "you can compute it by mutating from the initial seed along the sequence "
19891989
msg += str(self._path_dict[g_vector])
19901990
raise KeyError(msg)
1991-
else:
1992-
raise KeyError("the g-vector %s has not been found yet" % str(g_vector))
1991+
raise KeyError("the g-vector {} has not been found yet".format(g_vector))
19931992

19941993
def find_g_vector(self, g_vector, depth=infinity):
19951994
r"""

src/sage/algebras/commutative_dga.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,8 +1286,7 @@ def _element_constructor_(self, x, coerce=True):
12861286

12871287
if isinstance(x, sage.interfaces.abc.SingularElement):
12881288
# self._singular_().set_ring()
1289-
x = self.element_class(self, x.sage_poly(self.cover_ring()))
1290-
return x
1289+
return self.element_class(self, x.sage_poly(self.cover_ring()))
12911290

12921291
return self.element_class(self, x)
12931292

src/sage/algebras/finite_dimensional_algebras/finite_dimensional_algebra.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -749,8 +749,7 @@ def one(self):
749749
"""
750750
if not self.is_unitary():
751751
raise TypeError("algebra is not unitary")
752-
else:
753-
return self(self._one)
752+
return self(self._one)
754753

755754
def random_element(self, *args, **kwargs):
756755
"""

src/sage/algebras/finite_gca.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,7 @@ def __classcall_private__(cls, base, names=None, degrees=None,
160160
if names is None:
161161
if degrees is None:
162162
raise ValueError("you must specify names or degrees")
163-
else:
164-
n = len(degrees)
163+
n = len(degrees)
165164
names = tuple(f'x{i}' for i in range(n))
166165
elif isinstance(names, str):
167166
names = tuple(names.split(','))
@@ -397,13 +396,13 @@ def _repr_term(self, w) -> str:
397396
return '1'
398397
# Non-trivial case:
399398
terms = []
400-
for i in range(len(w)):
401-
if w[i] == 0:
399+
for i, wi in enumerate(w):
400+
if wi == 0:
402401
continue
403-
elif w[i] == 1:
402+
if wi == 1:
404403
terms.append(self._names[i])
405404
else:
406-
terms.append(self._names[i] + f'^{w[i]}')
405+
terms.append(self._names[i] + f'^{wi}')
407406
return self._mul_symbol.join(terms)
408407

409408
def _latex_term(self, w) -> str:
@@ -432,13 +431,13 @@ def _latex_term(self, w) -> str:
432431
return '1'
433432
# Non-trivial case:
434433
terms = []
435-
for i in range(len(w)):
436-
if w[i] == 0:
434+
for i, wi in enumerate(w):
435+
if wi == 0:
437436
continue
438-
elif w[i] == 1:
437+
if wi == 1:
439438
terms.append(self._names[i])
440439
else:
441-
terms.append(self._names[i] + '^{' + str(w[i]) + '}')
440+
terms.append(self._names[i] + '^{' + str(wi) + '}')
442441
latex_mul = self._mul_latex_symbol + ' ' # add whitespace
443442
return latex_mul.join(terms)
444443

src/sage/algebras/free_algebra_element.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def __init__(self, A, x):
8282

8383
IndexedFreeModuleElement.__init__(self, A, x)
8484

85-
def _repr_(self):
85+
def _repr_(self) -> str:
8686
"""
8787
Return string representation of ``self``.
8888
@@ -104,10 +104,9 @@ def _repr_(self):
104104
M = P.monoid()
105105
from sage.structure.parent_gens import localvars
106106
with localvars(M, P.variable_names(), normalize=False):
107-
x = repr_lincomb(v, strip_one=True)
108-
return x
107+
return repr_lincomb(v, strip_one=True)
109108

110-
def _latex_(self):
109+
def _latex_(self) -> str:
111110
r"""
112111
Return latex representation of ``self``.
113112

src/sage/algebras/fusion_rings/f_matrix.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,12 +1379,9 @@ def _map_triv_reduce(self, mapper, input_iter, worker_pool=None, chunksize=None,
13791379
else:
13801380
mapped = worker_pool.imap_unordered(executor, input_iter, chunksize=chunksize)
13811381
# Reduce phase
1382-
results = set()
1383-
for child_eqns in mapped:
1384-
if child_eqns is not None:
1385-
results.update(child_eqns)
1386-
results = list(results)
1387-
return results
1382+
results = {eqn for child_eqns in mapped for eqn in child_eqns
1383+
if child_eqns is not None}
1384+
return list(results)
13881385

13891386
########################
13901387
# Equations set up #
@@ -1770,8 +1767,7 @@ def _par_graph_gb(self, eqns=None, term_order='degrevlex', largest_comp=45, verb
17701767
small_comps.append(comp_eqns)
17711768
input_iter = zip_longest(small_comps, [], fillvalue=term_order)
17721769
small_comp_gb = self._map_triv_reduce('compute_gb', input_iter, worker_pool=self.pool, chunksize=1, mp_thresh=50)
1773-
ret = small_comp_gb + temp_eqns
1774-
return ret
1770+
return small_comp_gb + temp_eqns
17751771

17761772
def _get_component_variety(self, var, eqns):
17771773
r"""
@@ -2324,7 +2320,7 @@ def find_cyclotomic_solution(self, equations=None, algorithm='', verbose=True, o
23242320
[]
23252321
"""
23262322
if self._poly_ring.ngens() == 0:
2327-
return
2323+
return None
23282324
self._reset_solver_state()
23292325
self._var_to_sextuple = {self._poly_ring.gen(i): s for i, s in self._idx_to_sextuple.items()}
23302326

src/sage/algebras/fusion_rings/fusion_double.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,9 @@ def s_matrix(self, unitary=False, base_coercion=True):
328328
[ 1/3 1/3 -1/3 0 0 -1/3 2/3 -1/3]
329329
"""
330330
b = self.basis()
331-
S = matrix([[self.s_ij(b[x], b[y], unitary=unitary, base_coercion=base_coercion)
332-
for x in self.get_order()] for y in self.get_order()])
333-
return S
331+
return matrix([[self.s_ij(b[x], b[y], unitary=unitary,
332+
base_coercion=base_coercion)
333+
for x in self.get_order()] for y in self.get_order()])
334334

335335
@cached_method
336336
def N_ijk(self, i, j, k):

src/sage/algebras/hecke_algebras/cubic_hecke_algebra.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,8 +1552,6 @@ def product_on_basis(self, g1, g2):
15521552
g1 = self.monomial(g1)
15531553
g2 = self.monomial(g2)
15541554

1555-
result = None
1556-
15571555
g1_Tietze = g1.Tietze()
15581556
g2_Tietze = g2.Tietze()
15591557

@@ -1563,9 +1561,8 @@ def product_on_basis(self, g1, g2):
15631561
# The product is calculated from the corresponding product of the braids
15641562
# ----------------------------------------------------------------------
15651563
braid_group = self.braid_group()
1566-
braid_product = braid_group(g1_Tietze+g2_Tietze)
1567-
result = self._braid_image(braid_product)
1568-
return result
1564+
braid_product = braid_group(g1_Tietze + g2_Tietze)
1565+
return self._braid_image(braid_product)
15691566

15701567
############################################################################
15711568
# --------------------------------------------------------------------------
@@ -2043,8 +2040,7 @@ def _braid_image_from_reduced_powers(self, braid_tietze):
20432040
return self.one()
20442041
k = braid_tietze[0]*len_braid
20452042
result_vect = self._reduce_gen_power(k)
2046-
result = self.from_vector(result_vect)
2047-
return result
2043+
return self.from_vector(result_vect)
20482044

20492045
# ----------------------------------------------------------------------
20502046
# Try to use former calculations (from dynamic library) to obtain the
@@ -2094,8 +2090,7 @@ def _braid_image_from_reduced_powers(self, braid_tietze):
20942090
braid_preimage = tuple(word_result)
20952091
result_vect = self._mult_by_regular_rep(vect, tuple(word_right), RepresentationType.RegularRight, braid_preimage)
20962092

2097-
result = self.from_vector(result_vect)
2098-
return result
2093+
return self.from_vector(result_vect)
20992094

21002095
# --------------------------------------------------------------------------
21012096
# _braid_image_from_former_calculations
@@ -2649,7 +2644,6 @@ def _extend_braid_automorphism(self, element, braid_automorphism):
26492644
sage: CHA2.mirror_isomorphism(br) # indirect doctest
26502645
c^-1
26512646
"""
2652-
26532647
result = self.zero()
26542648
for braid in element.support():
26552649
autom_braid = braid_automorphism(braid)

src/sage/algebras/hecke_algebras/cubic_hecke_base_ring.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ def construction(self):
273273
sage: ER = chbr.CubicHeckeExtensionRing('a, b, c')
274274
sage: ER._test_category() # indirect doctest
275275
"""
276-
return None
276+
return
277277

278278
def __reduce__(self):
279279
r"""

src/sage/algebras/hecke_algebras/cubic_hecke_matrix_rep.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ def construction(self):
708708
sage: MS = c1.matrix().parent()
709709
sage: MS._test_category() # indirect doctest
710710
"""
711-
return None
711+
return
712712

713713
def __reduce__(self):
714714
r"""
@@ -894,8 +894,8 @@ def invert_gen(matr):
894894
matri += cf2 * matr
895895
matri += cf3 * matr**2
896896
d1, d2 = matr.dimensions()
897-
matrI = matrix(original_base_ring, d1, d2, lambda i, j: original_base_ring(matri[i, j]))
898-
return matrI
897+
return matrix(original_base_ring, d1, d2,
898+
lambda i, j: original_base_ring(matri[i, j]))
899899

900900
if n == 2:
901901
if representation_type.is_split():

0 commit comments

Comments
 (0)