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

Code marked as unreachable after numpy.cross() #3277

Closed
liam-clink opened this issue Sep 3, 2022 · 5 comments
Closed

Code marked as unreachable after numpy.cross() #3277

liam-clink opened this issue Sep 3, 2022 · 5 comments

Comments

@liam-clink
Copy link

Environment data

  • Language Server version: v2022.8.50
  • OS and version: Windows 10
  • Python version (& distribution if applicable, e.g. Anaconda): 3.10

Code Snippet

def foo(vector1, vector2):
    perp = numpy.cross(vector1, vector2)
    normed_perp = perp/numpy.linalg.norm(perp) # This line and everything after is marked as unreachable

Logs

XXX

Unclear how to do this, I changed the settings.json as instructed and no additional output was generated.

@erictraut
Copy link
Contributor

This appears to be a bug in the type stubs that ship with numpy. It includes the following (first) overload for the cross method:

@overload
def cross(
    a: _ArrayLikeBool_co,
    b: _ArrayLikeBool_co,
    axisa: int = ...,
    axisb: int = ...,
    axisc: int = ...,
    axis: None | int = ...,
) -> NoReturn: ...

If the provided arguments have an unknown type, this first overload will be selected, and a NoReturn return type will be assumed. That means the call will generate an exception or will never return control back to the caller, hence the grayed-out text.

You can work around the issue by providing appropriate type annotations for vector1 and vector2.

The real fix will need to be made by the maintainers of numpy. They should delete the first overload, the one that returns a NoReturn.

@liam-clink
Copy link
Author

Ok I'll close the issue in that case

@Korijn
Copy link

Korijn commented Sep 6, 2022

Has a corresponding issue been opened with the numpy maintainers?

@jabarragann
Copy link

Code snippet to work around the problem

import numpy as np

#option 1
cross1 = lambda x,y:np.cross(x,y)
#option 2
def cross2(a:np.ndarray,b:np.ndarray)->np.ndarray:
    return np.cross(a,b)

def cross_test():

    y_ax1 = cross1(np.array([1, 0, 0]), np.array([0, 1, 0]))
    y_ax2 = cross2(np.array([1, 0, 0]), np.array([0, 1, 0]))
    print(y_ax1)
    return y_ax1,y_ax2

if __name__ == "__main__":
    cross_test()

@janosh
Copy link

janosh commented Sep 21, 2022

@Korijn Yes, see numpy/numpy#22146.

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

5 participants