2016-08-31 4 views
1

Если я определяю метод в cdef class, метод не имеет какой-либо информации о аргументов и аннотации:Как получить имена аргументов и __annotations__ из дескриптора метода Cython?

Python класс:

class MyClass: 
    def test(self, arg: int): 
     pass 
print(dir(MyClass.test)) 

['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__self__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name'] 

Cython класс:

cdef class MyClass: 
    def test(self, arg: int): 
     pass 
print(dir(MyClass.test)) 
['__call__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__name__', '__ne__', '__new__', '__objclass__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__text_signature__'] 

Есть ли способы получить argumnets с аннотациями из метода Китонов?

P.S. тип метода класса Cython является не метод: <class 'method_descriptor'>, не <class 'cython_function_or_method'>

ответ

2

Согласно this issue на репо Cython GitHub, были добавлены аннотации, но только для cdef функций:

%%cython 
def test2(arg: int): pass 
cpdef test1(arg: int): pass 
cdef test(arg: int): pass 

print(['__annotations__' in dir(i) for i in [test2, test1, test]]) 
[False, False, True] 

Кроме того, получение __annotations__ просто возвращает пустой словарь. Кажется, что аннотации используются только при создании кода C (не подтвержденного) и не предназначены для использования для пользователя.