Skip to content

BUG: Numpy Typing does not recognize a deque as a valid input for np.max #24265

Closed

Description

@chacabuco

Describe the issue:

When running mypy on the example code one gets the following message:

error: No overload variant matches argument type "deque[int]" [call-overload]

Nonetheless, the code executes correctly. Should collections' deques be a valid input for both np.min and np.max?

When trying to convert a deque to a numpy array the conversions is successful; therefore, shouldn't deques be considered as array_like?

Reproduce the code example:

import numpy as np
from collections import deque

a = deque([0])
np.max(a)

Error message:

error: No overload variant matches argument type "deque[int]"  [call-overload]

Runtime information:

1.23.2
3.10.6 (main, May 29 2023, 11:10:38) [GCC 11.3.0]

Context for the issue:

No response

Activity

  1. BvB93 commented on Jul 26, 2023

    @BvB93
    Member

    The issue here stems from deque failing to properly implement the interface as specified by the Sequence abstract base class (which we rely on in our typing definition of array-like), the former lacking support for slice-based indexing. Loosening the __getitem__ annotations down below via removal of the slice-based overload might be worth a shot here:

    @overload
    def __getitem__(self, index: int, /) -> _T_co | _NestedSequence[_T_co]: ...
    @overload
    def __getitem__(self, index: slice, /) -> _NestedSequence[_T_co]: ...
    def __getitem__(self, index, /):
    """Implement ``self[x]``."""
    raise NotImplementedError

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