Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility with variable annotations (PEP 526) #1850

Closed
mjos opened this issue Aug 30, 2017 · 9 comments
Closed

Compatibility with variable annotations (PEP 526) #1850

mjos opened this issue Aug 30, 2017 · 9 comments

Comments

@mjos
Copy link

mjos commented Aug 30, 2017

Hi,

when trying to compile the following code:

n: int = 10

which uses PEP 526 style variable annotation (introduced in Python 3.6.0 beta 1) we get the following result from cython 0.26.1:

Error compiling Cython file:
------------------------------------------------------------
...
n: int = 10
^
------------------------------------------------------------

test.pyx:1:1: Syntax error in simple statement list

Is this a known limitation of cython that will be resolved in near future? Our code base uses a lot of variable annotations (helps documenting the code and refactoring) and this issue prevents us from using cython without code changes.

@TeamSpen210
Copy link
Contributor

To allow Cython's various syntax changes, it has it's own parser which will need to be updated to handle those. Issue #1672 is also related.

@scoder
Copy link
Contributor

scoder commented Aug 30, 2017 via email

@scoder
Copy link
Contributor

scoder commented Aug 31, 2017

@mjos: could you provide a set of tests for this feature? That would simplify the implementation. Just write a .py file to put into /tests/run/ that uses different variations of variable annotations, both at the module level and inside of functions.

@mjos
Copy link
Author

mjos commented Aug 31, 2017

@scoder Sure, we will prepare something. Thanks.

@mjos
Copy link
Author

mjos commented Aug 31, 2017

@scoder I have created variable_annotations.py with a buch of examples gathered quickly from the PEP 526. Is it ok like this (see the attachment)?

variable_annotations.zip

@scoder scoder closed this as completed in c0b299e Sep 2, 2017
@scoder
Copy link
Contributor

scoder commented Sep 2, 2017

Implemented in latest master. Could you please give it a try?

@mjos
Copy link
Author

mjos commented Sep 7, 2017

Sorry for my late reply. Works like a charm! Thanks a lot.

@lxkain
Copy link

lxkain commented Oct 25, 2017

I'm really excited about the prospect of using standard python conventions for optional static typing for cython's purposes. This is a great first step towards it! I am wondering, could we also make this work for the case of

cdef double f_c(double x):
    return x**2-x

-->

@cython.cdef
f_c(x: float) -> float:
    return x**2-x

The idea being that pure python mode completely specifies "all the things" that cython needs.

@lxkain
Copy link

lxkain commented Oct 25, 2017

Although, I am not sure how to use python's optional typing to specify the type of i in the following code snippet:

cdef int i  
for i in range(N):
  ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants