Cython:如何为包含枚举的代码创建 .pxd-file?

Cython: How can I create a .pxd-file for Code including an Enum?

我正在尝试“cythonize”以下示例代码,其中包括一个枚举 class 的实例:

from enum import Enum
class AnimalType(Enum):
    Shark = 0
    Fish = 1

class Animal:
    def __init__(self, animal_type: AnimalType, weight: float):
        self.animal_type = animal_type
        self.weight = weight

创建带有类型声明的 .pyx-file 很容易:

cpdef enum AnimalType:
    Shark = 0
    Fish = 1

cdef class Animal:
    cdef double weight
    cdef AnimalType animal_type

    def __init__(self, animal_type: AnimalType, weight: float):
        self.animal_type = animal_type
        self.weight = weight

但是,我无法将 .pyx 文件拆分为 .pyx 和 .pxd (header) 文件。你能帮我为我的例子定义一个 .pxd-file 吗?

编辑:我已阅读 https://groups.google.com/g/cython-users/c/ZoLsLHwnUY4。可能无法做到这一点....

它对我有用,不需要做任何特别的事情:

testenum.pxd:

cpdef enum AnimalType:
    Shark = 0
    Fish = 1

(如果你也想分享 Cython 的定义,你也可以把 Animalcdef 部分放在那里。

testenum.pyx:

# no need to cimport testenum - this happens implicitly

cdef class Animal:
    cdef double weight   # omit this if you put it in the pxd file
    cdef AnimalType animal_type   # omit this if you put it in the pxd file

    def __init__(self, animal_type: AnimalType, weight: float):
        self.animal_type = animal_type
        self.weight = weight

someotherfile.pyx:

from testenum cimport AnimalType

cdef class C:
    cdef AnimalType at