python / python/mypy

overloads not properly evaluated with mypy 2.3.0 and numpy 2.5.1

Open
#21,733 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-overloads
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no repository file or existing test. Start by saving the supplied reproducer and running it with mypy 2.3.0, then trace overload evaluation for the two array signatures and compare the result with the expected reveal_type output. Done means the call reveals DatetimeArray and a regression test covers this case.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
devtools, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.