Skip to content

[ruff --fix] (v0.0.239) When working with numpy arrays mask == True is not the same as mask is True #2443

@FrancescElies

Description

@FrancescElies

After running ruff --fix the last line of the following script gets reformatted as np.where(mask is True) but in this context is not the same.

import numpy as np

a = np.arange(10)
mask = a > 5
np.where(mask == True)  # equivalent to `mask.nonzero()

Maybe in that context one should not be using np.where(mask == True) but it's equivalent mask.nonzero() instead, at anyrate we have some old code using that pattern.

See below an interactive session showing is not the same.

In [2]: import numpy as np
In [13]: a = np.arange(10)

In [14]: mask = a > 5

In [15]: np.where(mask == True)
Out[15]: (array([6, 7, 8, 9], dtype=int64),)

In [18]: np.where(mask is True)
Out[18]: (array([], dtype=int64),)

What do you think about this corner case?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingtype-inferenceRequires more advanced type inference.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions