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

Compilation error for code with forward reference hint #1887

Closed
petrstupka opened this issue Sep 26, 2017 · 3 comments
Closed

Compilation error for code with forward reference hint #1887

petrstupka opened this issue Sep 26, 2017 · 3 comments

Comments

@petrstupka
Copy link

petrstupka commented Sep 26, 2017

Hello,
when trying to compile following code (simplified example):

class A:
    def __init__(self, x):
        self.x = x
    def get_x(self) -> 'X':
        return self.x

class X:
    pass

(with cython 0.27) I get error message

------------------------------------------------------------
...
    def __init__(self, x):
        self.x = x
    def get_x(self) -> 'X':
        return self.x

class X:
^
------------------------------------------------------------

test.py:7:0: Assignment to non-lvalue 'X'

After removing forward reference type hint -> 'X' from A.get_x() method, compilation proceeds without error.
It seems more like a parser's problem.

@scoder
Copy link
Contributor

scoder commented Sep 26, 2017

Thanks for the report. What happens is that Cython looks up X as a type in the annotation, does not find it (yet), and assumes that it's something from an outer scope, i.e. a builtin. When it then finds the class definition, it tries to assign the class object to the assumed builtin X, and rejects it as invalid.

@scoder scoder added this to the 0.27.1 milestone Sep 26, 2017
@scoder scoder closed this as completed in 1a0c132 Sep 26, 2017
@scoder
Copy link
Contributor

scoder commented Sep 27, 2017

Fixed in 0.27.1b1. Please give it a try.

@petrstupka
Copy link
Author

Hi,
thank you very much for the fix. I have already checked 0.27 version with your patch applied. It works for test case and also works for all real cases occuring in my python projects.

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

No branches or pull requests

2 participants