In my code base, I use Cython
to create shared object files from my modules. When I try to access the function __annotations__
, I get the different behavior when I use Cython and when I do not, and I am wondering why this is the case:
Minimal example:
I tried to produce a minimal reproducable example and came up with the following two files main.py
and setup.py
that I create within the same directory. Requirements: pip install Cython setuptools
.
main.py
import setup
def test(name: str): pass
if __name__ == '__main__':
print(test.__annotations__, setup.test.__annotations__)
Setup.py
from Cython.Build import cythonize
from setuptools import setup, Extension
def test(name: str): pass
if __name__ == '__main__':
setup(python_requires='>=3.6', ext_modules=cythonize([Extension('setup', ['setup.py']), ], language_level="3"))
Execution
python main.py
> {'name': <class 'str'>}
> {'name': <class 'str'>}
python setup.py build_ext --inplace
python main.py
> {'name': <class 'str'>}
> {'name': 'unicode'}
I would have expected the __annotations__
to yield str
also when using the shared object files, but it yields unicode
instead. Why is this the case?
I use Python 3.9.2
and Cython version 0.29.21
.