Skip to content

Commit

Permalink
update docs about __getmetaclass__
Browse files Browse the repository at this point in the history
  • Loading branch information
infmagic2047 committed Aug 19, 2023
1 parent a1ab053 commit 55b088a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/sage/cpython/cython_metaclass.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ file if you are using one).
In the extension type (a.k.a. ``cdef class``) for which you want to
define a metaclass, define a method ``__getmetaclass__`` with a single
unused argument. This method should return a type to be used as
unused argument, and turn off the Cython directive
``always_allow_keywords``. This method should return a type to be used as
metaclass:
.. code-block:: cython
cimport cython
cimport sage.cpython.cython_metaclass
cdef class MyCustomType():
@cython.always_allow_keywords(False)
def __getmetaclass__(_):
from foo import MyMetaclass
return MyMetaclass
Expand Down Expand Up @@ -63,8 +66,10 @@ EXAMPLES::
sage: cython( # optional - sage.misc.cython
....: '''
....: cimport cython
....: cimport sage.cpython.cython_metaclass
....: cdef class MyCustomType():
....: @cython.always_allow_keywords(False)
....: def __getmetaclass__(_):
....: class MyMetaclass(type):
....: def __init__(*args):
Expand Down Expand Up @@ -101,8 +106,10 @@ returns a non-type::
sage: cython( # optional - sage.misc.cython
....: '''
....: cimport cython
....: cimport sage.cpython.cython_metaclass
....: cdef class MyCustomType():
....: @cython.always_allow_keywords(False)
....: def __getmetaclass__(_):
....: return 2
....: ''')
Expand Down
3 changes: 3 additions & 0 deletions src/sage/misc/inherit_comparison.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ cdef class InheritComparisonMetaclass(type):
sage: cython( # optional - sage.misc.cython
....: '''
....: cimport cython
....:
....: from sage.misc.inherit_comparison cimport InheritComparisonMetaclass
....:
....: cdef class Base():
Expand All @@ -65,6 +67,7 @@ cdef class InheritComparisonMetaclass(type):
....: return 1
....:
....: cdef class DerivedWithRichcmp(Base):
....: @cython.always_allow_keywords(False)
....: def __getmetaclass__(_):
....: from sage.misc.inherit_comparison import InheritComparisonMetaclass
....: return InheritComparisonMetaclass
Expand Down

0 comments on commit 55b088a

Please sign in to comment.