3.x issue: cython.inline with class type annotations

31 views
Skip to first unread message

Martin Strobel (rullepœlse)

unread,
Apr 7, 2024, 10:02:54 AMApr 7
to cython-users
HI,

I've been using a type annotation of some sort as below which worked fine up to 0.29.36.

import cython
import inspect

class _Signal:
pass

def test_inline():
scope = cython.inline("""

def unit_interface(a : _Signal, q : _Signal):
return []

""", globals = test_inline.__globals__)

f = scope['unit_interface']

print(inspect.getfullargspec(f))
sig = inspect.signature(f)

ann = f.__annotations__
print('DEBUG ANNOTATIONS', ann)
assert issubclass(ann['a'], _Signal)

The reasons for using inline compilation is that code is being translated first, then compiled. When I write this code out to a .pyx and build an extension from it, all works fine.

The interesting thing is that the class type annotations are obviously seen as strings:

DEBUG ANNOTATIONS {'a': '_Signal', 'q': '_Signal'}

When the class definitions are added to the inline-compiled source string, the compiler seems to accept the definition, however my internal type check would choke on the string type annotation.

Is this a bug, or something I've been missing?


da-woods

unread,
Apr 7, 2024, 10:10:10 AMApr 7
to cython...@googlegroups.com
Cython 3 switched to "annotations as strings" as specified in https://peps.python.org/pep-0563/.

At the time we wrote it this was seen as the "__future__" so it seemed like a sensible thing to do. And solved a problem for us because compiling annotations was pretty fragile in places.

Since then, Python has changed its mind about the future behaviour (but not yet implemented their replacement yet).  We'll probably implement the "real" behaviour once Python finalizes their decision. But for now, Cython's ended up with something a bit odd.
--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cython-users/2a292de8-16db-4f4f-b073-23c5e03a6521n%40googlegroups.com.


Reply all
Reply to author
Forward
0 new messages