Skip to content

TYP: fix unit-dependent timedelta64/datetime64 overloads - #31874

Merged
charris merged 1 commit into
numpy:mainfrom
jorenham:typing/_TimeUnitSpec-typar
Jul 6, 2026
Merged

charris merged 1 commit into
numpy:mainfrom
jorenham:typing/_TimeUnitSpec-typar

Conversation

@jorenham

@jorenham jorenham commented Jul 6, 2026

Copy link
Copy Markdown
Member

The _TimeUnitSpec type alias type parameter was unused, leading to incorrect inference for the timedelta64 and datetime64 constructors:

reveal_type(np.timedelta64(5, 'D').item())          # int
reveal_type(np.datetime64('2021-01-01', 's').item())  # int
reveal_type(np.datetime64(dt.date(2021, 1, 1), 'ns').item())  # date
int(np.timedelta64(5, 'D'))                          # accepted by checkers

Clearly, these are all wrong.

Luckily it's a pretty trivial fix.


Claude Fable 5 (high) discovered this bug. but no AI used for the fix.

@jorenham jorenham added 09 - Backport-Candidate PRs tagged should be backported 41 - Static typing labels Jul 6, 2026
@github-actions

github-actions Bot commented Jul 6, 2026

Copy link
Copy Markdown

Diff from mypy_primer, showing the effect of this PR on type check results on a corpus of open source code:

pandas-stubs (https://github.com/pandas-dev/pandas-stubs)
- tests/scalars/test_scalars.py:570: error: Unused "type: ignore" comment  [unused-ignore]
- tests/scalars/test_scalars.py:618: error: Unused "type: ignore" comment  [unused-ignore]

@charris
charris merged commit 5375552 into numpy:main Jul 6, 2026
14 checks passed
@charris

charris commented Jul 6, 2026

Copy link
Copy Markdown
Member

Thanks Joren.

@jorenham
jorenham deleted the typing/_TimeUnitSpec-typar branch July 6, 2026 15:21
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Jul 6, 2026
MaanasArora pushed a commit to MaanasArora/numpy that referenced this pull request Jul 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants