Open the context
Choose the project folder and name the files or behavior involved.
Shift a date by whole calendar months and clamp its day to the target month's final day. Check January 31 in leap and non-leap years plus a negative shift, rejecting fractional offsets and years outside the supported range.
Choose the project folder and name the files or behavior involved.
Describe the input, expected output and boundaries that must stay intact.
Review the proposed edits and any commands before relying on their result.
Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.
from calendar import monthrange
from datetime import date
def add_months(day, delta):
if type(day) is not date or type(delta) is not int:
raise ValueError("month.input")
year, month0 = divmod(day.year * 12 + day.month - 1 + delta, 12)
if not 1 <= year <= 9999:
raise ValueError("month.year")
last = monthrange(year, month0 + 1)[1]
return date(year, month0 + 1, min(day.day, last))The example is a starting point for a real project, not a claim that generated code is already tested. Adjust paths, dependencies and expected behavior to match your application.