Skip to content

[MRG] Update pytest #186

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

Merged

Conversation

wdevazelhes
Copy link
Member

This makes us update to the lastest pytest

@wdevazelhes wdevazelhes changed the title Update pytest [MRG] Update pytest Mar 20, 2019
@wdevazelhes
Copy link
Member Author

Apparently I used fixtures in a bad way sometimes, which was still supported in old pytest versions but not anymore so I deleted fixtures and replaced them by regular functions when needed

tuples_no_prep = np.array([[['img1.png'], ['img2.png']],
[['img3.png'], ['img5.png']]])
tuples_no_prep = tuples_no_prep.astype(object)
tuples_no_prep_bis = np.array([[['img1.png'], ['img2.png']],
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I let tuples_no_prep here, I have an error saying tuples_no_prep is referenced before assignment... I didn't understand why ? Since tuples_no_prep is a function defined above in the module ? But changing into tuples_no_prep_bis it works
(I tried with a simple example and I still don't understand: for instance this script returns UnboundLocalError: local variable 'a' referenced before assignment when executed:

def a():
    return 5

def test():
    c = a()
    a = 3

if __name__ == '__main__':
    test()

Does python automatically detects that the variable "a" is assigned in "test" and that it must be a variable local to "test" ?)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, Python treats local variables specially at the bytecode level. Here's the result of dis.dis(test) using your example code:

  5           0 LOAD_FAST                0 (a)
              3 CALL_FUNCTION            0
              6 STORE_FAST               1 (c)

  6           9 LOAD_CONST               1 (3)
             12 STORE_FAST               0 (a)
             15 LOAD_CONST               0 (None)
             18 RETURN_VALUE

The LOAD_FAST opcode is specifically looking for a local variable named a, which is why you get the UnboundLocalError.

Changing the assignment of a to a different name results in a LOAD_GLOBAL opcode, which works as expected:

In [5]: def test2():
   ...:     c = a()
   ...:     b = 3
   ...:

In [6]: test2()

In [7]: dis.dis(test2)
  2           0 LOAD_GLOBAL              0 (a)
              3 CALL_FUNCTION            0
              6 STORE_FAST               0 (c)

  3           9 LOAD_CONST               1 (3)
             12 STORE_FAST               1 (b)
             15 LOAD_CONST               0 (None)
             18 RETURN_VALUE

You could also mark a global in the function, but this will result in overwriting the module-level function:

In [8]: def test3():
   ...:     global a
   ...:     c = a()
   ...:     a = 3
   ...:

In [9]: test3()

In [10]: test3()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-5dea5f7ba8cb> in <module>()
----> 1 test3()

<ipython-input-8-32a55428a886> in test3()
      1 def test3():
      2     global a
----> 3     c = a()
      4     a = 3
      5

TypeError: 'int' object is not callable

In [11]: dis.dis(test3)
  3           0 LOAD_GLOBAL              0 (a)
              3 CALL_FUNCTION            0
              6 STORE_FAST               0 (c)

  4           9 LOAD_CONST               1 (3)
             12 STORE_GLOBAL             0 (a)
             15 LOAD_CONST               0 (None)
             18 RETURN_VALUE

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very interesting, thanks ! :)

@perimosocordiae perimosocordiae merged commit 3490349 into scikit-learn-contrib:master Mar 21, 2019
@perimosocordiae
Copy link
Contributor

Merged, thanks!

@wdevazelhes wdevazelhes deleted the update_pytest branch March 21, 2019 14:37
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

Successfully merging this pull request may close these issues.

2 participants