SyntaxError involving unpack operator during CI tests on GitHub

Hello everyone. I encountered a CI test error when I pushed my code to GitHub. My code involves a 2D numpy.ndarray a and an index pos. The code runs without errors locally, but during the GitHub CI test, the following SyntaxError is triggered:

===========================================================================

While Executing Cell #19:
  Cell In[1], line 1
    a[*pos], b[*pos]
          ^
SyntaxError: invalid syntax

nbdev Tests Failed On The Following Notebooks:
==================================================
    00_core.ipynb
Error: Process completed with exit code 1.

How can I fix it?


Update: The code that can lead to the above-mentioned error in GitHub Action.

import numpy as np

r = np.random.rand(5, 5)
a = lower_triangular = np.tril(r, k = -1) + np.tril(r, k = -1).T
b = np.exp(-a)
pos = np.where(a == a.max())[0]
a[*pos], b[*pos]

Hi @locc
I’m not sure if this will help, from the limited code that I can see, I am assuming you are trying to index locations in array. Here’s a trivial example,


*pos actually does value unpacking,

Here, print(pos) prints the entire numpy array, whereas print(*pos) gives the print command each element of the array separately to print, which is technically equivalent to print(pos[0], pos[1],...)

Hello @deven367,

Thank you for replying.

I provided the code that can lead to the above-mentioned error in GitHub Action.

It would be grateful if you could take a look at it:)

Just use, a[pos] and b[pos], that should fix the issue.

Yes, the error disappeared when a[pos], b[pos] was used. However, a[pos], b[pos] and a[*pos], b[*pos] did not give the same result.
image

Yes, that is because of the way array indexing works in numpy. The value of pos in your case is [2,3].

While using

a = np.array(....) # a.shape = (5,5)
pos = np.array(...) # pos = [2,3]

# this is equivalent to a[2:4, :] meaning choose the second row 
# and third row and choose all the columns
a[pos] 

# whereas what you need is 
a[pos[0], pos[1]]
# choose the value of the array at the index 2,3

Thank you @deven367 !

Actually, I’m still curious why a[*pos] doesn’t work in GitHub Action.

You could check the numpy version is same in your local env vs the one running the GH action.

It seems GitHub uses numpy-1.26.4, just like my local version.

Using cached numpy-1.26.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (18.2 MB)

I would like to use a[*pos] rather than [pos[0], pos[1]] because the rank of this tensor could be arbitrary, not just 2. Now, I have found that I can use a[tuple(pos)] to make the CI test run successfully while accommodating tensors of different ranks.