Skip to content

[python-dateutil] relativedelta accepts float durations - #16782

Open
maringuadarrama wants to merge 1 commit into
python:mainfrom
maringuadarrama:relativedelta-float-durations
Open

maringuadarrama wants to merge 1 commit into
python:mainfrom
maringuadarrama:relativedelta-float-durations

Conversation

@maringuadarrama

Copy link
Copy Markdown

relativedelta.__init__ types days, weeks, hours, minutes, seconds and microseconds as int, but dateutil accepts floats for all of them and normalises them in _fix():

>>> from datetime import datetime
>>> from dateutil.relativedelta import relativedelta
>>> datetime(2024, 3, 1) + relativedelta(hours=1.5)
datetime.datetime(2024, 3, 1, 1, 30)
>>> relativedelta(hours=25.5)
relativedelta(days=+1, hours=+1.5)
>>> relativedelta(weeks=0.5)
relativedelta(days=+3.5)

Only years and months must be integral (ValueError: Non-integer years and months are ambiguous and not currently supported.), so they stay int, as do the absolute fields (year=, hour=, ...), which go to datetime.replace(). leapdays also accepts a float at runtime but counts days added in a leap year, so it stays int here.

The attributes stay int as well. A relativedelta built from float durations holds floats, so float would be the exact type, but widening the attributes would break existing code that reads rd.days as an int. I can widen them too if you'd prefer the exact types.

🤖 Generated with Claude Code

relativedelta(days=1.5), weeks=, hours=, minutes=, seconds= and
microseconds= are accepted at runtime and normalised by _fix(); only
years and months must be integral (ValueError otherwise), so they stay int.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant