Pure Python mode does not like -> None annotations
See original GitHub issueI am trying to change my already heavily typed Python code to run in Cython. For that, it felt useful to put @cython.ccall
everywhere and see what happens.
I found out that Cython does not understand the -> None
function return type annotation as intended. Let’s take this dumb function.
def do_something(l):
if len(l) == 0:
return
l.append(1)
This works fine with the decorator.
import cython
@cython.ccall
def do_something(l):
if len(l) == 0:
return
l.append(1)
It’s type annotation is
def do_something(l: List[int]) -> None:
or
def do_something(l: List[int]) -> type(None):
but with both these type annotations, Cython complains
Error compiling Cython file:
------------------------------------------------------------
...
from typing import List, NoneType
import cython
@cython.ccall
def do_something(l: List[int]) -> type(None):
^
------------------------------------------------------------
test.pyx:5:38: Not a type
Error compiling Cython file:
------------------------------------------------------------
...
import cython
@cython.ccall
def do_something(l: List[int]) -> type(None):
if len(l) == 0:
return
^
------------------------------------------------------------
test.pyx:7:8: Return value required
Cython should know to treat None return types special.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Pure Python Mode — Cython 3.0.0a11 documentation
In pure mode, you are more or less restricted to code that can be expressed (or at least emulated) in Python, plus static...
Read more >Cython Pure Python Mode: Not a type - Stack Overflow
I have a class object that I am trying to cythonize, I added some decorators for a start. My actual class is slightly...
Read more >Faster Python made easier with Cython's pure Python mode
First, pure Python mode doesn't support the full range of PEP 484 type annotations. Annotations such as Final or Union aren't respected. The ......
Read more >Python: Why None is NOT Nothing - Level Up Coding
print("Pure magic!") At first glance it looks like a function taking no input returning nothing, as it does not have a return statement....
Read more >pure Python mode: .pxd and @locals - Google Groups
This specific example will definitely not work as expected, but providing ... /23784564/cython-pure-python-mode-pxd-and-locals>but unfortunately no one ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@Anaphory If you just want to make your code work, you can add
@cython.returns(cython.void)
as a decorator. It’d definitely be nice to infer this fromNone
in future though.NoReturn is something entirely different, it’s for a function which never returns, only raises an exception or infinitely loops.