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

[BUG] Annotated value in a generator expression misbehaving #5732

Open
behdad opened this issue Sep 28, 2023 · 2 comments
Open

[BUG] Annotated value in a generator expression misbehaving #5732

behdad opened this issue Sep 28, 2023 · 2 comments

Comments

@behdad
Copy link

behdad commented Sep 28, 2023

Describe the bug

[Tested with master branch as well as release on pip.]

When I annotate the type of a local value used in a generator expression, the behvaior is changed.

We faced this in a real environment: fonttools/fonttools#3282

Code to reproduce the behaviour:

import cython

@cython.locals(tolerance=cython.double)
def can_i_has_bug():

    tolerance = 1.0

    print(any( tolerance for _ in [None] ))
    print(any([tolerance for _ in [None]]))

can_i_has_bug()

Expected behaviour

True
True

Actual:

False
True

If I remove the locals annotation, the bug goes away.

OS

Linux

Python version

3.11.4

Cython version

3.0.3.dev0

Additional context

I get the following warning from cython:

warning: bug.py:11:38: local variable 'tolerance' referenced before assignment
@anthrotype
Copy link

the generator expression gets executed in its own new scope, but this is supposed to capture the outer scope so that when tolerance local variable is initialised to 1.0 in the outer scope, the new variable also named tolerance inside the generator's scope is also assigned that value -- and this works in pure Python mode.
But as soon as the variable is typed as C double in the outer scope, then it no longer gets captured by the generator's scope and the tolerance inside the generator is never initialised to 1.0 (probably defaulting to 0.0 hence the any() returning False).
So it looks like a generator cannot capture variables from its outer scope if these are typed, only generic python objects.

there's a similar issue about typing and scope of generator-local variables that might be relevant here:
#3928 (comment)

@behdad
Copy link
Author

behdad commented Sep 29, 2023

Yeah that's what I thought. It's a bug nonetheless.

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

2 participants