How to use typing in older versions seems a bit unclear, can you provide an example of how to use typing with older versions of python?
This doesn't seem to do anything:
def greeting(name): # type: (str) -> str
"""documentations"""
return ('Hello ' + name)
if __name__ == "__main__":
print (greeting("World"))
print (greeting(44)) # Doesn't catch invalid input type until i get to the string concat.
In addition, the __annotations__ property doesn't get added to the function:
import typing
def greeting(name): # type: (str) -> str
"""documentations"""
return ('Hello ' + name)
print(greeting.__annotations__) # fails because doesn't exist.
Please advise how this back port should be used properly for developers who need to maintain compatibility with older versions of Python (2.7.12 and 3.4).
Thank you
You need a separate static type checking tool such as mypy to do anything useful with typing in Python 2. You can't use __annotations__ in Python 2, but that's usually fine since it's not needed for static checking. If you want to do runtime introspection of annotations, supporting Python 2 is going to be tricky and you are basically on your own. Can tell more about your use case?
@JukkaL ok, I'm fine with typing only working with python 3, but i still need the back support an API I work on. When I use the # type in python 3.5.2 (my testing version) __annotations__ no longer populates.
Is this expected or am I doing it wrong?
Type comments are just normal comments even in Python 3 and don't affect __annotations__. You can only introspect non-comment function annotations (e.g. def f(x: int) -> str) and only in Python 3.x, and you can only introspect variable annotations in Python 3.6+ if you use the recently introduced variable annotation syntax. The variable annotations syntax isn't available in pre-3.6 Python. There is no easy way around these limitations.
@JukkaL thank you for this information. It unfortunate that it isn't back supported fully. I guess it would be better to wait to use this stuff until 3.6 is up and running.
I think the pypi package of typing needs to be better clarified on the limitations because essentially you can't have a v2/3 or even a v3.4/3.5/3.6 supported package with this type of syntax..
So to sum up what you are saying:
# type syntax is only supported in python pre-3.5 using the typing module.
The def foo(x: str) ->str: syntax is only supported 3.5+ (including 3.6).
The type notation and function annotation (bullet point 2) are not compatible so 3.5 will not recognize the # type in 3.5 but it will work in python 3.6
Let me know if I missed anything.
@achapkowski Function annotation syntax is supported in 3.0+ Type comments are supported in all versions (but have no runtime effects, since they are just comments). The typing module is normally used with a _static_ type checker like https://github.com/python/mypy no type checking happens at runtime, see PEP 484.
If you are interested in _runtime_ use of typing for other needs, then you need 3.2+ for function annotations and 3.6+ for variable annotations.
@ilevkivskyi are you saying:
def foo(x: str) -> str:
return 'bar'
should work in python 3.4?
It most certainly works.
@achapkowski That indeed works in python 3.0 or higher
@achapkowski It will work in the sense that it will compile and run without error. But once more: no type checks happen at runtime.
Most helpful comment
@achapkowski Function annotation syntax is supported in 3.0+ Type comments are supported in all versions (but have no runtime effects, since they are just comments). The typing module is normally used with a _static_ type checker like https://github.com/python/mypy no type checking happens at runtime, see PEP 484.
If you are interested in _runtime_ use of typing for other needs, then you need 3.2+ for function annotations and 3.6+ for variable annotations.