Skip to content

Annotate return types of PowerSeriesRing, LaurentPolynomialRing and QuotientRing (follow-up to #42670) - #42675

Open
starnotes-xj wants to merge 4 commits into
sagemath:developfrom
starnotes-xj:annotate-factory-function-returns
Open

Annotate return types of PowerSeriesRing, LaurentPolynomialRing and QuotientRing (follow-up to #42670)#42675
starnotes-xj wants to merge 4 commits into
sagemath:developfrom
starnotes-xj:annotate-factory-function-returns

Conversation

@starnotes-xj

@starnotes-xj starnotes-xj commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

Follow-up to #42670: this PR annotates three constructor functions whose return types static type checkers cannot infer.

  • PowerSeriesRing returns PowerSeriesRing_generic, the common base of its univariate and multivariate implementations.
  • LaurentPolynomialRing returns LaurentPolynomialRing_generic, the common base of its univariate and multivariate implementations.
  • QuotientRing returns Parent: unlike the narrower generic quotient class, this accurately covers all supported branches, including ordinary and principal quotients, noncommutative quotients, Boolean-polynomial implementations, integer quotients and the zero-ideal path that returns the original parent.

Forward references are quoted locally where the implementation class is defined later in the module, avoiding import-time NameError without changing annotation evaluation semantics for unrelated functions.

Regression doctests cover representative runtime branches and typing.get_type_hints resolution. No runtime behavior changes; these are annotations only.

@starnotes-xj

Copy link
Copy Markdown
Contributor Author

@cxzhong @tobiasdiez — this is the second follow-up to #42670 (annotates three more Sage constructor functions: PowerSeriesRing, LaurentPolynomialRing, QuotientRing). Could you take a look?

The CI runs for the current head fb1a079 are sitting in action_required (they have not been approved to execute). Could you approve them when you get a chance?

Note: the docker "Build & Test" shard reddening is the unrelated ecl.pyx abort tracked in #42680 (it reproduces on branches that do not touch libs/ecl.pyx).

…uotientRing

Static type checkers cannot infer the return type of these module-level
constructor functions.  Add return annotations using the common base of
the concrete implementations (verified against Sage 10.9):

- PowerSeriesRing      -> PowerSeriesRing_generic (covers PowerSeriesRing_domain,
                          PowerSeriesRing_over_field and MPowerSeriesRing_generic)
- LaurentPolynomialRing -> LaurentPolynomialRing_generic (covers the
                          univariate and multivariate implementations)
- QuotientRing          -> QuotientRing_generic (covers the generic and
                          polynomial quotient implementations)

TESTS blocks assert the annotation against the runtime type for the
common univariate and multivariate cases.  Follow-up to sagemath#42670.
PowerSeriesRing and QuotientRing are annotated with their generic
base class, which is defined further down in the same module.  On
Python < 3.14 annotations are evaluated eagerly at def time, so the
module fails to import on 3.13 (the fork doc build hit
'NameError: name QuotientRing_generic is not defined').  Add
'from __future__ import annotations' so the names resolve lazily.
LaurentPolynomialRing needs no change: its generic base lives in
laurent_polynomial_ring_base and is imported at the top.
Use local forward references where classes are defined later, keep the Laurent and power-series common bases, and broaden QuotientRing to the Parent contract used by all supported branches. Extend doctests across annotation resolution and representative factory paths.

Constraint: QuotientRing returns unchanged parents and multiple specialized quotient implementations\nRejected: QuotientRing_generic | excludes noncommutative, principal, pbori, and zero-ideal paths\nRejected: Module-wide postponed annotations | changes unrelated runtime introspection semantics\nConfidence: high\nScope-risk: moderate\nReversibility: clean\nDirective: Preserve local forward references unless module-wide annotation semantics are intentionally reviewed\nTested: python -m py_compile on all three modules; git diff --check\nNot-tested: Sage doctests require the configured Sage build environment
@starnotes-xj
starnotes-xj force-pushed the annotate-factory-function-returns branch from fb1a079 to 0bc02dc Compare August 23, 2026 18:19
@starnotes-xj
starnotes-xj marked this pull request as ready for review August 23, 2026 18:20
Copilot AI lite review requested due to automatic review settings August 23, 2026 18:20

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

The regression doctest uses Parent explicitly, so import it in the doctest namespace instead of relying on module globals.

Constraint: Sage doctests do not expose module-level imports automatically\nConfidence: high\nScope-risk: narrow\nReversibility: clean\nTested: git diff --check\nNot-tested: Sage doctest runtime
@github-actions

Copy link
Copy Markdown

Documentation preview for this PR (built with commit 663f741; changes) is ready! 🎉
This preview will update shortly after each push to this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants