If I define method in cdef class
, method has no any info about arguments and annotations:
Python class:
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 class:
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__']
Are there any ways to get argumnets with annotations from Cython method?
P.S. type of Cython's class method is not a method: <class 'method_descriptor'>
, not a <class 'cython_function_or_method'>
According to this issue on the Cython github repo, annotations were added but only for cdef
functions:
%%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]
Apart from that, getting __annotations__
simply returns an empty dictionary. It seems annotations are only used when generating the C
code (haven't verified) and not meant to be available for the user.