question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Python 3.7: from __future__ import annotations

See original GitHub issue

Hi,

I’m on Windows 10, Python 3.7.1, Cython 0.29.7.

I have some pure python code that I wanted to compile that uses from __future__ import annotations, but compiling it throws:

Error compiling Cython file:
------------------------------------------------------------
...
# -*- coding: utf-8 -*-

from __future__ import annotations
                      ^
------------------------------------------------------------

app.py:6:23: future feature annotations is not defined
Traceback (most recent call last):
  File "D:\app\compile_with_cython.py", line 59, in <module>
    main()
  File "D:\app\compile_with_cython.py", line 54, in main
    language_level=3,
  File "C:\Python37\lib\site-packages\Cython\Build\Dependencies.py", line 1097, in cythonize
    cythonize_one(*args)
  File "C:\Python37\lib\site-packages\Cython\Build\Dependencies.py", line 1220, in cythonize_one
    raise CompileError(None, pyx_file)
Cython.Compiler.Errors.CompileError: app/app.py

It is used to type annotate some self method parameters that need to reference itself. I’m guessing that this is a not yet supported feature of CPython 3.7? Is there a way to bypass this without removing the from __future__ import annotations code?

Matic

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
da-woodscommented, Mar 19, 2020

@shaolo1 Glad to know you have it fixed.

Just to try to clarify: the effect of “binding” is to make functions defined in Cython look more like a regular Python function. I think it’s necessary for __annotations __ to appear as an attribute of the function (but I’m not completely sure). It’s enabled by default on def functions anyway.

It isn’t required for the __future__ statement to work.

1reaction
da-woodscommented, Mar 19, 2020

@McSinyx The problem is that from __future__ import annotations should still be valid code. Although the feature is always enabled in Cython (post https://github.com/cython/cython/pull/3285) the __future__ statement shouldn’t break anything.

However @shaolo1 as far as I can tell it didn’t make it in to 0.29.15. It’s in the changelog for Cython 3.0. Given that 0.29.x doesn’t support to, generating an error message is the correct behaviour. If you download the “master” branch it’ll work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

__future__ — Future statement definitions — Python 3.11 ...
__future__ is a real module, and serves three purposes: ... OptionalRelease records the first release in which the feature was accepted. In the...
Read more >
from __future__ import annotations - python - Stack Overflow
In python doc about __future__ there is a table below where it shows that annotations "optional in" 3.7.0b1 and "mandatory in" 4.0 but...
Read more >
Python Type Hints - How to Enable Postponed Evaluation ...
Consider adding from __future__ import annotations to all your files. This can be done automatically with isort. ... After adding the import to ......
Read more >
flake8-future-annotations - PyPI
Verifies python 3.7+ files use from __future__ import annotations if a type is used in the module that can be rewritten using PEP...
Read more >
Annotation issues at runtime - mypy 0.991 documentation
Starting from Python 3.7, you can add from __future__ import annotations to resolve this, as discussed earlier: from __future__ import annotations def f(x ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found