Skip to content

np.float64 is not accepted as float under static type checks #21906

Closed
@0xjc

Description

@0xjc

Some of the built-in scalar types inherit from the corresponding Python types. However, this fact does not seem to be exposed by the numpy type stubs.

# Python version 3.8.10, numpy version 1.23.0
import numpy as np

x = np.float64(1.0)
print(f"x={x} of type={type(x)} is float: {isinstance(x, float)}")
# x=1.0 of type=<class 'numpy.float64'> is float: True


def f(a: float) -> float:
    return a


f(x)
"""
pyright 1.1.256:
error: Argument of type "float64" cannot be assigned to parameter "a" of type "float" in function "f"
  "float64" is incompatible with "float" (reportGeneralTypeIssues)

mypy 0.961:
error: Argument 1 to "f" has incompatible type "floating[_64Bit]"; expected "float"
"""

Replacing np.float64 with np.float_ or np.double does not help.

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