Skip to content

[fork CI] Verify upstream PR #42670 fix (bea9305) — CI vehicle, do not review - #1

Merged
starnotes-xj merged 8 commits into
developfrom
annotate-ring-factory-return-types
Oct 16, 2026
Merged

starnotes-xj merged 8 commits into
developfrom
annotate-ring-factory-return-types

Conversation

@starnotes-xj

Copy link
Copy Markdown
Owner

Fork-internal PR that exists only to run the same pull_request CI as sagemath#42670 without the maintainer approval gate.

Head: bea9305 (get_type_hints identity assertions removed after the Python 3.14 failure).

This PR is not intended for review or merge; it mirrors sagemath#42670.

Static type checkers cannot infer the return type of GF, Zmod,
Integers and IntegerModRing because these are UniqueFactory instances
whose Cython __call__ carries no annotation.  Add an annotated,
purely forwarding __call__ override to the two factory classes:

- FiniteFieldFactory.__call__ returns FiniteField, the common base of
  FiniteField_prime_modn, FiniteField_givaro, FiniteField_ntl_gf2e and
  FiniteField_pari_ffelt;
- IntegerModFactory.__call__ returns IntegerModRing_generic or
  IntegerRing_class, because Integers(0) returns ZZ.

Verified against Sage 10.9: the annotations appear in
inspect.signature, import sage.all is unaffected, the doctests of both
modules pass, and factory behavior is unchanged for GF(29),
GF(2^8, 'a'), Zmod(17) and Integers(0).
Add a comment explaining that the __call__ overrides exist only to
expose the return types to static type checkers (review feedback).
The TESTS blocks assert, on every backend, that the annotated return
type matches the runtime type:

- typing.get_type_hints(type(GF).__call__)['return'] is FiniteField
- GF(2^8, 'a', implementation=impl) is a FiniteField for givaro/ntl/pari_ffelt
- typing.get_type_hints(type(Zmod).__call__)['return'] equals the declared union
- Zmod(29) is an IntegerModRing_generic; Integers(0) is an IntegerRing_class

This mechanically guards the annotations against future runtime drift
(replacing the manual verification against Sage 10.9).
The typing.get_type_hints(...) is FiniteField identity check failed on
the Python 3.14 CI job (sage's 3.14 build resolves the annotation to a
non-identical object).  The isinstance assertions are the actual
regression guard for the annotation contract; keep those and drop the
fragile get_type_hints comparisons.
Copilot AI lite review requested due to automatic review settings August 17, 2026 01:52
@starnotes-xj starnotes-xj reopened this Aug 17, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This fork-internal PR mirrors upstream changes to add explicit return-type annotations for the UniqueFactory-based constructors IntegerModRing/aliases and GF, primarily to improve static typing inference.

Changes:

  • Added IntegerModFactory.__call__ override with an explicit return-type annotation and doctests validating runtime types.
  • Added FiniteFieldFactory.__call__ override with an explicit return-type annotation and doctests validating runtime types across implementations.
  • Added an import of FiniteField (base class) to support the new runtime annotation in the finite field constructor module.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/sage/rings/finite_rings/integer_mod_ring.py Adds a __call__ override on the integer mod ring factory to expose a return type for static type checkers.
src/sage/rings/finite_rings/finite_field_constructor.py Adds a __call__ override on the finite field factory (and imports FiniteField) to expose a return type for static type checkers and adds doctests.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +207 to +211
# This override is only there for the typing info: it exists so that
# static type checkers can infer the return type of ``IntegerModRing``
# and its aliases. The behavior is identical to the ``UniqueFactory``
# base implementation.
def __call__(self, *args, **kwds) -> "IntegerModRing_generic | integer_ring.IntegerRing_class":
Comment on lines +507 to +510
# This override is only there for the typing info: it exists so that
# static type checkers can infer the return type of ``GF``. The
# behavior is identical to the ``UniqueFactory`` base implementation.
def __call__(self, *args, **kwds) -> FiniteField:
Comment on lines +514 to +521
The return annotation matches the runtime type for every backend::

sage: from sage.rings.finite_rings.finite_field_base import FiniteField
sage: all(isinstance(GF(2**8, 'a', implementation=impl), FiniteField)
....: for impl in ('givaro', 'ntl', 'pari_ffelt'))
True
sage: isinstance(GF(29), FiniteField)
True
Replace the forwarding __call__ overrides with bare class-level
variable annotations: static type checkers still infer the factory
return types, while factory calls keep dispatching directly to the
inherited UniqueFactory.__call__ with no extra Python frame or
super() lookup (review feedback).  Move the regression doctests to
class-level TESTS blocks and add a structural check that the
__call__ annotation resolves to the declared return type.

Import FiniteField under the alias FiniteField_base so that the
annotation resolves to the class rather than to the factory instance
once PEP 649 deferred annotations (Python 3.14) evaluate it through
the live module namespace.
Use from __future__ import annotations so the unquoted forward
reference to IntegerModRing_generic (defined later in this module)
is safe at runtime, and resolve the annotation via
typing.get_type_hints in the doctest (review feedback from
vincentmacri).  Keep the collections.abc.Callable import at runtime
visibility: get_type_hints - used by the doctest contract and by
downstream stub generators - evaluates the annotation string in the
module namespace, so the name must exist there.  Exempt that import
from TC003 via per-file-ignores with a comment.
Per review, keep the ruff exemption next to the import itself instead
of a per-file-ignores entry in pyproject.toml: the runtime visibility
of Callable is intentional (typing.get_type_hints resolves the
class-level __call__ annotation in the module namespace).
@starnotes-xj
starnotes-xj merged commit 4dc4cad into develop Oct 16, 2026
16 of 19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants