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.

Pure Python mode does not like -> None annotations

See original GitHub issue

I 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:open
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
da-woodscommented, Apr 26, 2020

@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 from None in future though.

0reactions
TeamSpen210commented, Sep 8, 2021

NoReturn is something entirely different, it’s for a function which never returns, only raises an exception or infinitely loops.

Read more comments on GitHub >

github_iconTop 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 >

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