Skip to content

Commit c7768dd

Browse files
committed
Add type annotations for rings and parents
1 parent 5c8d9e9 commit c7768dd

File tree

9 files changed

+561
-31
lines changed

9 files changed

+561
-31
lines changed

src/sage/categories/map.pyi

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
from __future__ import annotations
2+
3+
from typing import Any, Callable, Generic, Iterator, Mapping, Self, Sequence, TypeVar
4+
5+
from sage.categories.category import Category
6+
from sage.structure.element import Element
7+
from sage.structure.parent import Parent
8+
9+
DomainElementT_contra = TypeVar("DomainElementT_contra", contravariant=True)
10+
CodomainElementT_co = TypeVar("CodomainElementT_co", covariant=True)
11+
SectionDomainT = TypeVar("SectionDomainT")
12+
SectionCodomainT = TypeVar("SectionCodomainT")
13+
CompositeDomainT = TypeVar("CompositeDomainT")
14+
CompositeCodomainT = TypeVar("CompositeCodomainT")
15+
16+
17+
class Map(Element, Generic[DomainElementT_contra, CodomainElementT_co]):
18+
_coerce_cost: int
19+
_is_coercion: bool
20+
_repr_type_str: str | None
21+
_category_for: Category | None
22+
domain: Callable[[], Parent[Any] | None]
23+
codomain: Callable[[], Parent[Any]]
24+
_codomain: Parent[Any]
25+
26+
def __init__(self, parent: Any, codomain: Parent[Any] | None = ...) -> None: ...
27+
def __copy__(self) -> Self: ...
28+
def parent(self) -> Any: ...
29+
def _make_weak_references(self) -> None: ...
30+
def _make_strong_references(self) -> None: ...
31+
def _update_slots(self, slots: Mapping[str, Any]) -> None: ...
32+
def _update_slots_test(self, _slots: Mapping[str, Any]) -> None: ...
33+
def _extra_slots(self) -> dict[str, Any]: ...
34+
def _extra_slots_test(self) -> dict[str, Any]: ...
35+
def __reduce__(self) -> tuple[Any, tuple[Any, ...]]: ...
36+
def _repr_type(self) -> str: ...
37+
def _repr_defn(self) -> str: ...
38+
def _repr_(self) -> str: ...
39+
def _default_repr_(self) -> str: ...
40+
def domains(self) -> Iterator[Parent[Any] | None]: ...
41+
def category_for(self) -> Category: ...
42+
def __call__(self, x: DomainElementT_contra, *args: Any, **kwds: Any) -> CodomainElementT_co: ...
43+
def _call_(self, x: DomainElementT_contra) -> CodomainElementT_co: ...
44+
def _call_with_args(
45+
self,
46+
x: DomainElementT_contra,
47+
args: Sequence[Any] = ...,
48+
kwds: Mapping[str, Any] | None = ...,
49+
) -> CodomainElementT_co: ...
50+
def __mul__(self, right: Map[Any, Any]) -> Map[Any, Any]: ...
51+
def _composition(self, right: Map[Any, Any]) -> Map[Any, Any]: ...
52+
def _composition_(self, right: Map[Any, Any], homset: Any) -> Map[Any, Any]: ...
53+
def pre_compose(self, right: Map[Any, Any]) -> Map[Any, Any]: ...
54+
def post_compose(self, left: Map[Any, Any]) -> Map[Any, Any]: ...
55+
def extend_domain(self, new_domain: Any) -> Map[Any, Any]: ...
56+
def extend_codomain(self, new_codomain: Any) -> Map[Any, Any]: ...
57+
def is_surjective(self) -> bool: ...
58+
def _pow_int(self, n: int) -> Map[Any, Any]: ...
59+
def section(self) -> Map[Any, Any] | None: ...
60+
def __hash__(self) -> int: ...
61+
62+
63+
class Section(Map[SectionCodomainT, SectionDomainT], Generic[SectionDomainT, SectionCodomainT]):
64+
_inverse: Map[SectionDomainT, SectionCodomainT]
65+
66+
def __init__(self, map: Map[SectionDomainT, SectionCodomainT]) -> None: ...
67+
def _extra_slots(self) -> dict[str, Any]: ...
68+
def _update_slots(self, slots: Mapping[str, Any]) -> None: ...
69+
def _repr_type(self) -> str: ...
70+
def inverse(self) -> Map[SectionDomainT, SectionCodomainT]: ...
71+
72+
73+
class FormalCompositeMap(
74+
Map[CompositeDomainT, CompositeCodomainT],
75+
Generic[CompositeDomainT, CompositeCodomainT],
76+
):
77+
__list: Sequence[Map[Any, Any]]
78+
79+
def __init__(
80+
self,
81+
parent: Any,
82+
first: Map[Any, Any] | Sequence[Map[Any, Any]],
83+
second: Map[Any, Any] | None = ...,
84+
) -> None: ...
85+
def __copy__(self) -> Self: ...
86+
def _update_slots(self, slots: Mapping[str, Any]) -> None: ...
87+
def _extra_slots(self) -> dict[str, Any]: ...
88+
def __richcmp__(self, other: Any, op: int) -> bool: ...
89+
def __hash__(self) -> int: ...
90+
def __getitem__(self, i: int) -> Map[Any, Any]: ...
91+
def _call_(self, x: CompositeDomainT) -> CompositeCodomainT: ...
92+
def _call_with_args(
93+
self,
94+
x: CompositeDomainT,
95+
args: Sequence[Any] = ...,
96+
kwds: Mapping[str, Any] | None = ...,
97+
) -> CompositeCodomainT: ...
98+
def _repr_type(self) -> str: ...
99+
def _repr_defn(self) -> str: ...
100+
def first(self) -> Map[Any, Any]: ...
101+
def then(self) -> Map[Any, Any]: ...
102+
def is_injective(self) -> bool: ...
103+
def is_surjective(self) -> bool: ...
104+
def domains(self) -> Iterator[Parent[Any] | None]: ...
105+
def section(self) -> Map[Any, Any] | None: ...
106+
107+
108+
def unpickle_map(
109+
_class: type[Map[Any, Any]],
110+
parent: Any,
111+
_dict: Mapping[str, Any],
112+
_slots: Mapping[str, Any],
113+
) -> Map[Any, Any]: ...
114+
115+
116+
def is_Map(x: Any) -> bool: ...
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from typing import Any
21

3-
from sage.structure.element import Element
42
from sage.categories.map import Map
3+
from sage.rings.complex_double import ComplexDoubleElement
4+
from sage.rings.complex_mpfr import ComplexNumber
55

6-
class CCtoCDF(Map):
7-
def _call_(self, x: Element) -> Element:
6+
class CCtoCDF(Map[ComplexNumber, ComplexDoubleElement]):
7+
def _call_(self, x: ComplexNumber) -> ComplexDoubleElement:
88
...

src/sage/rings/complex_interval_field.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@
3434
# (at your option) any later version.
3535
# https://www.gnu.org/licenses/
3636
# ****************************************************************************
37-
37+
from __future__ import annotations
3838

3939
import weakref
40+
from typing import Literal
4041

4142
import sage.rings.abc
4243
from sage.misc.cachefunc import cached_method
@@ -48,10 +49,10 @@
4849
from sage.rings.ring import Field
4950
from sage.structure.parent import Parent
5051

51-
cache = {}
52+
cache: dict[int, weakref.ReferenceType[ComplexIntervalField_class]] = {}
5253

5354

54-
def ComplexIntervalField(prec=53, names=None):
55+
def ComplexIntervalField(prec=53, names=None) -> ComplexIntervalField_class:
5556
"""
5657
Return the complex interval field with real and imaginary parts having
5758
``prec`` *bits* of precision.
@@ -182,7 +183,7 @@ class ComplexIntervalField_class(sage.rings.abc.ComplexIntervalField):
182183
"""
183184
Element = complex_interval.ComplexIntervalFieldElement
184185

185-
def __init__(self, prec=53):
186+
def __init__(self, prec=53) -> None:
186187
"""
187188
Initialize ``self``.
188189
@@ -243,7 +244,7 @@ def construction(self):
243244
from sage.categories.pushout import AlgebraicClosureFunctor
244245
return (AlgebraicClosureFunctor(), self.real_field())
245246

246-
def is_exact(self):
247+
def is_exact(self) -> Literal[False]:
247248
"""
248249
The complex interval field is not exact.
249250
@@ -254,7 +255,7 @@ def is_exact(self):
254255
"""
255256
return False
256257

257-
def prec(self):
258+
def prec(self) -> int:
258259
"""
259260
Return the precision of ``self`` (in bits).
260261
@@ -267,7 +268,7 @@ def prec(self):
267268
"""
268269
return self._prec
269270

270-
def to_prec(self, prec):
271+
def to_prec(self, prec) -> ComplexIntervalField_class:
271272
"""
272273
Return a complex interval field with the given precision.
273274
@@ -282,7 +283,7 @@ def to_prec(self, prec):
282283
"""
283284
return ComplexIntervalField(prec)
284285

285-
def _magma_init_(self, magma):
286+
def _magma_init_(self, magma) -> str:
286287
r"""
287288
Return a string representation of ``self`` in the Magma language.
288289
@@ -386,7 +387,7 @@ def __eq__(self, other):
386387
return False
387388
return self._prec == other._prec
388389

389-
def __hash__(self):
390+
def __hash__(self) -> int:
390391
"""
391392
Return the hash.
392393
@@ -400,7 +401,7 @@ def __hash__(self):
400401
"""
401402
return hash((self.__class__, self._prec))
402403

403-
def __ne__(self, other):
404+
def __ne__(self, other) -> bool:
404405
"""
405406
Test whether ``self`` is not equal to ``other``.
406407
@@ -650,7 +651,7 @@ def ngens(self):
650651
"""
651652
return 1
652653

653-
def zeta(self, n=2):
654+
def zeta(self, n: int = 2):
654655
r"""
655656
Return a primitive `n`-th root of unity.
656657

src/sage/rings/morphism.pyi

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
from sage.rings.integer import Integer
2-
from sage.structure.element import Element
3-
from sage.structure.parent import Parent
1+
from typing import Any
2+
43
from sage.categories.map import Map
54
from sage.categories.morphism import Morphism
5+
from sage.rings.integer import Integer
6+
from sage.structure.parent import Parent
67

78
class RingMap(Morphism):
89
pass
910

1011
class RingMap_lift(RingMap):
11-
S: Parent
12-
to_S: Map
12+
S: Parent[Any]
13+
to_S: Map[Any, Any]
1314

1415
class RingHomomorphism(RingMap):
1516
_lift: Morphism

src/sage/rings/ring.pyi

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
from __future__ import annotations
2+
3+
from typing import Any, Iterator, NoReturn
4+
5+
from sage.categories.category import Category
6+
from sage.categories.commutative_rings import CommutativeRings
7+
from sage.categories.fields import Fields
8+
from sage.categories.rings import Rings
9+
from sage.structure.category_object import NameSpec
10+
from sage.structure.parent import Parent
11+
from sage.structure.parent_gens import ParentWithGens
12+
13+
_Rings: Rings
14+
_CommutativeRings: CommutativeRings
15+
_Fields: Fields
16+
17+
18+
class Ring(ParentWithGens):
19+
_default_category: Category
20+
21+
def __init__(
22+
self,
23+
base: Parent[Any] | object,
24+
names: NameSpec = ...,
25+
normalize: bool = ...,
26+
category: Category | None = ...,
27+
) -> None: ...
28+
29+
def __iter__(self) -> Iterator[Any]: ...
30+
def __len__(self) -> int: ...
31+
def __xor__(self, n: object) -> NoReturn: ...
32+
def base_extend(self, X: Parent[Any]) -> Parent[Any]: ...
33+
def category(self) -> Category: ...
34+
def __mul__(self, x: object) -> object: ...
35+
def zero(self) -> Any: ...
36+
def one(self) -> Any: ...
37+
def order(self) -> int: ...
38+
39+
40+
class CommutativeRing(Ring):
41+
_default_category: Category
42+
43+
def __init__(
44+
self,
45+
base_ring: Parent[Any] | object,
46+
names: NameSpec = ...,
47+
normalize: bool = ...,
48+
category: Category | None = ...,
49+
) -> None: ...
50+
51+
def fraction_field(self) -> Ring: ...
52+
def extension(
53+
self,
54+
poly: object,
55+
name: str | None = ...,
56+
names: NameSpec = ...,
57+
**kwds: object,
58+
) -> Ring: ...
59+
60+
61+
class IntegralDomain(CommutativeRing):
62+
_default_category: Category
63+
64+
65+
class NoetherianRing(CommutativeRing):
66+
_default_category: Category
67+
68+
69+
class DedekindDomain(CommutativeRing):
70+
_default_category: Category
71+
72+
73+
class PrincipalIdealDomain(CommutativeRing):
74+
_default_category: Category
75+
76+
77+
def _is_Field(x: object) -> bool: ...
78+
79+
80+
class Field(CommutativeRing):
81+
_default_category: Category
82+
83+
84+
class Algebra(Ring):
85+
def __init__(self, base_ring: Parent[Any] | object, *args: object, **kwds: object) -> None: ...
86+
87+
88+
class CommutativeAlgebra(CommutativeRing):
89+
def __init__(self, base_ring: Parent[Any] | object, *args: object, **kwds: object) -> None: ...
90+
91+
92+
def is_Ring(x: object) -> bool: ...

0 commit comments

Comments
 (0)