Python Forum
How to import annoations for python 3.7 and below
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to import annoations for python 3.7 and below
#1
I want to have code that uses Python 3.7's
from __future__ import annoations
,
but I also want the code to run on older versions of Python (e.g. 3.5).

I have tried:

try:
    from __future__ import annotations
except ImportError:
    pass
but that fails with:

  File "test_import_annotations.py", line 6
    from __future__ import annotations
    ^
SyntaxError: future feature annotations is not defined
Is there any way to guard the import so that it will be ignored on older python versions, but work with python versions that support the import ?

Thanks,
Brendan.
Reply
#2
Python 3.7 is officially released! This new Python version has been in development since September 2016, and now we all get to enjoy the results of the core developers’ hard work.

What does the new Python version bring? While the documentation gives a good overview of the new features, this article will take a deep dive into some of the biggest pieces of news.
Reply
#3
Thanks, but that doesn't answer my question one little bit. I know Python 3.7 has been released and I use it for a lot of projects, however I have to use Python 3.5 for one particular project.

I want to run the same code with Python 3.5 (one a production system), but also on Python 3.7 (on my development machine).

The question is "how can I write the code so I can do an from __future__ import annotations in the code, but only if running in python 3.7 ?

The try: except: trick doesn't work as there seems to be some weird constraint that importing from __future__ has to be on the first line (that's a weird constraint !!). The same thing happens if I try checking the python version with if statement.
Reply
#4
you can check the version work from there, but you will need to do so (branch the code) not only for the import but elsewhere in the code where you would otherwise use annotations. It will clutter the code.

to fix the first line problem - you can have a "helper" file, e.g. myannotation.py with only the from __future__ import annotations line (i.e. on the first line)
then you can import that file/module in the if block or with the try block

the cleanest way is to develop on same version as production (assuming you don't want or can't upgrade the production)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Tool to adding type annoations Beliavsky 1 1,843 Dec-18-2019, 04:25 PM
Last Post: snippsat
  wordpress-python-xmlrpc. I can import 1 file. How do I import the entire folder? SamLearnsPython 0 3,234 Jul-05-2018, 06:21 AM
Last Post: SamLearnsPython

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020