Search code examples
python-3.xpython-2.7setuptoolsswig

How do tell "swig -python -py3 myswig.i" not to include annotations


I need to use SWIG to support both Python 2.7 and Python 3.10. [Yes, I know that Python 2.7 is dead, and we're doing our best to migrate users away from it as quickly as we can.]

I generate my module via setuptools.setup with a Swig extension. I can run the setup using both Python2 and Python3. The setuptools program creates a separate shareable library for the Python2 and Python3 runs. However both runs generate a myswig.py file in the same location.

It turns out that the Py2 and Py3 generated files are identical, except that the Py3 generated file contains annotations for the functions and the Py2 doesn't. Python3 can read the Python2 generated file and works just fine. Python2 cannot read the Python3 generated file.

I've tried both %feature("autodoc", 0); and leaving out this line completely, and I still get annotations.

So is there someway of either:

  1. Turning off annotations in the generated file
  2. Adding from __future__ import annotations automatically to the generated file

Solution

  • Don't use -py3. It's not required for Python 3, but enables Python 3-specific code features like annotations.