Test for nested class Parent#

This file contains a discussion, examples, and tests about nested classes and parents. It is kept in a separate file to avoid import loops.


Currently pickling fails for parents using nested classes (typically for categories), but deriving only from Parent:

sage: from sage.misc.test_nested_class import TestParent1, TestParent2, TestParent3, TestParent4
sage: P = TestParent1()
sage: TestSuite(P).run()
Failure ...
The following tests failed: _test_elements, _test_pickling

They actually need to be in the NestedClassMetaclass. However, due to a technical detail, this is currently not directly supported:

sage: P = TestParent2()
Traceback (most recent call last):
TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
sage: TestSuite(P).run()  # not tested

Instead, the easiest is to inherit from UniqueRepresentation, which is what you want to do anyway most of the time:

sage: P = TestParent3()
sage: TestSuite(P).run()

This is what all Sage’s parents using categories currently do. An alternative is to use ClasscallMetaclass as metaclass:

sage: P = TestParent4()
sage: TestSuite(P).run()