Skip to content

TYP: np.dtype is not Hashable #24621

@twoertwein

Description

@twoertwein

Describe the issue:

Pyright (and python) infer np.dtype (and other sub-classes of np._ArrayOrScalarCommon) as not Hashable as they overwrite __eq__ without overwriting __hash__.

xref microsoft/pyright#5878

Reproduce the code example:

from typing import Hashable
import numpy as np

def test(x: Hashable) -> None: ...

def foo() -> np.dtype: ...

test(foo())

Error message:

error: Argument of type "dtype[Unknown]" cannot be assigned to parameter "x" of type "Hashable" in function "test"
    "dtype[Unknown]" is incompatible with protocol "Hashable"
      "__hash__" is an incompatible type
        Type "None" cannot be assigned to type "(self: dtype[Unknown]) -> int" (reportGeneralTypeIssues)

Runtime information:

>>> import sys, numpy; print(numpy.__version__); print(sys.version)
1.24.4
3.11.4 (main, Jun 14 2023, 18:15:32) [GCC 12.2.0]
>>> print(numpy.show_runtime())
WARNING: `threadpoolctl` not found in system! Install it by `pip install threadpoolctl`. Once installed, try `np.show_runtime` again for more detailed build information
[{'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
                      'found': ['SSSE3',
                                'SSE41',
                                'POPCNT',
                                'SSE42',
                                'AVX',
                                'F16C',
                                'FMA3',
                                'AVX2'],
                      'not_found': ['AVX512F',
                                    'AVX512CD',
                                    'AVX512_KNL',
                                    'AVX512_KNM',
                                    'AVX512_SKX',
                                    'AVX512_CLX',
                                    'AVX512_CNL',
                                    'AVX512_ICL']}}]
None

Context for the issue:

No response

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