Repository navigation
convert_date: return str for incomplete values instead of raising TypeError - #1287
rahul05ranjan wants to merge 1 commit into
Conversation
…eError The documented contract (and existing doctests) for the DATE/TIME converters is that illegal values are returned unchanged as str, but a value with fewer than three '-'-separated components reached the datetime.date() call with too few arguments and raised a raw TypeError from deep inside result decoding (also reachable through convert_datetime's fallback). Catch TypeError alongside ValueError and pin the behavior with a doctest. Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com>
|
No actionable comments were generated in the recent review.
|
| Layer / File(s) | Summary |
|---|---|
Invalid date fallback pymysql/converters.py |
convert_date catches TypeError as well as ValueError. The doctest includes 2007-02 as an invalid value. |
Priority:
Estimated code review effort: 1 (Trivial) | ~5 minutes
Change: Bug fix · Severity of issue fixed: Medium
Suggested reviewers: methane
Merge Risk: 
Minimal · up to 415bc
-
The incomplete-date handling is narrowly scoped and introduces no identified merge-blocking risk.
Pre-merge checks: 
5
-
Full details: Linked Issues check
-
Explanation
The change satisfies issue
#1286.convert_datenow catchesTypeErrorfromdatetime.datearity validation and returns the original value. The new doctest verifiesconvert_date('2007-02') == '2007-02'.convert_datetimeroutes incomplete values throughconvert_date, so the reported fallback case is also covered by the implementation.
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 1 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.
Comment @coderabbitai help to get the list of available commands.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1287 +/- ##
==========================================
- Coverage 86.24% 85.15% -1.10%
==========================================
Files 17 17
Lines 2436 2492 +56
Branches 258 255 -3
==========================================
+ Hits 2101 2122 +21
- Misses 249 302 +53
+ Partials 86 68 -18 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Fixes #1286
The bug
The converters' documented contract (docstrings + doctests) is that illegal values are returned unchanged as str. Every malformed input followed it except one class: a value with fewer than three
--separated components survived theint()conversion and then blew updatetime.date()'s arity with a rawTypeError:so a result row containing such a value crashed inside result decoding instead of yielding the string.
Fix
Catch
TypeErroralongsideValueErrorinconvert_dateand pin the behavior with a doctest (>>> convert_date('2007-02')→'2007-02'), matching the module's own doctest-driven test convention.convert_datetime/convert_timedeltadon't need the same catch: their regexes fix the argument count, so arity errors are impossible there; their fallbacks route throughconvert_date, which now honors the contract.Verification
convert_date('2007-02')/convert_datetime('2007-02')raiseTypeError(reproduced before applying the change viagit stash).'2007-02'; all previously documented behaviors unchanged ('0000-00-00'→ str,'2007-02-31'→ str,'random crap'→ str, valid dates/datetimes parse as before).pytest --doctest-modules pymysql/converters.py: 4 passed (including the new doctest line).tests/test_auth.pyshows only the same 8 pre-existing host-environment failures (missing CA files / cryptography on this box) before and after the change; CI remains the authority.Co-authored-by: rahul05ranjan 11793455+rahul05ranjan@users.noreply.github.com
Summary by CodeRabbit