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.

(@cython.cfunc + Type Annotation) Fragility

See original GitHub issue

Going through some of pandas’ cython modules to try to make code valid-python where possible, I’m seeing cythonize-time errors. It seems like some combinations are invalid for surprising reasons, e.g:

cpdef object get_rule_month(object source, object default='DEC')
# existing, works as expected

cpdef object get_rule_month(source: object, default: object='DEC'):
# seems OK

cpdef get_rule_month(source: object, default: object='DEC') -> object:
# breaks --> Syntax error in C variable declaration

@cython.ccall
def get_rule_month(source: object, default: object='DEC') -> object:
# breaks --> Exception clause not allowed for function returning Python object

@cython.ccall
def object get_rule_month(source: object, default: object='DEC'):
# breaks --> Expected '(', found 'get_rule_month'

@cython.ccall
@cython.returns(object)
def get_rule_month(source: object, default: object='DEC'):
# appears OK

Another example with a slightly different set of errors:

cdef inline bint is_null_datetime64(v):
# existing, OK

cdef inline is_null_datetime64(v) -> bint:
# breaks --> Syntax error in C variable declaration

@cython.cfunc
def inline is_null_datetime64(v) -> bint:
# breaks --> Expected '(', found 'is_null_datetime64

@cython.cfunc
@cython.inline
def is_null_datetime64(v) -> bint:
# breaks --> Function signature does not match previous declaration

This last one here is the one that is noticeably different from the first example.

Is this behavior intentional?

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
Yobmodcommented, Oct 18, 2018

I’m having the similar issues on cython V0.29 with return types in the .pyx file. Any python object return type breaks compilation in python compatible mode. Cython types work for returns (int, float, cython.int, cython.void, etc), np.ndarray and python builtins don’t.

@cython.ccall
def get_object() -> object:  ...
# -> Exception clause not allowed for function returning Python object
@cython.cfunc
def get_str() -> str:  ...
# -> Exception clause not allowed for function returning Python object

Also returning None gives a different error:

@cython.ccall
def do_something_without_return() -> None:  ...
# -> Not a type

which makes it not compatible with typed python even with just builtin types. If returning None is redundant, can cython just ignore it? or alias it to a cython void / NULL? (Should this be a separate issue?)

1reaction
black-squarecommented, Dec 4, 2019

@Yobmod It looks like that there is a workaround for -> object case: If we add a decorator @cython.exceptval(check=False) it works fine. The generated code still checks the return value for exceptions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pure Python Mode — Cython 3.0.0a11 documentation
This is accomplished via an augmenting .pxd file, via Python type PEP-484 type annotations (following PEP 484 and PEP 526), and/or via special...
Read more >
10. Release Notes — Numba 0.39.0rc1+0.g26dde2b.dirty ...
The Numba dispatcher inspect_types() method now supports the kwarg pretty which if set to True will produce ANSI/HTML output, showing the annotated types, ......
Read more >
Pure Python with Cython decorators: How to get attribute ...
@cython.cfunc decorator is an equivalent to cdef ing a function (see here for details), so this function is accessible only within C code....
Read more >
numba Changelog - pyup.io
Numba's internal representations of Python/NumPy types are rendered inside GDB ... Fixes missing type annotation pass following #7704 (`stuartarchibald ...
Read more >
University of Auckland Research Repository, ResearchSpace
described here has type annotations and constant declarations which are checked at ... The Cython implementation (Behnel et al., 2011) is an ahead-of-time ......
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