130 days ago · Tech · hide · Fediverse post

Take this code, parsing the date of an annually recurring event that only has a month and day, such as a work anniversary: from datetime import datetime def parse_anniversary(date_str: str) -> datetime: parsed = datetime.strptime(date_str, "%d/%m") return parsed.day, parsed.month With Python 3.13 or 3.14, run it on January 1st, and it will log a warning, but still parse the date: >>> parse_anniversary("01/01") /.../example.py:7: DeprecationWarning: Parsing dates involving a day of month without a year specified is ambiguous and fails to parse leap day. The default behavior will change in Python 3.15 to either always raise an exception or to use a different default year (TBD). To avoid trouble, add a specific year to the input & format. See https://github.com/python/cpython/issues/70647. parsed = datetime.strptime(date_str, "%d/%m") (1, 1) Note the returned value’s year is 1900. But, parse February 29th, and the warning is followed by a ValueError: >>> parse_anniversary("29/02")…

Commenting on Bubbles has been removed. Why? If you want to talk about this post, write to the author, use the comments on their blog if it has them, or reply with a "RE:" post on your own blog. Suggestions? feedback@bubbles.town