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

all(...) rewrite produces incorrect results #5372

Closed
asottile opened this issue Jun 3, 2019 · 4 comments

Comments

Projects
None yet
3 participants
@asottile
Copy link
Member

commented Jun 3, 2019

this is boiled down from a more complicated case

def test():
    y = range(5)
    assert not all(x for x in y)
$ pytest t.py
============================= test session starts ==============================
platform linux -- Python 3.6.7, pytest-4.6.1, py-1.8.0, pluggy-0.12.0
rootdir: /tmp
collected 1 item                                                               

t.py F                                                                   [100%]

=================================== FAILURES ===================================
_____________________________________ test _____________________________________

    def test():
        y = range(5)
>       assert not all(x for x in y)
E       assert 0

t.py:3: AssertionError
=========================== 1 failed in 0.04 seconds ===========================
$ pytest t.py --assert=plain
============================= test session starts ==============================
platform linux -- Python 3.6.7, pytest-4.6.1, py-1.8.0, pluggy-0.12.0
rootdir: /tmp
collected 1 item                                                               

t.py .                                                                   [100%]

=========================== 1 passed in 0.01 seconds ===========================```
@tacaswell

This comment has been minimized.

Copy link

commented Jun 3, 2019

We also ran into issues with list comprehensions inside of all:

def test_foobar():
    events = [{'data': d} for d in [{'modified_motor': -5.0, 'modified_motor_setpoint': -5.0, 'modified_det': -3.726653172078671e-06}, {'modified_motor': -4.0, 'modified_motor_setpoint': -4.0, 'modified_det': -0.00033546262790251185}, {'modified_motor': -3.0, 'modified_motor_setpoint': -3.0, 'modified_det': -0.011108996538242306}, {'modified_motor': -2.0, 'modified_motor_setpoint': -2.0, 'modified_det': -0.1353352832366127}, {'modified_motor': -1.0, 'modified_motor_setpoint': -1.0, 'modified_det': -0.6065306597126334}, {'modified_motor': -0.0, 'modified_motor_setpoint': -0.0, 'modified_det': -1.0}, {'modified_motor': -1.0, 'modified_motor_setpoint': -1.0, 'modified_det': -0.6065306597126334}, {'modified_motor': -2.0, 'modified_motor_setpoint': -2.0, 'modified_det': -0.1353352832366127}, {'modified_motor': -3.0, 'modified_motor_setpoint': -3.0, 'modified_det': -0.011108996538242306}, {'modified_motor': -4.0, 'modified_motor_setpoint': -4.0, 'modified_det': -0.00033546262790251185}]]
    #     dis.dis(test_foobar)

    v = all([evt['data'][key] <= 0
             for evt in events
             for key in evt['data'].keys()])
    assert v
    assert all([evt['data'][key] <= 0
                for evt in events
                for key in evt['data'].keys()])
    key = 1
    assert key
=========================================================== test session starts ============================================================
platform linux -- Python 3.7.3, pytest-4.6.1, py-1.8.0, pluggy-0.12.0
rootdir: /tmp
plugins: cov-2.6.0, faulthandler-1.5.0, rerunfailures-5.0, timeout-1.3.2, forked-0.2, xdist-1.24.0
collected 1 item                                                                                                                           

test2.py F                                                                                                                           [100%]

================================================================= FAILURES =================================================================
_______________________________________________________________ test_foobar ________________________________________________________________

    def test_foobar():
        events = [{'data': d} for d in [{'modified_motor': -5.0, 'modified_motor_setpoint': -5.0, 'modified_det': -3.726653172078671e-06}, {'modified_motor': -4.0, 'modified_motor_setpoint': -4.0, 'modified_det': -0.00033546262790251185}, {'modified_motor': -3.0, 'modified_motor_setpoint': -3.0, 'modified_det': -0.011108996538242306}, {'modified_motor': -2.0, 'modified_motor_setpoint': -2.0, 'modified_det': -0.1353352832366127}, {'modified_motor': -1.0, 'modified_motor_setpoint': -1.0, 'modified_det': -0.6065306597126334}, {'modified_motor': -0.0, 'modified_motor_setpoint': -0.0, 'modified_det': -1.0}, {'modified_motor': -1.0, 'modified_motor_setpoint': -1.0, 'modified_det': -0.6065306597126334}, {'modified_motor': -2.0, 'modified_motor_setpoint': -2.0, 'modified_det': -0.1353352832366127}, {'modified_motor': -3.0, 'modified_motor_setpoint': -3.0, 'modified_det': -0.011108996538242306}, {'modified_motor': -4.0, 'modified_motor_setpoint': -4.0, 'modified_det': -0.00033546262790251185}]]
        #     dis.dis(test_foobar)
    
        v = all([evt['data'][key] <= 0
                 for evt in events
                 for key in evt['data'].keys()])
        assert v
>       assert all([evt['data'][key] <= 0
                    for evt in events
                    for key in evt['data'].keys()])
E       UnboundLocalError: local variable 'key' referenced before assignment

test2.py:10: UnboundLocalError
========================================================= 1 failed in 0.03 seconds =========================================================
(dd37) ✘  /tmp

attn @klauer who distilled this down to a MVP

@tacaswell

This comment has been minimized.

Copy link

commented Jun 3, 2019

Ah, I see #5370 already hit this case, sorry for the noise.

@asottile

This comment has been minimized.

Copy link
Member Author

commented Jun 3, 2019

that one looks more like #5370 fwiw

@asottile

This comment has been minimized.

Copy link
Member Author

commented Jun 3, 2019

this has been released as part of 4.6.2 -- those that were experiencing these issues may need to clean .pyc files after upgrading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.