Skip to content

Commit

Permalink
Trac #32766: fix E713 and E714 in matrix, modules, groups
Browse files Browse the repository at this point in the history
about negative comparison using "is not"

URL: https://trac.sagemath.org/32766
Reported by: chapoton
Ticket author(s): Frédéric Chapoton
Reviewer(s): Matthias Koeppe
  • Loading branch information
Release Manager committed Oct 29, 2021
2 parents f097900 + 9c1d6b4 commit 1012c70
Show file tree
Hide file tree
Showing 15 changed files with 42 additions and 46 deletions.
2 changes: 1 addition & 1 deletion src/sage/groups/abelian_gps/abelian_aut.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def __init__(self, parent, x, check=True):
sage: TestSuite(f).run()
"""
if check:
if not x in parent.gap():
if x not in parent.gap():
raise ValueError("%s is not in the group %s" % (x, parent))
ElementLibGAP.__init__(self, parent, x)

Expand Down
4 changes: 2 additions & 2 deletions src/sage/groups/abelian_gps/abelian_group_gap.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ def exponents(self):
orders = P.gens_orders()
i = 0
for k in range(len(P.gens())):
if not k+1 in Lgens:
if k + 1 not in Lgens:
exp.append(0)
else:
i = Lgens.index(k+1)
i = Lgens.index(k + 1)
exp.append(Lexpo[i] % orders[k])
return tuple(exp)

Expand Down
2 changes: 1 addition & 1 deletion src/sage/groups/fqf_orthogonal.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ def orbits(G, L):
if not automorphisms:
return f
g = ambient(matrix(f))
if not g in G:
if g not in G:
G = B.orthogonal_group(tuple(ambient(s.matrix()) for s in G.gens())+(g,))
waiting = orbits(G, waiting)
continue
Expand Down
4 changes: 1 addition & 3 deletions src/sage/groups/group_exp.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,6 @@ class GroupExp_Class(UniqueRepresentation, Parent):
sage: GroupExp()(QQ)
Multiplicative form of Rational Field
"""
def __init__(self, G):
r"""
Expand All @@ -268,8 +267,7 @@ def __init__(self, G):
sage: TestSuite(EG).run(skip = "_test_elements")
"""

if not G in CommutativeAdditiveGroups():
if G not in CommutativeAdditiveGroups():
raise TypeError("%s must be a commutative additive group" % G)
self._G = G
Parent.__init__(self, category=Groups())
Expand Down
2 changes: 1 addition & 1 deletion src/sage/groups/libgap_morphism.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ def lift(self, h):
ValueError: f2 is not an element of the image of Group endomorphism
of Abelian group with gap, generator orders (2, 4)
"""
if not h in self.codomain():
if h not in self.codomain():
raise TypeError("h (={}) must be an element of the codomain".format(h))
h = self.codomain()(h)
phi = self.gap()
Expand Down
4 changes: 2 additions & 2 deletions src/sage/groups/matrix_gps/isometries.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ def __init__(self, degree, base_ring,
Q = invariant_quotient_module
for f in gens:
self._check_matrix(f)
if (not I is None) and I*f != I:
if (I is not None) and I * f != I:
raise ValueError("the submodule is not preserved")
if not Q is None and (Q.W() != Q.W()*f or Q.V()*f != Q.V()):
if Q is not None and (Q.W() != Q.W()*f or Q.V()*f != Q.V()):
raise ValueError("the quotient module is not preserved")
if len(gens) == 0: # handle the trivial group
gens = [G.parent().identity_matrix()]
Expand Down
2 changes: 1 addition & 1 deletion src/sage/groups/perm_gps/cubegroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ def parse(self, mv, check=True):
return mv if mv.parent() is self else PermutationGroup_generic.__call__(self, mv, check)
elif isinstance(mv, str):
# It is a string: may be in cycle notation or Rubik's notation
if '(' in mv and not '^' in mv:
if '(' in mv and '^' not in mv:
return PermutationGroup_generic.__call__(self, mv, check)
else:
gens = self.gens()
Expand Down
10 changes: 5 additions & 5 deletions src/sage/groups/perm_gps/permgroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -1910,7 +1910,7 @@ def strong_generating_system(self, base_of_group=None, implementation="sage"):
True
"""
if implementation == "gap":
if not base_of_group is None:
if base_of_group is not None:
raise ValueError("the optional argument 'base_of_group'"
" (='%s') must be None if 'implementation'='gap'" % base_of_group)

Expand Down Expand Up @@ -3823,9 +3823,9 @@ def cosets(self, S, side='right'):
from copy import copy
from sage.categories.finite_permutation_groups import FinitePermutationGroups

if not side in ['right','left']:
if side not in ['right', 'left']:
raise ValueError("side should be 'right' or 'left', not %s" % side)
if not S in FinitePermutationGroups():
if S not in FinitePermutationGroups():
raise TypeError("%s is not a permutation group" % S)
if not S.is_subgroup(self):
raise ValueError("%s is not a subgroup of %s" % (S, self))
Expand All @@ -3836,9 +3836,9 @@ def cosets(self, S, side='right'):
while group:
rep = group[0]
if side == 'right':
coset = [e*rep for e in subgroup]
coset = [e * rep for e in subgroup]
if side == 'left':
coset = [rep*e for e in subgroup]
coset = [rep * e for e in subgroup]
for e in coset:
group.remove(e)
decomposition.append(coset)
Expand Down
4 changes: 1 addition & 3 deletions src/sage/groups/perm_gps/permgroup_named.py
Original file line number Diff line number Diff line change
Expand Up @@ -1552,13 +1552,11 @@ def __init__(self, p, m):
AUTHOR:
- Kevin Halasz (2012-8-7)
"""

if not isinstance(p, Integer) or not isinstance(m, Integer):
raise TypeError('both p and m must be integers')

if not p in Primes():
if p not in Primes():
raise ValueError('p must be prime, %s is not prime' % p)

if p == 2 and m <= 3:
Expand Down
2 changes: 1 addition & 1 deletion src/sage/matrix/matrix_integer_dense_saturation.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def random_sublist_of_size(k, n):
w = set([])
while len(w) < n:
z = randrange(k)
if not z in w:
if z not in w:
w.add(z)
return sorted(w)

Expand Down
6 changes: 3 additions & 3 deletions src/sage/modules/free_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,8 +512,8 @@ def VectorSpace(K, dimension_or_basis_keys=None, sparse=False, inner_product_mat
"""
if not K.is_field():
raise TypeError("Argument K (= %s) must be a field." % K)
if not sparse in (True,False):
raise TypeError("Argument sparse (= %s) must be a boolean."%sparse)
if sparse not in (True, False):
raise TypeError("Argument sparse (= %s) must be a boolean." % sparse)
return FreeModule(K, dimension_or_basis_keys, sparse, inner_product_matrix,
with_basis=with_basis, rank=dimension, basis_keys=basis_keys,
**args)
Expand Down Expand Up @@ -4449,7 +4449,7 @@ def linear_dependence(self, vectors, zeros='left', check=True):
"""
if check:
for v in vectors:
if not v in self:
if v not in self:
raise ValueError('vector %s is not an element of %s' % (v, self))
if zeros == 'left':
basis = 'echelon'
Expand Down
8 changes: 3 additions & 5 deletions src/sage/modules/matrix_morphism.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def __init__(self, parent, side='left'):
"""
if not sage.categories.homset.is_Homset(parent):
raise TypeError("parent must be a Hom space")
if not side in ["left", "right"]:
if side not in ["left", "right"]:
raise ValueError("the argument side must be either 'left' or 'right'")
self._side = side
sage.categories.morphism.Morphism.__init__(self, parent)
Expand Down Expand Up @@ -1695,13 +1695,11 @@ def matrix(self, side=None):
...
ValueError: side must be 'left' or 'right', not junk
"""

if not side in ['left', 'right', None]:
if side not in ['left', 'right', None]:
raise ValueError("side must be 'left' or 'right', not {0}".format(side))
if side == self.side() or side is None:
return self._matrix
else:
return self._matrix.transpose()
return self._matrix.transpose()

def is_injective(self):
"""
Expand Down
2 changes: 1 addition & 1 deletion src/sage/modules/vector_space_morphism.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ def linear_transformation(arg0, arg1=None, arg2=None, side='left'):
except ImportError:
Vector_callable_symbolic_dense = ()

if not side in ['left', 'right']:
if side not in ['left', 'right']:
raise ValueError("side must be 'left' or 'right', not {0}".format(side))
if not (is_Matrix(arg0) or is_VectorSpace(arg0)):
raise TypeError('first argument must be a matrix or a vector space, not {0}'.format(arg0))
Expand Down
26 changes: 14 additions & 12 deletions src/sage/modules/with_basis/morphism.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,12 @@ def __init__(self, domain, codomain=None, category=None, affine=False):
sage: TestSuite(phi).run()
"""
if category is None:
if not domain in ModulesWithBasis:
raise ValueError("domain(=%s) should be a module with basis"%(codomain))
if domain not in ModulesWithBasis:
raise ValueError("domain(=%s) should be a module with basis" % codomain)
base_ring = domain.base_ring()

if not hasattr( codomain, 'base_ring' ):
raise ValueError("codomain(=%s) needs to have a base_ring attribute"%(codomain))
if not hasattr(codomain, 'base_ring'):
raise ValueError("codomain(=%s) needs to have a base_ring attribute" % codomain)
# codomain should be a module over base_ring
# The natural test would be ``codomains in Modules(base_ring)``
# But this is not properly implemented yet::
Expand Down Expand Up @@ -227,6 +227,7 @@ def __init__(self, domain, codomain=None, category=None, affine=False):
if not issubclass(self.__class__, H._abstract_element_class):
self.__class__ = H.__make_element_class__(self.__class__)


class ModuleMorphismFromFunction(ModuleMorphism, SetMorphism):
"""
A class for module morphisms implemented by a plain function.
Expand Down Expand Up @@ -264,6 +265,7 @@ def __init__(self, domain, function, codomain=None, category=None):
ModuleMorphism.__init__(self, domain, codomain, category=category)
SetMorphism.__init__(self, self.parent(), function)


class ModuleMorphismByLinearity(ModuleMorphism):
"""
A class for module morphisms obtained by extending a function by linearity.
Expand Down Expand Up @@ -318,7 +320,7 @@ def __init__(self, domain, on_basis=None, codomain=None, category=None,
self._on_basis = on_basis

self._is_module_with_basis_over_same_base_ring = \
codomain in ModulesWithBasis( base_ring ) and zero == codomain.zero()
codomain in ModulesWithBasis(base_ring) and zero == codomain.zero()

ModuleMorphism.__init__(self, domain, codomain,
category=category,
Expand Down Expand Up @@ -954,7 +956,7 @@ def preimage(self, f):
F = self.domain()
G = self.codomain()
on_basis = self.on_basis()
if not f in G:
if f not in G:
raise ValueError("f(={}) must be in the codomain of the morphism to have a preimage under the latter".format(f))

remainder = f
Expand Down Expand Up @@ -1333,17 +1335,17 @@ def __init__(self, domain, matrix, codomain=None, category=None, side="left"):
[2 5]
"""
C = ModulesWithBasis(domain.base_ring()).FiniteDimensional()
if not domain in C:
raise ValueError("The domain %s should be finite dimensional"%domain)
if domain not in C:
raise ValueError("The domain %s should be finite dimensional" % domain)
if codomain is None:
raise ValueError("The codomain %s should be specified")
if not codomain in C:
raise ValueError("The codomain %s should be finite dimensional"%codomain)
if codomain not in C:
raise ValueError("The codomain %s should be finite dimensional" % codomain)
if not is_Matrix(matrix):
raise ValueError("matrix (=%s) should be a matrix"%matrix)
raise ValueError("matrix (=%s) should be a matrix" % matrix)
import sage.combinat.ranker
indices = tuple(domain.basis().keys())
rank_domain = sage.combinat.ranker.rank_from_list(indices)
rank_domain = sage.combinat.ranker.rank_from_list(indices)
if side == "left":
matrix = matrix.transpose()
if matrix.nrows() != len(indices):
Expand Down
10 changes: 5 additions & 5 deletions src/sage/modules/with_basis/subquotient.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
r"""
Quotients of Modules With Basis
"""
#*****************************************************************************
# ****************************************************************************
# Copyright (C) 2010-2015 Florent Hivert <Florent.Hivert@univ-mlv.fr>
#
# Distributed under the terms of the GNU General Public License (GPL)
# http://www.gnu.org/licenses/
#******************************************************************************
# https://www.gnu.org/licenses/
# *****************************************************************************

from sage.misc.cachefunc import cached_method
from sage.sets.family import Family
from sage.combinat.free_module import CombinatorialFreeModule
from sage.misc.lazy_attribute import lazy_attribute
from sage.categories.all import ModulesWithBasis


class QuotientModuleWithBasis(CombinatorialFreeModule):
r"""
A class for quotients of a module with basis by a submodule.
Expand Down Expand Up @@ -376,12 +377,11 @@ def is_submodule(self, other):
return True
if not isinstance(self, SubmoduleWithBasis) and self.ambient() is other.ambient():
raise ValueError("other (=%s) should be a submodule of the same ambient space" % other)
if not self in ModulesWithBasis.FiniteDimensional:
if self not in ModulesWithBasis.FiniteDimensional:
raise NotImplementedError("is_submodule for infinite dimensional modules")
for b in self.basis():
try:
other.retract(b.lift())
except ValueError:
return False
return True

0 comments on commit 1012c70

Please sign in to comment.