Skip to content

Commit c81c2ab

Browse files
author
Release Manager
committed
gh-40235: Annoate methods with `Self` <!-- ^ Please provide a concise and informative title. --> <!-- ^ Don't put issue numbers in the title, do this in the PR description below. --> <!-- ^ For example, instead of "Fixes #12345" use "Introduce new method to calculate 1 + 2". --> <!-- v Describe your changes below in detail. --> <!-- v Why is this change required? What problem does it solve? --> <!-- v If this PR resolves an open issue, please link to it here. For example, "Fixes #12345". --> Wanting to join the annoate-return-types-club, some methods that return `self` are annoted by `typing.Self`. ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [ ] The title is concise and informative. - [ ] The description explains in detail what this PR is about. - [ ] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - #12345: short description why this is a dependency --> <!-- - #34567: ... --> URL: #40235 Reported by: Tobias Diez Reviewer(s): Vincent Macri
2 parents eabf4d9 + c8c9eab commit c81c2ab

17 files changed

+163
-125
lines changed

src/sage/algebras/cellular_basis.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,11 @@
108108
# https://www.gnu.org/licenses/
109109
# ****************************************************************************
110110

111-
from sage.misc.cachefunc import cached_method
112-
from sage.combinat.free_module import CombinatorialFreeModule
111+
from typing import Self
112+
113113
from sage.categories.algebras import Algebras
114+
from sage.combinat.free_module import CombinatorialFreeModule
115+
from sage.misc.cachefunc import cached_method
114116

115117

116118
class CellularBasis(CombinatorialFreeModule):
@@ -283,7 +285,7 @@ def cell_module_indices(self, la):
283285
"""
284286
return self._algebra.cell_module_indices(la)
285287

286-
def cellular_basis(self):
288+
def cellular_basis(self) -> Self:
287289
"""
288290
Return the cellular basis of ``self``, which is ``self``.
289291

src/sage/algebras/lie_algebras/bgg_dual_module.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,22 @@
1616
# http://www.gnu.org/licenses/
1717
#*****************************************************************************
1818

19-
from sage.misc.lazy_attribute import lazy_attribute
20-
from sage.misc.cachefunc import cached_method
19+
from typing import Self
20+
21+
from sage.algebras.lie_algebras.verma_module import ModulePrinting
2122
from sage.categories.enumerated_sets import EnumeratedSets
2223
from sage.categories.monoids import Monoids
23-
from sage.structure.parent import Parent
24-
from sage.structure.indexed_generators import IndexedGenerators
25-
from sage.monoids.indexed_free_monoid import IndexedFreeAbelianMonoid, IndexedMonoid
2624
from sage.combinat.free_module import CombinatorialFreeModule
27-
from sage.sets.family import Family
28-
from sage.sets.finite_enumerated_set import FiniteEnumeratedSet
25+
from sage.data_structures.blas_dict import iaxpy
2926
from sage.matrix.constructor import matrix
27+
from sage.misc.cachefunc import cached_method
28+
from sage.misc.lazy_attribute import lazy_attribute
29+
from sage.monoids.indexed_free_monoid import IndexedFreeAbelianMonoid, IndexedMonoid
3030
from sage.rings.integer_ring import ZZ
31-
from sage.data_structures.blas_dict import iaxpy
32-
from sage.algebras.lie_algebras.verma_module import ModulePrinting
31+
from sage.sets.family import Family
32+
from sage.sets.finite_enumerated_set import FiniteEnumeratedSet
33+
from sage.structure.indexed_generators import IndexedGenerators
34+
from sage.structure.parent import Parent
3335

3436

3537
class BGGDualModule(CombinatorialFreeModule):
@@ -779,7 +781,9 @@ def cardinality(self):
779781
P = Phi.weight_lattice()
780782
coroots = Phi.root_lattice().simple_coroots()
781783
la = P._from_dict({i: weight.scalar(ac) for i, ac in coroots.items()})
782-
from sage.combinat.crystals.monomial_crystals import CrystalOfNakajimaMonomials
784+
from sage.combinat.crystals.monomial_crystals import (
785+
CrystalOfNakajimaMonomials,
786+
)
783787
return CrystalOfNakajimaMonomials(la).cardinality()
784788
from sage.rings.infinity import infinity
785789
return infinity
@@ -979,7 +983,7 @@ def _lift_on_basis(self, m):
979983
raise ValueError(f"{m} does not index a basis element")
980984
return self._indices._basis[m]
981985

982-
def dual(self):
986+
def dual(self) -> Self:
983987
r"""
984988
Return the dual module of ``self``, which is ``self`` since simple
985989
modules are self-dual.

src/sage/categories/additive_magmas.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@
99
# https://www.gnu.org/licenses/
1010
# *****************************************************************************
1111

12-
from sage.misc.lazy_import import LazyImport
13-
from sage.misc.abstract_method import abstract_method
14-
from sage.misc.cachefunc import cached_method
15-
from sage.categories.category_with_axiom import CategoryWithAxiom
16-
from sage.categories.category_singleton import Category_singleton
12+
from typing import Self
13+
1714
from sage.categories.algebra_functor import AlgebrasCategory
1815
from sage.categories.cartesian_product import CartesianProductsCategory
16+
from sage.categories.category_singleton import Category_singleton
17+
from sage.categories.category_with_axiom import CategoryWithAxiom
1918
from sage.categories.homsets import HomsetsCategory
20-
from sage.categories.with_realizations import WithRealizationsCategory
2119
from sage.categories.sets_cat import Sets
20+
from sage.categories.with_realizations import WithRealizationsCategory
21+
from sage.misc.abstract_method import abstract_method
22+
from sage.misc.cachefunc import cached_method
23+
from sage.misc.lazy_import import LazyImport
2224

2325

2426
class AdditiveMagmas(Category_singleton):
@@ -384,8 +386,9 @@ def addition_table(self, names='letters', elements=None):
384386
y| y z x
385387
z| z x y
386388
"""
387-
from sage.matrix.operation_table import OperationTable
388389
import operator
390+
391+
from sage.matrix.operation_table import OperationTable
389392
return OperationTable(self, operation=operator.add,
390393
names=names, elements=elements)
391394

@@ -596,7 +599,7 @@ def extra_super_categories(self):
596599

597600
class AdditiveUnital(CategoryWithAxiom):
598601

599-
def additional_structure(self):
602+
def additional_structure(self) -> Self:
600603
r"""
601604
Return whether ``self`` is a structure category.
602605

src/sage/categories/cartesian_product.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@
1313
# http://www.gnu.org/licenses/
1414
#*****************************************************************************
1515

16-
from sage.misc.lazy_import import lazy_import
17-
from sage.categories.covariant_functorial_construction import CovariantFunctorialConstruction, CovariantConstructionCategory
16+
from typing import Self
17+
18+
from sage.categories.covariant_functorial_construction import (
19+
CovariantConstructionCategory,
20+
CovariantFunctorialConstruction,
21+
)
1822
from sage.categories.pushout import MultivariateConstructionFunctor
23+
from sage.misc.lazy_import import lazy_import
1924

2025
native_python_containers = {tuple, list, set, frozenset, range}
2126

@@ -245,7 +250,7 @@ def _repr_object_names(self):
245250
# This method is only required for the capital `C`
246251
return "Cartesian products of %s" % (self.base_category()._repr_object_names())
247252

248-
def CartesianProducts(self):
253+
def CartesianProducts(self) -> Self:
249254
"""
250255
Return the category of (finite) Cartesian products of objects
251256
of ``self``.

src/sage/categories/category.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,19 +103,24 @@
103103
# ****************************************************************************
104104

105105
import inspect
106+
from typing import Self
106107
from warnings import warn
108+
109+
from sage.categories.category_cy_helper import (
110+
_flatten_categories,
111+
_sort_uniq,
112+
category_sort_key,
113+
join_as_tuple,
114+
)
107115
from sage.misc.abstract_method import abstract_method, abstract_methods_of_class
108-
from sage.misc.cachefunc import cached_method, cached_function
109-
from sage.misc.c3_controlled import _cmp_key, _cmp_key_named, C3_sorted_merge
116+
from sage.misc.c3_controlled import C3_sorted_merge, _cmp_key, _cmp_key_named
117+
from sage.misc.cachefunc import cached_function, cached_method
110118
from sage.misc.lazy_attribute import lazy_attribute
111119
from sage.misc.unknown import Unknown
112120
from sage.misc.weak_dict import WeakValueDictionary
113-
121+
from sage.structure.dynamic_class import DynamicMetaclass, dynamic_class
114122
from sage.structure.sage_object import SageObject
115123
from sage.structure.unique_representation import UniqueRepresentation
116-
from sage.structure.dynamic_class import DynamicMetaclass, dynamic_class
117-
118-
from sage.categories.category_cy_helper import category_sort_key, _sort_uniq, _flatten_categories, join_as_tuple
119124

120125
_join_cache = WeakValueDictionary()
121126

@@ -1045,7 +1050,7 @@ def _super_categories_for_classes(self):
10451050
# Methods handling of full subcategories
10461051
##########################################################################
10471052

1048-
def additional_structure(self):
1053+
def additional_structure(self) -> Self:
10491054
"""
10501055
Return whether ``self`` defines additional structure.
10511056
@@ -2214,7 +2219,7 @@ def _without_axiom(self, axiom):
22142219
else:
22152220
raise ValueError("Cannot remove axiom {} from {}".format(axiom, self))
22162221

2217-
def _without_axioms(self, named=False):
2222+
def _without_axioms(self, named=False) -> Self:
22182223
r"""
22192224
Return the category without the axioms that have been added
22202225
to create it.

src/sage/categories/complete_discrete_valuation.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#**************************************************************************
1111

1212

13+
from typing import Self
14+
1315
from sage.categories.category_singleton import Category_singleton
1416
from sage.categories.discrete_valuation import (
1517
DiscreteValuationFields,
@@ -97,7 +99,7 @@ def denominator(self):
9799
"""
98100
return self.parent()(1)
99101

100-
def numerator(self):
102+
def numerator(self) -> Self:
101103
"""
102104
Return the numerator of this element, normalized in such a
103105
way that `x = x.numerator() / x.denominator()` always holds

src/sage/categories/covariant_functorial_construction.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,15 @@
4242
# Distributed under the terms of the GNU General Public License (GPL)
4343
# http://www.gnu.org/licenses/
4444
#******************************************************************************
45+
from typing import Self
46+
47+
from sage.categories.category import Category
4548
from sage.misc.cachefunc import cached_function, cached_method
4649
from sage.misc.lazy_attribute import lazy_class_attribute
4750
from sage.misc.lazy_import import LazyImport
48-
from sage.categories.category import Category
51+
from sage.structure.dynamic_class import DynamicMetaclass
4952
from sage.structure.sage_object import SageObject
5053
from sage.structure.unique_representation import UniqueRepresentation
51-
from sage.structure.dynamic_class import DynamicMetaclass
5254

5355

5456
class CovariantFunctorialConstruction(UniqueRepresentation, SageObject):
@@ -624,7 +626,7 @@ def is_construction_defined_by_base(self):
624626
f = self._functor_category
625627
return not any(hasattr(C, f) for C in base.super_categories())
626628

627-
def additional_structure(self):
629+
def additional_structure(self) -> Self | None:
628630
r"""
629631
Return the additional structure defined by ``self``.
630632

src/sage/categories/dedekind_domains.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
# Distributed under the terms of the GNU General Public License (GPL)
77
# https://www.gnu.org/licenses/
88
# *****************************************************************************
9+
from typing import Self
10+
911
from sage.categories.category import Category
1012
from sage.categories.integral_domains import IntegralDomains
1113

@@ -86,7 +88,7 @@ def is_integrally_closed(self) -> bool:
8688
"""
8789
return True
8890

89-
def integral_closure(self):
91+
def integral_closure(self) -> Self:
9092
r"""
9193
Return ``self`` since Dedekind domains are integrally closed.
9294

0 commit comments

Comments
 (0)