Skip to content

TYP: histogram2d doesn't like bins arrays with floats #27092

@ldouteau

Description

@ldouteau

Describe the issue:

Hi,

I'm trying to use histogram2d with user-defined bins as array. The documentation states that bins allow tuple of arrays, which i understand as array of bin edges. Based on this assumption the type of bins arrays should be aligned with the one of the input data. However the typing tool flags the lines that use arrays containing floats.

The piece of code provided in code example runs properly, i.e., all the calls to histogram & histogram2d provide the same output.

I assume the type of bin arrays is off, as Pyright refers to _ArrayLikeInt_co, but i may be missing something related to the usage of the bins inputs

Reproduce the code example:

import numpy as np

n_bins = 3
data = np.linspace(0, 6, 6)
bin_edges = np.linspace(0, 6, n_bins + 1)

np.histogram(data, bins=n_bins)
np.histogram(data, bins=bin_edges)  # Not Flagged
np.histogram2d(data, data, bins=n_bins)
np.histogram2d(data, data, bins=(n_bins, n_bins))
np.histogram2d(data, data, bins=(bin_edges, bin_edges))  # Flagged by pyright
np.histogram2d(data, data, bins=(bin_edges.astype(int), bin_edges.astype(int)))  # Not Flagged

Error message:

Error log obtained with Pyright 1.1.374 (command line)


c:\Users\LouisDouteau\Documents\01-dev\IPL\hist_bins.py
  c:\Users\LouisDouteau\Documents\01-dev\IPL\hist_bins.py:12:1 - error: No overloads for "histogram2d" match the provided arguments (reportCallIssue)
  c:\Users\LouisDouteau\Documents\01-dev\IPL\hist_bins.py:12:33 - error: Argument of type "tuple[NDArray[floating[Any]], NDArray[floating[Any]]]" cannot be assigned to parameter "bins" of type "Sequence[_ArrayLikeInt_co]" in function "histogram2d"
    Type "NDArray[floating[Any]]" is incompatible with type "_ArrayLikeInt_co"
      "ndarray[Any, dtype[floating[Any]]]" is incompatible with protocol "_SupportsArray[dtype[bool | integer[Any]]]"
        "__array__" is an incompatible type
          No overloaded function matches type "() -> ndarray[Any, _DType_co@_SupportsArray]"
      "ndarray[Any, dtype[floating[Any]]]" is incompatible with protocol "_NestedSequence[_SupportsArray[dtype[bool | integer[Any]]]]"    
        "__reversed__" is not present
        "count" is not present
        "index" is not present (reportArgumentType)
2 errors, 0 warnings, 0 informations


### Python and NumPy Versions:

NumPy 2.0.0
Python 3.12.2 (tags/v3.12.2:6abddd9, Feb  6 2024, 21:26:36) [MSC v.1937 64 bit (AMD64)]


### Type-checker version and settings:

Pyright 1.1.374
Issue identified with VSCode's Pylance (latest version)

### Additional typing packages.

_No response_

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions