Skip to content

Commit 426e5f3

Browse files
author
Release Manager
committed
gh-41230: some typing in homology just adding a few basic typing annotations in the homology folder ### 📝 Checklist - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. URL: #41230 Reported by: Frédéric Chapoton Reviewer(s): Chenxin Zhong
2 parents 31aded8 + 24fe3f2 commit 426e5f3

File tree

9 files changed

+59
-54
lines changed

9 files changed

+59
-54
lines changed

src/sage/homology/chain_complex.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ def ChainComplex(data=None, base_ring=None, grading_group=None,
317317

318318
class Chain_class(ModuleElement):
319319

320-
def __init__(self, parent, vectors, check=True):
320+
def __init__(self, parent, vectors, check=True) -> None:
321321
r"""
322322
A Chain in a Chain Complex.
323323
@@ -370,7 +370,7 @@ def vector(self, degree):
370370
except KeyError:
371371
return self.parent().free_module(degree).zero()
372372

373-
def _repr_(self):
373+
def _repr_(self) -> str:
374374
"""
375375
Print representation.
376376
@@ -510,7 +510,7 @@ def vector_art(d):
510510
concatenated += UnicodeArt([' ... ']) + r
511511
return concatenated
512512

513-
def is_cycle(self):
513+
def is_cycle(self) -> bool:
514514
"""
515515
Return whether the chain is a cycle.
516516
@@ -531,7 +531,7 @@ def is_cycle(self):
531531
return False
532532
return True
533533

534-
def is_boundary(self):
534+
def is_boundary(self) -> bool:
535535
"""
536536
Return whether the chain is a boundary.
537537
@@ -607,7 +607,7 @@ def _lmul_(self, scalar):
607607
parent = self.parent()
608608
return parent.element_class(parent, vectors)
609609

610-
def __eq__(self, other):
610+
def __eq__(self, other) -> bool:
611611
"""
612612
Return ``True`` if this chain is equal to ``other``.
613613
@@ -624,7 +624,7 @@ def __eq__(self, other):
624624
return False
625625
return self._vec == other._vec
626626

627-
def __ne__(self, other):
627+
def __ne__(self, other) -> bool:
628628
"""
629629
Return ``True`` if this chain is not equal to ``other``.
630630
@@ -666,7 +666,8 @@ class ChainComplex_class(Parent):
666666
sage: D
667667
Chain complex with at most 2 nonzero terms over Integer Ring
668668
"""
669-
def __init__(self, grading_group, degree_of_differential, base_ring, differentials):
669+
def __init__(self, grading_group, degree_of_differential, base_ring,
670+
differentials) -> None:
670671
"""
671672
Initialize ``self``.
672673
@@ -690,7 +691,8 @@ def __init__(self, grading_group, degree_of_differential, base_ring, differentia
690691
if any(dim+degree_of_differential not in differentials and d.nrows() != 0
691692
for dim, d in differentials.items()):
692693
raise ValueError('invalid differentials')
693-
if any(dim-degree_of_differential not in differentials and d.ncols() != 0
694+
if any(dim - degree_of_differential not in differentials
695+
and d.ncols() != 0
694696
for dim, d in differentials.items()):
695697
raise ValueError('invalid differentials')
696698
self._grading_group = grading_group
@@ -1059,7 +1061,7 @@ def free_module(self, degree=None):
10591061
rank = self.free_module_rank(degree)
10601062
return FreeModule(self.base_ring(), rank)
10611063

1062-
def __hash__(self):
1064+
def __hash__(self) -> int:
10631065
"""
10641066
The hash is formed by combining the hashes of.
10651067
@@ -1078,7 +1080,7 @@ def __hash__(self):
10781080
^ hash(tuple(self.differential().items()))
10791081
^ hash(self.degree_of_differential()))
10801082

1081-
def __eq__(self, other):
1083+
def __eq__(self, other) -> bool:
10821084
"""
10831085
Return ``True`` iff this chain complex is the same as other: that
10841086
is, if the base rings and the matrices of the two are the
@@ -1109,7 +1111,7 @@ def __eq__(self, other):
11091111
equal = equal and mat.ncols() == 0 and mat.nrows() == 0
11101112
return equal
11111113

1112-
def __ne__(self, other):
1114+
def __ne__(self, other) -> bool:
11131115
"""
11141116
Return ``True`` iff this chain complex is not the same as other.
11151117
@@ -1607,7 +1609,7 @@ def shift(self, n=1):
16071609
return ChainComplex({k-shift: sgn * self._diff[k] for k in self._diff},
16081610
degree_of_differential=deg)
16091611

1610-
def _repr_(self):
1612+
def _repr_(self) -> str:
16111613
"""
16121614
Print representation.
16131615
@@ -1622,7 +1624,7 @@ def _repr_(self):
16221624
s = 'Trivial chain complex'
16231625
else:
16241626
s = 'Chain complex with at most {0} nonzero terms'.format(len(diffs)-1)
1625-
s += ' over {0}'.format(self.base_ring())
1627+
s += f' over {self.base_ring()}'
16261628
return s
16271629

16281630
def _ascii_art_(self):

src/sage/homology/chain_complex_morphism.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class ChainComplexMorphism(Morphism):
9595
"""
9696
An element of this class is a morphism of chain complexes.
9797
"""
98-
def __init__(self, matrices, C, D, check=True):
98+
def __init__(self, matrices, C, D, check=True) -> None:
9999
"""
100100
Create a morphism from a dictionary of matrices.
101101
@@ -515,7 +515,7 @@ def __sub__(self, x):
515515
"""
516516
return self + (-x)
517517

518-
def __eq__(self, x):
518+
def __eq__(self, x) -> bool:
519519
"""
520520
Return ``True`` if and only if ``self == x``.
521521
@@ -581,7 +581,7 @@ def is_surjective(self) -> bool:
581581
m = self.to_matrix()
582582
return m.rank() == m.nrows()
583583

584-
def is_injective(self):
584+
def is_injective(self) -> bool:
585585
"""
586586
Return ``True`` if this map is injective.
587587
@@ -604,7 +604,7 @@ def is_injective(self):
604604
"""
605605
return self.to_matrix().right_nullity() == 0
606606

607-
def __hash__(self):
607+
def __hash__(self) -> int:
608608
"""
609609
TESTS::
610610
@@ -616,7 +616,7 @@ def __hash__(self):
616616
"""
617617
return hash(self.domain()) ^ hash(self.codomain()) ^ hash(tuple(self._matrix_dictionary.items()))
618618

619-
def _repr_type(self):
619+
def _repr_type(self) -> str:
620620
"""
621621
EXAMPLES::
622622

src/sage/homology/chain_homotopy.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class ChainHomotopy(Morphism):
109109
...
110110
ValueError: the data do not define a valid chain homotopy
111111
"""
112-
def __init__(self, matrices, f, g=None):
112+
def __init__(self, matrices, f, g=None) -> None:
113113
r"""
114114
Create a chain homotopy between the given chain maps
115115
from a dictionary of matrices.
@@ -190,7 +190,7 @@ def __init__(self, matrices, f, g=None):
190190
self._g = g
191191
Morphism.__init__(self, Hom(domain, codomain))
192192

193-
def is_algebraic_gradient_vector_field(self):
193+
def is_algebraic_gradient_vector_field(self) -> bool:
194194
r"""
195195
An algebraic gradient vector field is a linear map
196196
`H: C \to C` such that `H H = 0`.
@@ -242,7 +242,7 @@ def is_algebraic_gradient_vector_field(self):
242242
return False
243243
return True
244244

245-
def is_homology_gradient_vector_field(self):
245+
def is_homology_gradient_vector_field(self) -> bool:
246246
r"""
247247
A homology gradient vector field is an algebraic gradient vector
248248
field `H: C \to C` (i.e., a chain homotopy satisfying `H
@@ -345,7 +345,7 @@ def dual(self):
345345
matrices = {i-deg: matrix_dict[i].transpose() for i in matrix_dict}
346346
return ChainHomotopy(matrices, self._f.dual(), self._g.dual())
347347

348-
def __hash__(self):
348+
def __hash__(self) -> int:
349349
"""
350350
TESTS::
351351
@@ -359,7 +359,7 @@ def __hash__(self):
359359
"""
360360
return hash(self._f) ^ hash(self._g) ^ hash(tuple(self._matrix_dictionary.items()))
361361

362-
def _repr_(self):
362+
def _repr_(self) -> str:
363363
"""
364364
String representation.
365365
@@ -422,7 +422,7 @@ class ChainContraction(ChainHomotopy):
422422
....: 1: zero_matrix(ZZ, 1),
423423
....: 2: identity_matrix(ZZ, 1)}, pi, iota)
424424
"""
425-
def __init__(self, matrices, pi, iota):
425+
def __init__(self, matrices, pi, iota) -> None:
426426
r"""
427427
Create a chain contraction from the given data.
428428

src/sage/homology/chains.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
class CellComplexReference:
2929

30-
def __init__(self, cell_complex, degree, cells=None):
30+
def __init__(self, cell_complex, degree, cells=None) -> None:
3131
"""
3232
Auxiliary base class for chains and cochains.
3333
@@ -124,7 +124,8 @@ class Chains(CellComplexReference, CombinatorialFreeModule):
124124
sage: c.eval(z)
125125
6
126126
"""
127-
def __init__(self, cell_complex, degree, cells=None, base_ring=None):
127+
def __init__(self, cell_complex, degree, cells=None,
128+
base_ring=None) -> None:
128129
"""
129130
EXAMPLES::
130131
@@ -264,7 +265,7 @@ def boundary(self):
264265
)
265266
return codomain.from_vector(d * self.to_vector())
266267

267-
def is_cycle(self):
268+
def is_cycle(self) -> bool:
268269
"""
269270
Test whether the chain is a cycle.
270271
@@ -288,7 +289,7 @@ def is_cycle(self):
288289
"""
289290
return self.to_complex().is_cycle()
290291

291-
def is_boundary(self):
292+
def is_boundary(self) -> bool:
292293
"""
293294
Test whether the chain is a boundary.
294295
@@ -351,7 +352,8 @@ class Cochains(CellComplexReference, CombinatorialFreeModule):
351352
sage: c.eval(z)
352353
6
353354
"""
354-
def __init__(self, cell_complex, degree, cells=None, base_ring=None):
355+
def __init__(self, cell_complex, degree, cells=None,
356+
base_ring=None) -> None:
355357
"""
356358
EXAMPLES::
357359
@@ -493,7 +495,7 @@ def coboundary(self):
493495
)
494496
return codomain.from_vector(d * self.to_vector())
495497

496-
def is_cocycle(self):
498+
def is_cocycle(self) -> bool:
497499
"""
498500
Test whether the cochain is a cocycle.
499501
@@ -517,7 +519,7 @@ def is_cocycle(self):
517519
"""
518520
return self.to_complex().is_cycle()
519521

520-
def is_coboundary(self):
522+
def is_coboundary(self) -> bool:
521523
"""
522524
Test whether the cochain is a coboundary.
523525

src/sage/homology/free_resolution.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def __classcall_private__(cls, module, *args, graded=False, degrees=None, shifts
180180
return module.graded_free_resolution(*args, **kwds)
181181
return module.free_resolution(*args, **kwds)
182182

183-
def __init__(self, module, name='S', **kwds):
183+
def __init__(self, module, name='S', **kwds) -> None:
184184
"""
185185
Initialize ``self``.
186186
@@ -206,7 +206,7 @@ def __init__(self, module, name='S', **kwds):
206206
self._name = name
207207
self._module = module
208208

209-
def _repr_(self):
209+
def _repr_(self) -> str:
210210
r"""
211211
Return a string representation of ``self``.
212212
@@ -224,7 +224,7 @@ def _repr_(self):
224224
return f"Free resolution of the row space of the matrix:\n{self._module}"
225225
return f"Free resolution of {self._module}"
226226

227-
def _repr_module(self, i):
227+
def _repr_module(self, i) -> str:
228228
r"""
229229
Return the string form of the `i`-th free module.
230230
@@ -369,7 +369,7 @@ def _length(self):
369369
"""
370370
return len(self._maps)
371371

372-
def _repr_(self):
372+
def _repr_(self) -> str:
373373
"""
374374
Return the string form of this resolution.
375375
@@ -851,7 +851,7 @@ class FiniteFreeResolution_singular(FiniteFreeResolution):
851851
[-y*z + x*w]
852852
[ z^2 - y*w]
853853
"""
854-
def __init__(self, module, name='S', algorithm='heuristic', **kwds):
854+
def __init__(self, module, name='S', algorithm='heuristic', **kwds) -> None:
855855
r"""
856856
Initialize ``self``.
857857

src/sage/homology/hochschild_complex.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class HochschildComplex(UniqueRepresentation, Parent):
8383
- https://ncatlab.org/nlab/show/Hochschild+cohomology
8484
- [Red2001]_
8585
"""
86-
def __init__(self, A, M):
86+
def __init__(self, A, M) -> None:
8787
"""
8888
Initialize ``self``.
8989
@@ -105,7 +105,7 @@ def __init__(self, A, M):
105105
Parent.__init__(self, base=A.base_ring(),
106106
category=ChainComplexes(A.base_ring()))
107107

108-
def _repr_(self):
108+
def _repr_(self) -> str:
109109
"""
110110
Return a string representation of ``self``.
111111
@@ -121,7 +121,7 @@ def _repr_(self):
121121
"""
122122
return "Hochschild complex of {} with coefficients in {}".format(self._A, self._M)
123123

124-
def _latex_(self):
124+
def _latex_(self) -> str:
125125
r"""
126126
Return a latex representation of ``self``.
127127
@@ -570,7 +570,7 @@ class Element(ModuleElement):
570570
sage: H({0: x-y, 2: H.module(2).basis().an_element()})
571571
Chain with 2 nonzero terms over Integer Ring
572572
"""
573-
def __init__(self, parent, vectors):
573+
def __init__(self, parent, vectors) -> None:
574574
"""
575575
Initialize ``self``.
576576
@@ -601,7 +601,7 @@ def vector(self, degree):
601601
except KeyError:
602602
return self.parent().module(degree).zero()
603603

604-
def _repr_(self):
604+
def _repr_(self) -> str:
605605
"""
606606
Print representation.
607607
@@ -725,7 +725,7 @@ def _lmul_(self, scalar):
725725
vectors[d] = vec
726726
return self.__class__(self.parent(), vectors)
727727

728-
def _richcmp_(self, other, op):
728+
def _richcmp_(self, other, op) -> bool:
729729
"""
730730
Rich comparison of ``self`` to ``other``.
731731

src/sage/homology/homology_group.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class HomologyGroup_class(AdditiveAbelianGroup_fixed_gens):
4545
sage: HomologyGroup(100, ZZ)
4646
Z^100
4747
"""
48-
def __init__(self, n, invfac):
48+
def __init__(self, n, invfac) -> None:
4949
"""
5050
See :func:`HomologyGroup` for full documentation.
5151
@@ -62,7 +62,7 @@ def __init__(self, n, invfac):
6262
AdditiveAbelianGroup_fixed_gens.__init__(self, A, B, A.gens())
6363
self._original_invts = invfac
6464

65-
def _repr_(self):
65+
def _repr_(self) -> str:
6666
"""
6767
Print representation of ``self``.
6868

0 commit comments

Comments
 (0)