Skip to content

Reject malformed DATE values in convert_date instead of parsing them - #1301

Open
waw666waw666 wants to merge 1 commit into
PyMySQL:mainfrom
waw666waw666:fix/reject-malformed-date-values
Open

waw666waw666 wants to merge 1 commit into
PyMySQL:mainfrom
waw666waw666:fix/reject-malformed-date-values

Conversation

@waw666waw666

@waw666waw666 waw666waw666 commented Dec 5, 2026 •

Copy link
Copy Markdown

convert_date() split on - and fed the parts to int(), which
silently accepts underscores, signs and surrounding whitespace, so
malformed values like "2007-02-2_6" came back as date(2007, 2, 26)
instead of the original string the docstring promises for illegal
values. The other three converters were fixed the same way in #1280
(strict pattern plus fullmatch); convert_date had no pattern at
all and was the one left behind. Give it a DATE_RE matching the
field widths of DATETIME_RE and require a full match, so only
digit-only components reach the date constructor. This also returns
incomplete values such as "2007-02" unchanged instead of raising
TypeError (issue #1286, superseding the narrower #1287/#1292 for
that symptom).

Tests: added test_convert_date_rejects_non_digit_components and
test_convert_date_rejects_incomplete_values in
pymysql/tests/test_converters.py, plus a doctest line (fail before
the fix, pass after). Server-free suite: 32 passed (baseline 30,
only the 2 new tests added). ruff format and ruff check pass.
The integration suite needs a live MySQL server and was not run
locally; the change only touches a pure decoding function.

Summary by CodeRabbit

  • Bug Fixes
    • Date values are now converted only when they match a complete year-month-day format. Malformed or incomplete values, including values with extra whitespace or characters, are preserved unchanged.
    • Invalid calendar dates continue to be preserved unchanged rather than converted.

convert_date() split on '-' and fed the parts to int(), which silently
accepts underscores, signs and surrounding whitespace, so malformed
values like '2007-02-2_6' came back as date(2007, 2, 26) instead of
the original string the docstring promises for illegal values. The
other three converters were fixed the same way in PyMySQL#1280 (strict
pattern plus fullmatch); convert_date had no pattern at all and was
the one left behind. Give it a DATE_RE matching the field widths of
DATETIME_RE and require a full match, so only digit-only components
reach the date constructor. This also returns incomplete values such
as '2007-02' unchanged instead of raising TypeError (issue PyMySQL#1286,
superseding the narrower PyMySQL#1287/PyMySQL#1292 for that symptom).

Tests: test_convert_date_rejects_non_digit_components and
test_convert_date_rejects_incomplete_values in
pymysql/tests/test_converters.py, plus a doctest line (fail before
the fix, pass after). Server-free suite: 32 passed (baseline 30,
only the 2 new tests added). ruff format and ruff check pass.
@coderabbitai

coderabbitai Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

convert_date now requires date strings to match a year-month-day pattern before parsing. It returns nonmatching or invalid values unchanged. Tests cover malformed and incomplete inputs for convert_date and convert_datetime.

Changes: Date converter validation

Layer / File(s) Summary
Date pattern, parsing, and tests
pymysql/converters.py, pymysql/tests/test_converters.py
convert_date checks for a full match against the date pattern before parsing. Nonmatching inputs and values that raise ValueError are returned unchanged. Tests cover malformed and incomplete strings for both converters.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Possibly related issues

  • PyMySQL/PyMySQL#1286 — The change returns incomplete date strings unchanged, addressing the reported convert_date error.

Possibly related PRs

  • PyMySQL/PyMySQL#1300 — Its convert_datetime fallback path calls convert_date, whose behavior this PR changes.
  • PyMySQL/PyMySQL#1287 — Both PRs change how convert_date handles incomplete date strings.
  • PyMySQL/PyMySQL#1292 — Both PRs address incomplete date strings in convert_date and its convert_datetime fallback path.

Suggested reviewers: methane

Merge Risk: Minimal · up to cc796

This change makes malformed date strings come back unchanged instead of being parsed. No concrete merge-blocking risk was found, and tests cover the new behavior.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title states the primary change: convert_date now rejects malformed DATE values and returns them unchanged instead of parsing them. The raw summary confirms DATE_RE requires a full match before co…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title states the primary change: convert_date now rejects malformed DATE values and returns them unchanged instead of parsing them. The raw summary confirms DATE_RE requires a full match before conversion, and the tests cover malformed inputs. The title is specific and matches the changeset.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 0696f381-f164-4b57-99c6-2283d6c287a7
Commits

Reviewing files that changed from the base of the PR and between 7b9584f and cc796c8.

Files selected for processing: 2

  • pymysql/converters.py
  • pymysql/tests/test_converters.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

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