Repository navigation
Conversation
With `use_unicode=False`, column values reach the decoders as bytes. The date and time converters decode bytes first, but DECIMAL and NEWDECIMAL were mapped to `Decimal` itself, which rejects bytes. Any result with a DECIMAL column, including `SUM()` and `AVG()` of integer columns, raised `TypeError: conversion from bytes to Decimal is not supported`, and the rows left unread made the next query on the same connection fail with "Packet sequence number wrong". Add `convert_decimal()`, which decodes bytes like the other converters and returns a `Decimal`, and use it for both DECIMAL field types. This matches mysqlclient, which returns `Decimal` for these columns with `use_unicode=False`.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
With
use_unicode=False, any query that returns a DECIMAL column raisesTypeError: conversion from bytes to Decimal is not supported. That includesSUM()andAVG()over plain INT columns, since MySQL returns those as DECIMAL. The exception is raised in the middle of reading the result, so the remaining packets stay on the socket, and the next query on the same connection fails withInternalError: Packet sequence number wrong - got 5 expected 1.With
use_unicode=Falsethe values reach the decoders as bytes.convert_datetime(),convert_timedelta()andconvert_date()decode bytes first (#456), andintandfloataccept bytes, but DECIMAL and NEWDECIMAL are mapped toDecimalitself, which does not.I added
convert_decimal(), which decodes bytes the same way and returns aDecimal, and mapped both DECIMAL field types to it. With the defaultuse_unicode=Truethe value is still a str and the result is the sameDecimal. mysqlclient 2.3.0 returnsDecimal('9.99'),Decimal('5')andDecimal('5.245000')for the three queries below withuse_unicode=False, so this also matches it.Reproduced on MySQL 8.4.9 with Python 3.13, before the change:
After the change the queries return
Decimal('9.99'),Decimal('5')andDecimal('5.245000').Tests:
TestConversion.test_decimal_without_unicodeselects a DECIMAL and aSUM()on ause_unicode=Falseconnection. With the oldconverters.pyit fails with the sameTypeError. The docstring ofconvert_decimal()has doctests for str and bytes. The full suite (pytest pymysql: 188 passed, 12 skipped, 1 xfailed), the converters doctests,ruff format --diffandruff check --diffpass. I ran the suite against MySQL 8.4 only, without the unix socket entry fromci/docker.json, and not against MariaDB or MySQL 5.7.Summary by CodeRabbit