Skip to content

Conversation

@TroyDowling
Copy link

@TroyDowling TroyDowling commented Jan 15, 2026

This small patch revises the string_to_date function in typing.py to more gracefully handle parsing dates without a year. The motivation being to address the DeprecationWarning issued by Python in recent versions (and to avoid a possible breakage in the upcoming Python 3.15).

This closes #335 on the bug board.

  • Interpolates the placeholder year 1900 for the two ambiguous formats.
  • Formatted with ruff.

Before

................./home/troy/projects/khard/khard/helpers/typing.py:76: DeprecationWarning: Parsing dates i
nvolving 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.
  return datetime.strptime(string, fmt)
.................../home/troy/projects/khard/khard/cli.py:208: PendingDeprecationWarning: FileType is depr
ecated. Simply open files after parsing arguments.
  type=argparse.FileType("w"),
..........................................................................................................
....................................................................................ss....................
.....................................................................x....................................
.............................................
----------------------------------------------------------------------
Ran 399 tests in 0.326s

OK (skipped=2, expected failures=1)

After

..................................../home/troy/projects/khard/khard/cli.py:208: PendingDeprecationWarning:
 FileType is deprecated. Simply open files after parsing arguments.
  type=argparse.FileType("w"),
..........................................................................................................
....................................................................................ss....................
.....................................................................x....................................
.............................................
----------------------------------------------------------------------
Ran 399 tests in 0.327s

OK (skipped=2, expected failures=1)

and

$ python -W error::DeprecationWarning -m unittest test.test_helpers_typing.StringToDate
..........
----------------------------------------------------------------------
Ran 10 tests in 0.003s

OK

+ Python now throws a DeprecationWarning when trying to parse a date
  which has no year.
+ This small patch adds the special handling required to avoid this.
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.

DeprecationWarning on python3.13

1 participant