Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
""" 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.
EXAMPLES:
Currently pickling fails for parents using nested classes (typically for categories), but deriving only from Parent::
sage: from sage.misc.nested_class_test 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()
""" #***************************************************************************** # Copyright (C) 2009 Nicolas M. Thiery <nthiery at users.sf.net> # # Distributed under the terms of the GNU General Public License (GPL) # http://www.gnu.org/licenses/ #******************************************************************************
""" EXAMPLES::
sage: sage.misc.nested_class_test.TestParent1() <sage.misc.nested_class_test.TestParent1_with_category object at ...> """
""" EXAMPLES::
sage: sage.misc.nested_class_test.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 """
""" EXAMPLES::
sage: sage.misc.nested_class_test.TestParent3() <sage.misc.nested_class_test.TestParent3_with_category object at ...> """
""" EXAMPLES::
sage: sage.misc.nested_class_test.TestParent4() <sage.misc.nested_class_test.TestParent4_with_category object at ...> """
""" EXAMPLES::
sage: from sage.misc.nested_class_test import TestParent4 sage: TestParent4() == TestParent4() True """
""" EXAMPLES::
sage: from sage.misc.nested_class_test import TestParent4 sage: TestParent4() != TestParent4() False """
# Class for tests: """ A normal external class. """
""" This class is broken and can't be pickled. A warning is emmited during compilation. """
""" There is no problem here. """
""" There is a nested class just below. Which can't be properly sphinxed. """ """ Internal C class.
Thanks to the links below this class is pickled ok. But it is sphixed wrong: It is typeset as a link to an outer class. """
""" B interne """
""" There is a nested class just below which is properly sphinxed. """ """ B interne """
""" This is a dummy for testing source inspection of nested classes.
EXAMPLES::
sage: from sage.misc.nested_class_test import TestNestedParent sage: from sage.misc.sageinspect import sage_getsource sage: P = TestNestedParent() sage: E = P.element_class sage: E.__bases__ (<class sage.misc.nested_class_test.TestNestedParent.Element at ...>, <class 'sage.categories.sets_cat.Sets.element_class'>) sage: print(sage_getsource(E)) class Element: "This is a dummy element class" pass
""" "This is a dummy element class"
|