Skip to content

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

Closed

Description

@nschloe

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]

Activity

  1. BvB93 commented on Nov 1, 2021

    @BvB93
    Member

    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...

  2. nschloe commented on Nov 1, 2021

    @nschloe
    ContributorAuthor

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

  3. BvB93 commented on Nov 1, 2021

    @BvB93
    Member

    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: ...

  4. BvB93 commented on Nov 1, 2021

    @BvB93
    Member

    Closing this in favor of #20099

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions