Skip to content

Meta-ticket: Replace is_Class functions, create abstract base classes to enable modularization #32414

@mkoeppe

Description

@mkoeppe

git grep 'import.* is_[A-Z]' (or git grep 'import.*is_[A-Z]' | grep -E -v 'is_(Ring|Matrix)[^A-Z]' | grep -v 'sage:') reveals a common pattern in our code: We import a module just to check whether a user-provided object belongs to a class defined by that module.
This pattern is an obstacle to modularization.

The ongoing effort (#12824, https://groups.google.com/g/sage-devel/c/wEzb0awmyN0, #13370, #14329, #15076, #15098, #15333, #18173, #24443, #24525, #27593, #28470, #32347) to replace this pattern by importing the class and isinstance does not help in this regard by itself:
The module providing the class must be installed so that the import does not fail.

This can of course be worked around by try... except around import, as done for example in #32455, but we should avoid cluttering the code with this type of workaround.

Instead we propose to create stub classes (abstract base classes) in sage.structure.element, sage.structure.parent, and/or sage.PAC.KAGE.abc, and make the actual implementation classes subclasses (mostly, unique direct subclasses). This pattern already exists in the Sage code for a few existing classes:

We add the following new classes:

Technical limitation: If a sage.PAC.KAGE.abc module is intended to provide the new classes, then sage.PAC.KAGE must be prepared to become a native namespace package. (See Meta-ticket #32501: Clear out __init__.py files in preparation for namespace packages)

As of this ticket, each of the new abstract base classes is intended to have a unique direct implementation subclass. #32637 adds doctests to document and enforce this design. Attempting to define new hierarchies of abstract base classes is beyond the scope of this ticket. Also, the new classes do not define any methods.

Tickets not needed for modularization, but removing is_ functions in favor of isinstance for uniformity of the codebase:

Functions that look like is_Class:

Related:

Part of:

CC: @tscrim @fchapoton @kliem

Component: refactoring

Issue created by migration from https://trac.sagemath.org/ticket/32414

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions