Skip to content

Fix escape_timedelta for negative values - #1262

Merged
methane merged 2 commits into
PyMySQL:mainfrom
dngr2:escape-timedelta-sign
Aug 18, 2026
Merged

methane merged 2 commits into
PyMySQL:mainfrom
dngr2:escape-timedelta-sign

Conversation

@dngr2

@dngr2 dngr2 commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

escape_timedelta produced the wrong SQL literal for a negative timedelta. Python normalizes a negative timedelta so seconds/microseconds are non-negative and the sign lives entirely in days, but escape_timedelta decomposed the always-positive seconds and applied the sign only through days*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 a timedelta is 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.

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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 format cleanup 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 runs ruff format --diff, so the Python lint job will fail. Please run ruff format to 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

codecov Bot commented Aug 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.50%. Comparing base (1e28be8) to head (a3f0f18).
⚠️ Report is 62 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Refactor time formatting logic to handle negative timedelta and simplify code.
@methane
methane merged commit 34f2676 into PyMySQL:main Aug 18, 2026
12 of 13 checks passed
methane added a commit to PyMySQL/mysqlclient that referenced this pull request Aug 21, 2026
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.

3 participants