cython: PEP 526 is not implemented for all cases
Bug I suspect that cython just skips annotations if they are not in a function or in a class, so as a result, the types of variables declared in this way are python objects.
To Reproduce Code to reproduce the behaviour:
# cython: annotation_typing=True
import cython
a: cython.int
b: cython.int = 5
# cython.typeof(a) # CompileError - undeclared name not builtin: a
cython.typeof(b) # Python_object and no indication of it!
An easy way to understand this is to use annotate=True:

Expected behavior Same as if I compiled this:
import cython
cdef int a
cdef int b = 5
cython.typeof(a) # int
cython.typeof(b) # int

That is, so that both a and b are of type int, if for some reason this is not the correct behavior, then either an exception or a warning about it.
Environment:
- OS: Microsoft Windows 10
- Python version Python: 3.9.0
- Checked Cython versions: 3.0a7, 0.29.23
Additional context I also checked the rest of the options, here:
# cython: annotation_typing=True
import cython
# same with pure python analogs,
# e.g. @cython.cclass, @cython.cfunc or @cython.ccall
cdef class Foo:
cdef int a # cannot be initialized here
b: cython.int
c: cython.int = 4 # on 0.29: AssertionError: Python global or builtin not a Python object
cython.declare(d=cython.int) # no initialization functionality in this case
e = cython.declare(cython.int) # cannot be normaly initialized here (#4220)
# or cdef or cpdef; function body code is practically the same
def bar(self):
cython.typeof(self.a) # int
cython.typeof(self.b) # int
cython.typeof(self.c) # Python_object but warned on compilation with 3.0a7
cython.typeof(self.d) # int
cython.typeof(self.e) # int
cdef int a
cdef int b = 5
c: cython.int
d: cython.int = 5
cython.declare(e=cython.int) # no initialization functionality in this case
f = cython.declare(cython.int)
g = cython.declare(cython.int, 5)
cython.typeof(a) # int
cython.typeof(b) # int
# cython.typeof(c) # CompileError - undeclared name not builtin: c
cython.typeof(d) # Python_object and no indication of it!
cython.typeof(e) # int
cython.typeof(f) # int
cython.typeof(g) # int
def baz():
cdef int a
cdef int b = 6
c: cython.int
d: cython.int = 6
cython.declare(e=cython.int) # no initialization functionality in this case
f = cython.declare(cython.int)
g = cython.declare(cython.int, 6)
cython.typeof(a) # int
cython.typeof(b) # int
cython.typeof(c) # int
cython.typeof(d) # int
cython.typeof(e) # int
cython.typeof(f) # int
cython.typeof(g) # int
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 19 (19 by maintainers)
Duplicate of https://github.com/cython/cython/issues/3944
Discussion of how to expose C module variables is in https://github.com/cython/cython/issues/3959