Skip to content

overloads not properly evaluated with mypy 2.3.0 and numpy 2.5.1 #21733

Description

@Dr-Irv

Bug Report

The final reveal_type() below should show DatetimeArray, but it is showing Any

With pyright, ty and pyrefly, it produces the correct result. May be related to #21608

To Reproduce

import numpy as np
from typing import Any, Sequence, overload, reveal_type


class DatetimeArray:
    pass


class NumpyExtensionArray:
    pass


@overload
def array(  # pyright: ignore[reportOverlappingOverload]
    data: Sequence[np.datetime64],
) -> DatetimeArray: ...
@overload
def array(data: Sequence[Any]) -> NumpyExtensionArray: ...
def array(data: Sequence[Any]) -> DatetimeArray | NumpyExtensionArray:
    return (
        DatetimeArray() if isinstance(data[0], np.datetime64) else NumpyExtensionArray()
    )


foo1 = [np.datetime64("2026-01-05 23:27:59")]
foo0 = np.datetime64("2026-01-05 23:27:59")

reveal_type(foo0)
reveal_type(foo1)
reveal_type(array(foo1))  # Should be DatetimeArray

Expected Behavior

numpy_datetimearray.py:28: note: Revealed type is "numpy.datetime64[Any]"
numpy_datetimearray.py:29: note: Revealed type is "list[numpy.datetime64[Any]]"
numpy_datetimearray.py:30: note: Revealed type is "DatetimeArray"

Actual Behavior

numpy_datetimearray.py:28: note: Revealed type is "numpy.datetime64[Any]"
numpy_datetimearray.py:29: note: Revealed type is "list[numpy.datetime64[Any]]"
numpy_datetimearray.py:30: note: Revealed type is "Any"

Your Environment

  • Mypy version used: 2.3.0
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.12
  • Numpy version used: 2.5.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions