If type hinting is the only issue you have with your code, then look at SO question Type hinting in Python 2
It says, that python3 respects also type hinting in comment lines.
Python2 will ignore it and python3 respects this alternative syntax. It has specifically been designed for code that still had to be python2 compatible.
However please note, that just because the code compiles with python2, it doesn't mean it will yield the correct result.
If you have more compatibility issues I strongly propose to look at the future
module (not to be mixed up with the from __future__ import xxx
statement.
You can install future ( https://pypi.org/project/future/ ) with pip install future
.
As you don't show any other code that causes problems I cannot advise on specific issues.
The url https://python-future.org/compatible_idioms.html shows a quite extensive list of potential python 2 / python 3 issues and how you might resolve them.
For example for opening files in python 2 with less encoding / unicode issues can be done by importing an alternative version of open with the line
from io import open
https://python-future.org/compatible_idioms.html
Addendum:
If you really are in the need of declaring one function for python3 and one function for python2 you can do:
import sys
if sys.version_info < (3, 0):
def myfunc(a, b): # python 2 compatible function
bla bla bla
else:
def myfunc(a, b): # python 3 compatible function
bla bla bla
However: Both function must be syntactically correct for python 2 and python 3
If you really want to have functions, which are only python2 or only python3 syntactically correct (e.g. print statement or await), then you could do something like:
import sys
if sys.version_info < (3, 0):
from myfunc_py2 import myfunc # the file myfunc_py2.py does not have to be python3 compatible
else:
from myfunc_py3 import myfunc # the file myfunc_py3.py does not have to be python2 compatible