Skip to content

fix: convert_date raises TypeError for incomplete values - #1292

Open
rahul05ranjan wants to merge 1 commit into
PyMySQL:mainfrom
rahul05ranjan:fix/convert-date-incomplete-values
Open

rahul05ranjan wants to merge 1 commit into
PyMySQL:mainfrom
rahul05ranjan:fix/convert-date-incomplete-values

Conversation

@rahul05ranjan

@rahul05ranjan rahul05ranjan commented Nov 9, 2026 •

Copy link
Copy Markdown

convert_date and convert_datetime document that illegal values are returned unchanged as a string, and the doctests pin that convention (convert_date('2007-02-31') → '2007-02-31', convert_datetime('0000-00-00 00:00:00') → str). But a value with fewer than three --separated components passes the int() conversion and then fails datetime.date()'s arity with a raw TypeError:

>>> from pymysql.converters import convert_date, convert_datetime
>>> convert_date('2007-02')
TypeError: function missing required argument 'day' (pos 3)
>>> convert_datetime('2007-02')
TypeError: function missing required argument 'day' (pos 3)   # via the convert_date fallback

Since both components int()-parse cleanly, the incomplete value slips past the except ValueError that implements the "illegal values are returned as str" contract. A result row containing such a string crashes inside result decoding instead of yielding the value.

Fix: check the component count up front and return the value as str, consistent with every other input the date constructor cannot consume. convert_datetime is covered through its convert_date fallback. Doctest lines pinning convert_date('2007-02') and convert_datetime('2007-02') → '2007-02' are added to the converter docstrings.

Verification (system Python 3.13, no server needed — the converter path is pure decoding):

  • pytest --doctest-modules pymysql/converters.py: 4 passed (13 doctest asserts) with the change; on unpatched main@7b9584f the repro crashes with the raw TypeError above.
  • Full behavior spot-check: '2007-02' → '2007-02' (both converters), '2007-02-31' → unchanged, '2007-02-26' → date(2007, 2, 26), bytes input b'2007-02' → '2007-02', valid datetimes unchanged.
  • The DB-server integration suite cannot run on this host (no MySQL server); the converters doctests are the repo's own documented test entry for this module (pytest --doctest-modules pymysql/converters.py).

Summary by CodeRabbit

  • Bug Fixes
    • Incomplete date strings, such as "2007-02", are now returned unchanged instead of causing an error.

datetime.date(*...) is called with whatever int() accepts from the
dash-separated components, so a value with fewer than three parts
crashes with a raw TypeError (missing required argument 'day') instead
of being returned as str like every other illegal value per the
documented contract. convert_datetime reaches the same crash through
its convert_date fallback, taking down result decoding.

Check the component count up front and return the value as str, and pin
the behavior in the converter doctests.

Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com>
@coderabbitai

coderabbitai Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

convert_date now returns incomplete date strings unchanged instead of passing them to the date constructor. Doctests document this behavior for convert_date and convert_datetime.

Changes: Date conversion

Layer / File(s) Summary
Incomplete date fallback
pymysql/converters.py
convert_date checks for exactly three hyphen-separated parts before parsing. Doctests for convert_date and convert_datetime document that incomplete date strings are returned unchanged.

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

Change: Bug fix

Possibly related issues

Possibly related PRs

  • PyMySQL/PyMySQL#1287: This PR changes convert_date to handle the same incomplete-date case using a different check.

Suggested reviewers: methane

Merge Risk: Minimal · up to 00d86

Incomplete dates are intended to remain strings. No merge-blocking issue is identified.

Pre-merge checks: Passed 5

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title is a conventional-commit style summary that names the specific bug and function. The PR changes convert_date so that incomplete date strings with fewer than three hyphen-separated parts are …
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
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 is a conventional-commit style summary that names the specific bug and function. The PR changes convert_date so that incomplete date strings with fewer than three hyphen-separated parts are returned unchanged instead of raising TypeError. The title matches the main change, so it passes.

Full details: Docstring Coverage

Explanation

Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 2 functions across 1 files.

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: 668d65cf-4913-4286-a7f4-581999a33241
Commits

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

Files selected for processing: 1

  • pymysql/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.

@rahul05ranjan

Copy link
Copy Markdown
Author

Hi! Friendly reminder — convert_date still raises TypeError for incomplete values on current main, and the fix here is green. Would appreciate a look when you get a chance.

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