Fix escape_timedelta for negative values - #1262
Merged
Merged
Conversation
timedelta normalizes a negative value so seconds/microseconds are non-negative and the sign lives in days. escape_timedelta decomposed the always-positive obj.seconds and bolted the sign on via obj.days*24, so a negative TIME came out with complemented sub-hour fields (-timedelta(minutes=30) -> -1:30:00 instead of -00:30:00), silently corrupting negative TIME parameters. Reconstruct the signed magnitude first, then format it with a single leading sign.
There was a problem hiding this comment.
Pull request overview
Fixes negative timedelta SQL escaping by reconstructing its signed magnitude before formatting.
Changes:
- Formats negative durations with one leading sign.
- Adds positive, negative, multi-hour, and fractional-second tests.
- Requires
ruff formatcleanup in the new tests.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
pymysql/converters.py |
Corrects signed duration formatting. |
pymysql/tests/test_converters.py |
Adds escape and round-trip coverage. |
Suppressed comments (1)
pymysql/tests/test_converters.py:64
- This line is not
ruff format-compliant and the lint workflow runsruff format --diff, so the Python lint job will fail. Please runruff formatto wrap the nested call.
converters.convert_timedelta(converters.escape_timedelta(tdelta).strip("'")),
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| -datetime.timedelta(minutes=30): "'-00:30:00'", | ||
| -datetime.timedelta(hours=1, minutes=30): "'-01:30:00'", | ||
| datetime.timedelta(seconds=83579, microseconds=51000): "'23:12:59.051000'", | ||
| -datetime.timedelta(seconds=83579, microseconds=51000): "'-23:12:59.051000'", |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1262 +/- ##
==========================================
- Coverage 86.24% 84.50% -1.75%
==========================================
Files 17 17
Lines 2436 2471 +35
Branches 258 248 -10
==========================================
- Hits 2101 2088 -13
- Misses 249 306 +57
+ Partials 86 77 -9 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Refactor time formatting logic to handle negative timedelta and simplify code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
escape_timedeltaproduced the wrong SQL literal for a negativetimedelta. Python normalizes a negativetimedeltasoseconds/microsecondsare non-negative and the sign lives entirely indays, butescape_timedeltadecomposed the always-positivesecondsand applied the sign only throughdays*24. A negative value therefore came out with complemented sub-hour fields —-timedelta(minutes=30)escaped to'-1:30:00'(-5400s) instead of'-00:30:00'.MySQL TIME allows negatives (
-838:59:59..838:59:59) and atimedeltais the registered encoder for TIME parameters, so this silently corrupts a negative TIME bound as a parameter. Reconstruct the signed magnitude first, then format the absolute value with a single leading sign. Added a test.