Skip to content

Commit c937bdc

Browse files
committed
typ: use protocol type bounds
1 parent 502bf69 commit c937bdc

File tree

1 file changed

+43
-46
lines changed

1 file changed

+43
-46
lines changed

src/flint/typing.py

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,6 @@
5252
]
5353

5454

55-
_Telem = TypeVar("_Telem", bound=flint_scalar)
56-
_Telem_contra = TypeVar("_Telem_contra", bound=flint_scalar, contravariant=True)
57-
_Sctx = TypeVar("_Sctx", bound=flint_mpoly_context)
58-
5955
_str = str
6056

6157

@@ -86,6 +82,7 @@ def __rpow__(self, other: int, /) -> Self: ...
8682

8783

8884
_Tscalar = TypeVar("_Tscalar", bound=scalar_p)
85+
_Tscalar_contra = TypeVar("_Tscalar_contra", bound=scalar_p, contravariant=True)
8986

9087

9188
class poly_p(elem_p, Protocol[_Tscalar]):
@@ -143,72 +140,72 @@ def factor_squarefree(self) -> tuple[_Tscalar, list[tuple[Self, int]]]: ...
143140
def deflation(self) -> tuple[Self, int]: ...
144141

145142

146-
class _series_p(elem_p, Protocol[_Telem]):
143+
class _series_p(elem_p, Protocol[_Tscalar]):
147144
"""FLINT univariate power series."""
148145

149-
def __iter__(self) -> Iterator[_Telem]: ...
150-
def coeffs(self) -> list[_Telem]: ...
146+
def __iter__(self) -> Iterator[_Tscalar]: ...
147+
def coeffs(self) -> list[_Tscalar]: ...
151148

152149

153-
class mpoly_p(elem_p, Protocol[_Telem]):
150+
class mpoly_p(elem_p, Protocol[_Tscalar]):
154151
def __init__(
155152
self,
156153
val: Self
157-
| _Telem
154+
| _Tscalar
158155
| int
159-
| Mapping[tuple[int, ...], _Telem | int]
156+
| Mapping[tuple[int, ...], _Tscalar | int]
160157
| str = 0,
161158
ctx: Any | None = None,
162159
) -> None: ...
163160
def str(self) -> _str: ...
164161
def repr(self) -> _str: ...
165-
def context(self) -> mpoly_context_p[Self, _Telem]: ...
162+
def context(self) -> mpoly_context_p[Self, _Tscalar]: ...
166163
def degrees(self) -> tuple[int, ...]: ...
167164
def total_degree(self) -> int: ...
168-
def leading_coefficient(self) -> _Telem: ...
169-
def to_dict(self) -> dict[tuple[int, ...], _Telem]: ...
165+
def leading_coefficient(self) -> _Tscalar: ...
166+
def to_dict(self) -> dict[tuple[int, ...], _Tscalar]: ...
170167
def is_one(self) -> bool: ...
171168
def is_zero(self) -> bool: ...
172169
def is_constant(self) -> bool: ...
173170
def __len__(self) -> int: ...
174-
def __getitem__(self, index: tuple[int, ...]) -> _Telem: ...
171+
def __getitem__(self, index: tuple[int, ...]) -> _Tscalar: ...
175172
def __setitem__(
176-
self, index: tuple[int, ...], coeff: _Telem | int
173+
self, index: tuple[int, ...], coeff: _Tscalar | int
177174
) -> None: ...
178175
def __iter__(self) -> Iterable[tuple[int, ...]]: ...
179176
def __contains__(self, index: tuple[int, ...]) -> bool: ...
180-
def coefficient(self, i: int) -> _Telem: ...
177+
def coefficient(self, i: int) -> _Tscalar: ...
181178
def monomial(self, i: int) -> tuple[int, ...]: ...
182-
def terms(self) -> Iterable[tuple[tuple[int, ...], _Telem]]: ...
179+
def terms(self) -> Iterable[tuple[tuple[int, ...], _Tscalar]]: ...
183180
def monoms(self) -> list[tuple[int, ...]]: ...
184-
def coeffs(self) -> list[_Telem]: ...
181+
def coeffs(self) -> list[_Tscalar]: ...
185182
def __pos__(self) -> Self: ...
186183
def __neg__(self) -> Self: ...
187-
def __add__(self, other: Self | _Telem | int) -> Self: ...
188-
def __radd__(self, other: _Telem | int) -> Self: ...
189-
def __sub__(self, other: Self | _Telem | int) -> Self: ...
190-
def __rsub__(self, other: _Telem | int) -> Self: ...
191-
def __mul__(self, other: Self | _Telem | int) -> Self: ...
192-
def __rmul__(self, other: _Telem | int) -> Self: ...
193-
def __truediv__(self, other: Self | _Telem | int) -> Self: ...
194-
def __rtruediv__(self, other: _Telem | int) -> Self: ...
195-
def __floordiv__(self, other: Self | _Telem | int) -> Self: ...
196-
def __rfloordiv__(self, other: _Telem | int) -> Self: ...
197-
def __mod__(self, other: Self | _Telem | int) -> Self: ...
198-
def __rmod__(self, other: _Telem | int) -> Self: ...
184+
def __add__(self, other: Self | _Tscalar | int) -> Self: ...
185+
def __radd__(self, other: _Tscalar | int) -> Self: ...
186+
def __sub__(self, other: Self | _Tscalar | int) -> Self: ...
187+
def __rsub__(self, other: _Tscalar | int) -> Self: ...
188+
def __mul__(self, other: Self | _Tscalar | int) -> Self: ...
189+
def __rmul__(self, other: _Tscalar | int) -> Self: ...
190+
def __truediv__(self, other: Self | _Tscalar | int) -> Self: ...
191+
def __rtruediv__(self, other: _Tscalar | int) -> Self: ...
192+
def __floordiv__(self, other: Self | _Tscalar | int) -> Self: ...
193+
def __rfloordiv__(self, other: _Tscalar | int) -> Self: ...
194+
def __mod__(self, other: Self | _Tscalar | int) -> Self: ...
195+
def __rmod__(self, other: _Tscalar | int) -> Self: ...
199196
def __divmod__(
200-
self, other: Self | _Telem | int
197+
self, other: Self | _Tscalar | int
201198
) -> tuple[Self, Self]: ...
202-
def __rdivmod__(self, other: _Telem | int) -> tuple[Self, Self]: ...
203-
def __pow__(self, other: _Telem | int) -> Self: ...
204-
def __rpow__(self, other: _Telem | int) -> Self: ...
205-
def iadd(self, other: _Telem | int) -> None: ...
206-
def isub(self, other: _Telem | int) -> None: ...
207-
def imul(self, other: _Telem | int) -> None: ...
199+
def __rdivmod__(self, other: _Tscalar | int) -> tuple[Self, Self]: ...
200+
def __pow__(self, other: _Tscalar | int) -> Self: ...
201+
def __rpow__(self, other: _Tscalar | int) -> Self: ...
202+
def iadd(self, other: _Tscalar | int) -> None: ...
203+
def isub(self, other: _Tscalar | int) -> None: ...
204+
def imul(self, other: _Tscalar | int) -> None: ...
208205
def gcd(self, other: Self) -> Self: ...
209206
def term_content(self) -> Self: ...
210-
def factor(self) -> tuple[_Telem, Sequence[tuple[Self, int]]]: ...
211-
def factor_squarefree(self) -> tuple[_Telem, Sequence[tuple[Self, int]]]: ...
207+
def factor(self) -> tuple[_Tscalar, Sequence[tuple[Self, int]]]: ...
208+
def factor_squarefree(self) -> tuple[_Tscalar, Sequence[tuple[Self, int]]]: ...
212209
def sqrt(self) -> Self: ...
213210
def resultant(self, other: Self, var: _str | int) -> Self: ...
214211
def discriminant(self, var: _str | int) -> Self: ...
@@ -217,9 +214,9 @@ def deflation(self) -> tuple[Self, list[int]]: ...
217214
def deflation_monom(self) -> tuple[Self, list[int], Self]: ...
218215
def inflate(self, N: list[int]) -> Self: ...
219216
def deflate(self, N: list[int]) -> Self: ...
220-
def subs(self, mapping: Mapping[_str | int, _Telem | int]) -> Self: ...
217+
def subs(self, mapping: Mapping[_str | int, _Tscalar | int]) -> Self: ...
221218
def compose(self, *args: Self, ctx: Any | None = None) -> Self: ...
222-
def __call__(self, *args: _Telem) -> _Telem: ...
219+
def __call__(self, *args: _Tscalar) -> _Tscalar: ...
223220
def derivative(self, var: _str | int) -> Self: ...
224221
def unused_gens(self) -> tuple[_str, ...]: ...
225222
def project_to_context(
@@ -230,18 +227,18 @@ def project_to_context(
230227
_Tmpoly_p = TypeVar("_Tmpoly_p", bound=mpoly_p, covariant=True)
231228

232229

233-
class mpoly_context_p(elem_p, Protocol[_Tmpoly_p, _Telem_contra]):
230+
class mpoly_context_p(elem_p, Protocol[_Tmpoly_p, _Tscalar_contra]):
234231
def nvars(self) -> int: ...
235232
def ordering(self) -> Ordering: ...
236233
def gen(self, i: int, /) -> _Tmpoly_p: ...
237-
def from_dict(self, d: Mapping[tuple[int, ...], _Telem_contra | int], /) -> _Tmpoly_p: ...
238-
def constant(self, z: _Telem_contra | int, /) -> _Tmpoly_p: ...
234+
def from_dict(self, d: Mapping[tuple[int, ...], _Tscalar_contra | int], /) -> _Tmpoly_p: ...
235+
def constant(self, z: _Tscalar_contra | int, /) -> _Tmpoly_p: ...
239236
def name(self, i: int, /) -> str: ...
240237
def names(self) -> tuple[str]: ...
241238
def gens(self) -> tuple[_Tmpoly_p, ...]: ...
242239
def variable_to_index(self, var: str, /) -> int: ...
243240
def term(
244-
self, coeff: _Telem_contra | None = None, exp_vec: Iterable[int] | None = None
241+
self, coeff: _Tscalar_contra | None = None, exp_vec: Iterable[int] | None = None
245242
) -> _Tmpoly_p: ...
246243
def drop_gens(self, gens: Iterable[str | int], /) -> Self: ...
247244
def append_gens(self, gens: Iterable[str | int], /) -> Self: ...
@@ -310,7 +307,7 @@ def _epoly(x: epoly_p[_Tscalar0], y: epoly_p[_Tscalar0]) -> epoly_p[_Tscalar0]:
310307

311308
_Tmpoly0 = TypeVar("_Tmpoly0", bound=mpoly_p)
312309

313-
def _mpoly(x: mpoly_p[_Telem], y: mpoly_p[_Telem], ctx: mpoly_context_p[_Tmpoly0, _Telem]) -> mpoly_p[_Telem]:
310+
def _mpoly(x: mpoly_p[_Tscalar], y: mpoly_p[_Tscalar], ctx: mpoly_context_p[_Tmpoly0, _Tscalar]) -> mpoly_p[_Tscalar]:
314311
z = x.gcd(y).factor()[1][0][0] + ctx.gens()[0]
315312
t = z.compose(z, ctx=ctx) + z.context().gens()[0]
316313
return t

0 commit comments

Comments
 (0)