Skip to content

BUG: typings: a - b returns NoReturn instead of ndarray #20265

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

Closed
nschloe opened this issue Nov 1, 2021 · 4 comments
Closed

BUG: typings: a - b returns NoReturn instead of ndarray #20265

nschloe opened this issue Nov 1, 2021 · 4 comments

Comments

@nschloe
Copy link
Contributor

nschloe commented Nov 1, 2021

Describe the issue:

Running

pyright foo.py

on

import numpy as np


def myfun(x: np.ndarray, b: np.ndarray):
    v = b - x
    v.copy()

gives

  /tmp/dummy.py:6:7 - error: Cannot access member "copy" for type "NoReturn"
    Member "copy" is unknown (reportGeneralTypeIssues)

The vector operations +, *, / work fine.

NumPy/Python version information:

1.21.3 3.9.7 (default, Sep 10 2021, 14:59:43)
[GCC 11.2.0]

@BvB93
Copy link
Member

BvB93 commented Nov 1, 2021

Duplicate of #20099 and (to some extent) #20072.

Interestingly, it appears that this bug is not limited to mypy (xref python/mypy#11347).
Annoying as it is, I'm wondering if it might be best to temporarily remove the dunder overloads...

@nschloe
Copy link
Contributor Author

nschloe commented Nov 1, 2021

It's funny that only - is concerned, not +, *, or /.

@BvB93
Copy link
Member

BvB93 commented Nov 1, 2021

It's funny that only - is concerned, not +, *, or /.

The problem is that self annotations are ignored when a Any-parametrized object is passed to an overload method.
So what happens is that np.ndarray (or equivalently np.ndarray[Any, Any]) will just pick whatever happens to be the first overload, which turns out to be to NoReturn overload used for boolean arrays:

numpy/numpy/__init__.pyi

Lines 2013 to 2014 in fa397e9

@overload
def __sub__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NoReturn: ...

@BvB93
Copy link
Member

BvB93 commented Nov 1, 2021

Closing this in favor of #20099

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants