From ab8f9c64cc38def1b3c8338c1e38b6290894606d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Chapoton?= Date: Fri, 7 Nov 2025 18:01:15 +0100 Subject: [PATCH] some type annotations in algebras,coding,graphs --- src/sage/algebras/clifford_algebra.py | 16 ++++++------- .../finite_dimensional_algebra_ideal.py | 4 ++-- .../letterplace/letterplace_ideal.pyx | 4 ++-- .../lie_algebras/affine_lie_algebra.py | 10 ++++---- .../algebras/lie_algebras/bgg_dual_module.py | 8 +++---- src/sage/algebras/lie_algebras/subalgebra.py | 8 ++++--- .../algebras/quatalg/quaternion_algebra.py | 23 ++++++++++--------- .../algebras/steenrod/steenrod_algebra.py | 2 +- src/sage/coding/abstract_code.py | 4 ++-- src/sage/coding/cyclic_code.py | 18 +++++++-------- src/sage/coding/linear_code_no_metric.py | 15 ++++++------ src/sage/graphs/independent_sets.pyx | 4 ++-- src/sage/graphs/pq_trees.py | 6 ++--- src/sage/graphs/views.pyx | 12 +++++----- 14 files changed, 69 insertions(+), 65 deletions(-) diff --git a/src/sage/algebras/clifford_algebra.py b/src/sage/algebras/clifford_algebra.py index 945db75f991..cffa86d0c17 100644 --- a/src/sage/algebras/clifford_algebra.py +++ b/src/sage/algebras/clifford_algebra.py @@ -51,7 +51,7 @@ class CliffordAlgebraIndices(UniqueRepresentation, Parent): A facade parent for the indices of Clifford algebra. Users should not create instances of this class directly. """ - def __init__(self, Qdim, degree=None): + def __init__(self, Qdim, degree=None) -> None: r""" Initialize ``self``. @@ -271,7 +271,7 @@ def __iter__(self): yield FrozenBitset(C) k += 1 - def __contains__(self, elt): + def __contains__(self, elt) -> bool: r""" Check containment of ``elt`` in ``self``. @@ -503,7 +503,7 @@ def __classcall_private__(cls, Q, names=None): raise ValueError("the number of variables does not match the number of generators") return super().__classcall__(cls, Q, names) - def __init__(self, Q, names, category=None): + def __init__(self, Q, names, category=None) -> None: r""" Initialize ``self``. @@ -1463,7 +1463,7 @@ def __classcall_private__(cls, R, names=None, n=None): raise ValueError("the number of variables does not match the number of generators") return super().__classcall__(cls, R, names) - def __init__(self, R, names): + def __init__(self, R, names) -> None: """ Initialize ``self``. @@ -2124,7 +2124,7 @@ def __classcall__(cls, E, s_coeff): from sage.sets.family import Family return super().__classcall__(cls, E, Family(d)) - def __init__(self, E, s_coeff): + def __init__(self, E, s_coeff) -> None: """ Initialize ``self``. @@ -2538,7 +2538,7 @@ class ExteriorAlgebraCoboundary(ExteriorAlgebraDifferential): - :wikipedia:`Exterior_algebra#Differential_geometry` """ - def __init__(self, E, s_coeff): + def __init__(self, E, s_coeff) -> None: """ Initialize ``self``. @@ -2733,7 +2733,7 @@ class ExteriorAlgebraIdeal(Ideal_nc): sage: xbar * ybar 0 """ - def __init__(self, ring, gens, coerce=True, side='twosided'): + def __init__(self, ring, gens, coerce=True, side='twosided') -> None: """ Initialize ``self``. @@ -2815,7 +2815,7 @@ def _contains_(self, f): """ return not self.reduce(f) - def __richcmp__(self, other, op): + def __richcmp__(self, other, op) -> bool: """ Compare ``self`` and ``other``. diff --git a/src/sage/algebras/finite_dimensional_algebras/finite_dimensional_algebra_ideal.py b/src/sage/algebras/finite_dimensional_algebras/finite_dimensional_algebra_ideal.py index a25b5bdb4f1..19b76c8a5e4 100644 --- a/src/sage/algebras/finite_dimensional_algebras/finite_dimensional_algebra_ideal.py +++ b/src/sage/algebras/finite_dimensional_algebras/finite_dimensional_algebra_ideal.py @@ -47,7 +47,7 @@ class FiniteDimensionalAlgebraIdeal(Ideal_generic): sage: A.ideal(A([0,1])) Ideal (e1) of Finite-dimensional algebra of degree 2 over Finite Field of size 3 """ - def __init__(self, A, gens=None, given_by_matrix=False): + def __init__(self, A, gens=None, given_by_matrix=False) -> bool: """ EXAMPLES:: @@ -138,7 +138,7 @@ def _richcmp_(self, other, op): elif op == op_GE or op == op_GT: return other.vector_space().is_subspace(self.vector_space()) - def __contains__(self, elt): + def __contains__(self, elt) -> bool: """ EXAMPLES:: diff --git a/src/sage/algebras/letterplace/letterplace_ideal.pyx b/src/sage/algebras/letterplace/letterplace_ideal.pyx index b504b237582..b80446cdbe4 100644 --- a/src/sage/algebras/letterplace/letterplace_ideal.pyx +++ b/src/sage/algebras/letterplace/letterplace_ideal.pyx @@ -155,7 +155,7 @@ class LetterplaceIdeal(Ideal_nc): sage: (z*I.0-x*y*z).normal_form(I) -y*x*z + z*z """ - def __init__(self, ring, gens, coerce=True, side='twosided'): + def __init__(self, ring, gens, coerce=True, side='twosided') -> None: """ INPUT: @@ -334,7 +334,7 @@ class LetterplaceIdeal(Ideal_nc): self.__GB.__uptodeg = degbound return self.__GB - def __contains__(self, x): + def __contains__(self, x) -> bool: """ The containment test is based on a normal form computation. diff --git a/src/sage/algebras/lie_algebras/affine_lie_algebra.py b/src/sage/algebras/lie_algebras/affine_lie_algebra.py index 02fefb8e623..43eea05c7bf 100644 --- a/src/sage/algebras/lie_algebras/affine_lie_algebra.py +++ b/src/sage/algebras/lie_algebras/affine_lie_algebra.py @@ -106,7 +106,7 @@ def __classcall_private__(cls, arg0, cartan_type=None, kac_moody=True): return UntwistedAffineLieAlgebra(g, kac_moody=kac_moody) return TwistedAffineLieAlgebra(arg0, cartan_type, kac_moody=kac_moody) - def __init__(self, g, cartan_type, names, kac_moody): + def __init__(self, g, cartan_type, names, kac_moody) -> None: """ Initialize ``self``. @@ -578,7 +578,7 @@ class UntwistedAffineLieAlgebra(AffineLieAlgebra): sage: D.d() 0 """ - def __init__(self, g, kac_moody): + def __init__(self, g, kac_moody) -> None: """ Initialize ``self``. @@ -671,7 +671,7 @@ class TwistedAffineLieAlgebra(AffineLieAlgebra): weights in this representation with the roots of type `B_n` and the double all of its short roots. """ - def __init__(self, R, cartan_type, kac_moody): + def __init__(self, R, cartan_type, kac_moody) -> None: """ Initialize ``self``. @@ -1107,7 +1107,7 @@ def __classcall_private__(cls, cartan_type): raise ValueError("the Cartan type must be a twisted affine type") return super().__classcall__(cls, cartan_type) - def __init__(self, cartan_type): + def __init__(self, cartan_type) -> None: """ Initialize ``self``. @@ -1141,7 +1141,7 @@ def __init__(self, cartan_type): from sage.categories.infinite_enumerated_sets import InfiniteEnumeratedSets super().__init__(facade=facade, category=InfiniteEnumeratedSets()) - def __contains__(self, x): + def __contains__(self, x) -> bool: """ Return if ``x`` is contained in ``self``. diff --git a/src/sage/algebras/lie_algebras/bgg_dual_module.py b/src/sage/algebras/lie_algebras/bgg_dual_module.py index eb6814a5d7a..a3c7e4a3ce9 100644 --- a/src/sage/algebras/lie_algebras/bgg_dual_module.py +++ b/src/sage/algebras/lie_algebras/bgg_dual_module.py @@ -100,7 +100,7 @@ class BGGDualModule(CombinatorialFreeModule): - [Humphreys08]_ """ - def __init__(self, module): + def __init__(self, module) -> None: r""" Initialize ``self``. @@ -458,7 +458,7 @@ def __classcall__(cls, simple, prefix='f', **kwds): """ return super(IndexedMonoid, cls).__classcall__(cls, simple, prefix=prefix, **kwds) - def __init__(self, simple, prefix, category=None, **kwds): + def __init__(self, simple, prefix, category=None, **kwds) -> None: r""" Initialize ``self``. @@ -591,7 +591,7 @@ def weight_space_basis(self, mu): self._construct_next_level() return self._weight_space_bases.get(mu, []) - def __contains__(self, m): + def __contains__(self, m) -> bool: r""" Check if ``m`` is contained in ``self``. @@ -815,7 +815,7 @@ def __classcall_private__(cls, g, weight, *args, **kwds): return FiniteDimensionalSimpleModule(g, weight, *args, **kwds) return super().__classcall__(cls, g, weight, *args, **kwds) - def __init__(self, g, weight, prefix='f', basis_key=None, **kwds): + def __init__(self, g, weight, prefix='f', basis_key=None, **kwds) -> None: r""" Initialize ``self``. diff --git a/src/sage/algebras/lie_algebras/subalgebra.py b/src/sage/algebras/lie_algebras/subalgebra.py index a4b535a3b32..cd1e42557ef 100644 --- a/src/sage/algebras/lie_algebras/subalgebra.py +++ b/src/sage/algebras/lie_algebras/subalgebra.py @@ -260,7 +260,8 @@ def __classcall_private__(cls, ambient, gens, ideal_of=None, return super().__classcall__(cls, ambient, gens, ideal_of, order, category) - def __init__(self, ambient, gens, ideal_of, order=None, category=None): + def __init__(self, ambient, gens, ideal_of, + order=None, category=None) -> None: r""" Initialize ``self``. @@ -302,7 +303,7 @@ def __init__(self, ambient, gens, ideal_of, order=None, category=None): f = SetMorphism(H, self.lift) ambient.register_coercion(f) - def __contains__(self, x): + def __contains__(self, x) -> bool: r""" Return ``True`` if ``x`` is an element of ``self``. @@ -338,7 +339,8 @@ def __getitem__(self, x): If `x` is a pair `(a, b)`, return the Lie bracket `[a, b]`. Otherwise try to return the `x`-th element of ``self``. - This replicates the convenience syntax for Lie brackets of Lie algebras. + This replicates the convenience syntax for Lie brackets + of Lie algebras. EXAMPLES:: diff --git a/src/sage/algebras/quatalg/quaternion_algebra.py b/src/sage/algebras/quatalg/quaternion_algebra.py index 8b75f3ed19f..7c0b0b287b7 100644 --- a/src/sage/algebras/quatalg/quaternion_algebra.py +++ b/src/sage/algebras/quatalg/quaternion_algebra.py @@ -823,7 +823,7 @@ class QuaternionAlgebra_ab(QuaternionAlgebra_abstract): sage: QuaternionAlgebra(QQ, -7, -21) # indirect doctest Quaternion Algebra (-7, -21) with base ring Rational Field """ - def __init__(self, base_ring, a, b, names='i,j,k'): + def __init__(self, base_ring, a, b, names='i,j,k') -> None: """ Create the quaternion algebra with `i^2 = a`, `j^2 = b`, and `ij = -ji = k`. @@ -1234,7 +1234,7 @@ def invariants(self): """ return self._a, self._b - def __eq__(self, other): + def __eq__(self, other) -> bool: """ Compare ``self`` and ``other``. @@ -1250,7 +1250,7 @@ def __eq__(self, other): return (self.base_ring() == other.base_ring() and (self._a, self._b) == (other._a, other._b)) - def __ne__(self, other): + def __ne__(self, other) -> bool: """ Compare ``self`` and ``other``. @@ -1263,7 +1263,7 @@ def __ne__(self, other): """ return not self.__eq__(other) - def __hash__(self): + def __hash__(self) -> int: """ Compute the hash of ``self``. @@ -1985,7 +1985,7 @@ class QuaternionOrder(Parent): sage: type(QuaternionAlgebra(-1,-7).maximal_order()) """ - def __init__(self, A, basis, check=True): + def __init__(self, A, basis, check=True) -> None: """ INPUT: @@ -2186,7 +2186,7 @@ def gen(self, n): """ return self.__basis[n] - def __richcmp__(self, other, op): + def __richcmp__(self, other, op) -> bool: """ Compare this quaternion order to ``other``. @@ -2239,7 +2239,7 @@ def __richcmp__(self, other, op): return op == op_NE return richcmp(self.unit_ideal(), other.unit_ideal(), op) - def __hash__(self): + def __hash__(self) -> int: """ Compute the hash of ``self``. @@ -3013,7 +3013,8 @@ class QuaternionFractionalIdeal_rational(QuaternionFractionalIdeal): - ``check`` -- boolean (default: ``True``); if ``False``, do no type checking. """ - def __init__(self, Q, basis, left_order=None, right_order=None, check=True): + def __init__(self, Q, basis, left_order=None, + right_order=None, check=True) -> None: """ EXAMPLES:: @@ -3267,7 +3268,7 @@ def right_order(self): self.__right_order = self._compute_order(side='right') return self.__right_order - def __repr__(self): + def __repr__(self) -> str: """ Return string representation of this quaternion fractional ideal. @@ -3361,7 +3362,7 @@ def _richcmp_(self, right, op): """ return self.free_module().__richcmp__(right.free_module(), op) - def __hash__(self): + def __hash__(self) -> int: """ Return the hash of ``self``. @@ -4165,7 +4166,7 @@ def is_principal(self, certificate=False): # find an element of minimal norm in self; see [Piz1980]_, Corollary 1.20. return True, self.minimal_element() - def __contains__(self, x): + def __contains__(self, x) -> bool: """ Return whether ``x`` is in ``self``. diff --git a/src/sage/algebras/steenrod/steenrod_algebra.py b/src/sage/algebras/steenrod/steenrod_algebra.py index 7aa7035034f..12752243744 100644 --- a/src/sage/algebras/steenrod/steenrod_algebra.py +++ b/src/sage/algebras/steenrod/steenrod_algebra.py @@ -2129,7 +2129,7 @@ def _element_constructor_(self, x): return a.change_basis(self.basis_name()) raise ValueError("element does not lie in this Steenrod algebra") - def __contains__(self, x): + def __contains__(self, x) -> bool: r""" Return ``True`` if ``self`` contains `x`. diff --git a/src/sage/coding/abstract_code.py b/src/sage/coding/abstract_code.py index 3d1fe305dbc..1ee2f922016 100644 --- a/src/sage/coding/abstract_code.py +++ b/src/sage/coding/abstract_code.py @@ -203,7 +203,7 @@ class AbstractCode(Parent): """ def __init__(self, length, default_encoder_name=None, - default_decoder_name=None, metric='Hamming'): + default_decoder_name=None, metric='Hamming') -> None: r""" Initialize mandatory parameters that any code shares. @@ -348,7 +348,7 @@ def __iter__(self): """ raise RuntimeError("Please override __iter__ in the implementation of {}".format(self.parent())) - def __contains__(self, c): + def __contains__(self, c) -> bool: r""" Return an error message requiring to override ``__contains__`` in ``self``. diff --git a/src/sage/coding/cyclic_code.py b/src/sage/coding/cyclic_code.py index 80e8d68ef30..bd9017d346a 100644 --- a/src/sage/coding/cyclic_code.py +++ b/src/sage/coding/cyclic_code.py @@ -288,7 +288,7 @@ class CyclicCode(AbstractLinearCode): _registered_decoders = {} def __init__(self, generator_pol=None, length=None, code=None, check=True, - D=None, field=None, primitive_root=None): + D=None, field=None, primitive_root=None) -> None: r""" TESTS: @@ -462,7 +462,7 @@ def __init__(self, generator_pol=None, length=None, code=None, check=True, "of powers and the length and the field, or " "a generator polynomial and the code length") - def __contains__(self, word): + def __contains__(self, word) -> bool: r""" Return ``True`` if ``word`` belongs to ``self``, ``False`` otherwise. @@ -484,7 +484,7 @@ def __contains__(self, word): R = self._polynomial_ring return (g.divides(R(word.list())) and word in self.ambient_space()) - def __eq__(self, other): + def __eq__(self, other) -> bool: r""" Test equality between CyclicCode objects. @@ -836,7 +836,7 @@ class CyclicCodePolynomialEncoder(Encoder): Polynomial-style encoder for [7, 4] Cyclic Code over GF(2) """ - def __init__(self, code): + def __init__(self, code) -> None: r""" EXAMPLES:: @@ -853,7 +853,7 @@ def __init__(self, code): self._polynomial_ring = code._polynomial_ring super().__init__(code) - def __eq__(self, other): + def __eq__(self, other) -> bool: r""" Test equality between CyclicCodePolynomialEncoder objects. @@ -1005,7 +1005,7 @@ class CyclicCodeVectorEncoder(Encoder): Vector-style encoder for [7, 4] Cyclic Code over GF(2) """ - def __init__(self, code): + def __init__(self, code) -> None: r""" EXAMPLES:: @@ -1023,7 +1023,7 @@ def __init__(self, code): self._polynomial_ring = code._polynomial_ring super().__init__(code) - def __eq__(self, other): + def __eq__(self, other) -> bool: r""" Test equality between CyclicCodeVectorEncoder objects. @@ -1198,7 +1198,7 @@ class CyclicCodeSurroundingBCHDecoder(Decoder): sage: D Decoder through the surrounding BCH code of the [15, 10] Cyclic Code over GF(16) """ - def __init__(self, code, **kwargs): + def __init__(self, code, **kwargs) -> None: r""" EXAMPLES:: @@ -1213,7 +1213,7 @@ def __init__(self, code, **kwargs): self._decoder_type = copy(self._bch_decoder.decoder_type()) super().__init__(code, code.ambient_space(), "Vector") - def __eq__(self, other): + def __eq__(self, other) -> bool: r""" Test equality between CyclicCodeSurroundingBCHDecoder objects. diff --git a/src/sage/coding/linear_code_no_metric.py b/src/sage/coding/linear_code_no_metric.py index d357c6ac7e7..c5e1f372fac 100644 --- a/src/sage/coding/linear_code_no_metric.py +++ b/src/sage/coding/linear_code_no_metric.py @@ -138,7 +138,8 @@ class AbstractLinearCodeNoMetric(AbstractCode, Module): _registered_encoders = {} _registered_decoders = {} - def __init__(self, base_field, length, default_encoder_name, default_decoder_name, metric='Hamming'): + def __init__(self, base_field, length, default_encoder_name, + default_decoder_name, metric='Hamming') -> None: """ Initialize mandatory parameters that any linear code shares. @@ -244,7 +245,7 @@ def generator_matrix(self, encoder_name=None, **kwargs): E = self.encoder(encoder_name, **kwargs) return E.generator_matrix() - def __eq__(self, other): + def __eq__(self, other) -> bool: r""" Test equality between two linear codes. @@ -280,7 +281,7 @@ def __eq__(self, other): # the worst case does only one system solving. return all(c in self for c in other.gens()) - def __ne__(self, other): + def __ne__(self, other) -> bool: r""" Test inequality of ``self`` and ``other``. @@ -490,7 +491,7 @@ def syndrome(self, r): """ return self.parity_check_matrix()*r - def __contains__(self, v): + def __contains__(self, v) -> bool: r""" Return ``True`` if `v` can be coerced into ``self``. Otherwise, return ``False``. @@ -818,7 +819,7 @@ def __getitem__(self, i): codeword.set_immutable() return codeword - def __hash__(self): + def __hash__(self) -> int: r""" Return the hash value of ``self``. @@ -1112,7 +1113,7 @@ class LinearCodeSystematicEncoder(Encoder): if LinearCodeSystematicEncoder is the default encoder """ - def __init__(self, code, systematic_positions=None): + def __init__(self, code, systematic_positions=None) -> None: r""" EXAMPLES:: @@ -1134,7 +1135,7 @@ def __init__(self, code, systematic_positions=None): # Test that the systematic positions are an information set self.generator_matrix() - def __eq__(self, other): + def __eq__(self, other) -> bool: r""" Test equality between LinearCodeSystematicEncoder objects. diff --git a/src/sage/graphs/independent_sets.pyx b/src/sage/graphs/independent_sets.pyx index e46921b2c82..d416ec4ba9f 100644 --- a/src/sage/graphs/independent_sets.pyx +++ b/src/sage/graphs/independent_sets.pyx @@ -127,7 +127,7 @@ cdef class IndependentSets: ... ValueError: a is not a vertex of the graph """ - def __init__(self, G, maximal=False, complement=False): + def __init__(self, G, maximal=False, complement=False) -> None: r""" Constructor for this class. @@ -328,7 +328,7 @@ cdef class IndependentSets: from sage.rings.integer import Integer return Integer(i) - def __contains__(self, S): + def __contains__(self, S) -> bool: r""" Check whether the set is an independent set (possibly maximal). diff --git a/src/sage/graphs/pq_trees.py b/src/sage/graphs/pq_trees.py index 998e35fd845..869f9f909a4 100644 --- a/src/sage/graphs/pq_trees.py +++ b/src/sage/graphs/pq_trees.py @@ -285,7 +285,7 @@ class PQ: AUTHOR : Nathann Cohen """ - def __init__(self, seq): + def __init__(self, seq) -> None: r""" Construction of a PQ-Tree. @@ -329,7 +329,7 @@ def reverse(self): self._children.reverse() - def __contains__(self, v): + def __contains__(self, v) -> bool: r""" Test whether there exists an element of ``self`` containing an element ``v``. @@ -378,7 +378,7 @@ def number_of_children(self): """ return len(self._children) - def ordering(self): + def ordering(self) -> list: r""" Return the current ordering given by listing the leaves from left to right. diff --git a/src/sage/graphs/views.pyx b/src/sage/graphs/views.pyx index 8dd73c928d4..d1ee421ac3c 100644 --- a/src/sage/graphs/views.pyx +++ b/src/sage/graphs/views.pyx @@ -342,7 +342,7 @@ cdef class EdgesView: def __init__(self, G, vertices=None, vertices2=None, labels=True, ignore_direction=False, - sort=False, key=None, sort_vertices=True): + sort=False, key=None, sort_vertices=True) -> None: """ Construction of this :class:`EdgesView`. @@ -385,7 +385,7 @@ cdef class EdgesView: self._sort_edges_key = key self._sort_vertices = sort_vertices - def __len__(self): + def __len__(self) -> int: """ Return the number of edges in ``self``. @@ -414,7 +414,7 @@ cdef class EdgesView: return self._graph.size() return sum(1 for _ in self) - def __repr__(self): + def __repr__(self) -> str: """ Return a string representation of ``self``. @@ -499,7 +499,7 @@ cdef class EdgesView: else: yield from self._iter_unsorted() - def __bool__(self): + def __bool__(self) -> bool: """ Check whether ``self`` is not empty. @@ -517,7 +517,7 @@ cdef class EdgesView: return True return False - def __eq__(self, right): + def __eq__(self, right) -> bool: """ Check whether ``self`` and ``right`` are equal. @@ -587,7 +587,7 @@ cdef class EdgesView: # Check that the same edges are reported in the same order return all(es == eo for es, eo in zip(self, other)) - def __contains__(self, e): + def __contains__(self, e) -> bool: """ Check whether edge ``e`` is part of ``self``.