我想在函数或类方法中获取注释。如果类型提示是在源代码中编写的,那么我可以通过获取属性__annotations__
来获取类型。
def hoge(n: int): ...
print(hoge.__annotations__) # {'n': <class 'int'>}
但我不知道如何在存根文件(.pyi)中编写类型。
# .pyi
def fuga(n: int): ...
# .py
def fuga(n): ...
print(fuga.__annotations__) # {}
有没有很酷的方法?