You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import cython
@cython.locals(tolerance=cython.double)
defcan_i_has_bug():
tolerance =1.0print(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
The text was updated successfully, but these errors were encountered:
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)
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:
Expected behaviour
Actual:
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:
The text was updated successfully, but these errors were encountered: