Skip to content

TYP: timedelta64.__divmod__ incorrect inference #28256

@randolf-scholz

Description

@randolf-scholz

Describe the issue:

Using divmod widens generic type of timedelta64. The last overload should probably use Self instead of timedelta64, or possible add an overload for the timedelta case.

numpy/numpy/__init__.pyi

Lines 4472 to 4477 in 6bc9058

@overload
def __divmod__(self: timedelta64[None], x: timedelta64, /) -> tuple[int64, timedelta64[None]]: ...
@overload
def __divmod__(self: timedelta64[dt.timedelta], x: dt.timedelta, /) -> tuple[int, dt.timedelta]: ...
@overload
def __divmod__(self, x: timedelta64, /) -> tuple[int64, timedelta64]: ...

Reproduce the code example:

from datetime import timedelta as TD
from typing import assert_type

import numpy as np

td = np.timedelta64(1, "D")
assert_type(td, np.timedelta64[TD])  # ✅

n, remainder = divmod(td, td)
assert_type(remainder, np.timedelta64[TD])  # ❌ timedelta64[timedelta | int | None]

Python and NumPy Versions:

2.2.2
3.13.1 (main, Dec 4 2024, 08:54:14) [GCC 11.4.0]

Type-checker version and settings:

mypy 1.4.1
pyright 1.1.393

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions