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 is unexpectedly marked as unreachable #3195

Closed
xingjianleng opened this issue Aug 14, 2022 · 2 comments
Closed

Code is unexpectedly marked as unreachable #3195

xingjianleng opened this issue Aug 14, 2022 · 2 comments

Comments

@xingjianleng
Copy link

Environment data

  • Language Server version: Pylance language server 2022.8.20 (pyright 888eb243)
  • OS and version: macOS 12.5 (arm64)
  • Python version (& distribution if applicable, e.g. Anaconda): Miniconda CPython 3.9.12

Code Snippet

import numpy as np


x = np.array([1,2,3])
y = np.array([2,3,4])

z = np.cross(x, y)
print(z)

Repro Steps

  1. Install numpy dependencies and run the above code.

Expected behavior

Code should not be marked as unreachable

Actual behavior

The last line of code print(z) is marked as unreachable.

It is also observed in other Python scripts where sections of code followed by np.cross() are marked as unreachable.

@erictraut
Copy link
Contributor

The numpy library includes a type stub file called core/numeric.pyi that declares the cross function. You can see this if you right click on cross in your code and choose "Go to Declaration". The numpy type stub defines the type as an overloaded function — i.e. an ordered series of function signatures that describe different return types based on the types of the input arguments. The first overload for the cross function is declared as:

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

The NoReturn type tells a static type analyzer that the function will always raise an exception.

The the two arguments you are passing to np.cross have the type NDArray[Any], and this is type compatible with _ArrayLikeBool_co. So this overload is being selected.

Given the type information provided to pylance, it appears to be correctly analyzing the code and concluding that the call to np.cross in this case always raises an exception. If you think this is incorrect, the problem would need to be fixed within the numpy library. You could report the issue in the numpy issue tracker.

@xingjianleng
Copy link
Author

Thanks for your clarification Eric!

I just found this link which also illustrates why code is marked as unreachable in this case.

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

2 participants