Encode subclasses of the specially handled types instead of rejecting them - #329
Open
binggao1230 wants to merge 2 commits into
Open
Encode subclasses of the specially handled types instead of rejecting them#329binggao1230 wants to merge 2 commits into
binggao1230 wants to merge 2 commits into
Conversation
… them The stdlib encoder table was matched by exact type identity, so subclasses of datetime, Decimal, UUID, the ipaddress types etc. raised CBOREncodeError where cbor2 5.x encoded them like their base type. Keep the exact-identity pass first for the common case, then fall back to isinstance matching, with the table ordered so subclasses precede their bases.
binggao1230
force-pushed
the
fix-stdlib-subclass-encoding
branch
from
July 15, 2026 20:29
c1bfade to
bbe2a25
Compare
agronholm
requested changes
Aug 19, 2026
agronholm
left a comment
Owner
There was a problem hiding this comment.
Overall looks good. I could add this in v7.0.
| py.import("re")?.getattr("Pattern")?.cast_into()?.unbind(), | ||
| CBOREncoder::encode_regexp, | ||
| ), | ||
| ( |
Owner
There was a problem hiding this comment.
Why did you move these around?
| ), | ||
| ]) | ||
| })?; | ||
| // Exact type matches first: the common case, using cheap pointer comparisons |
Owner
There was a problem hiding this comment.
Suggested change
| // Exact type matches first: the common case, using cheap pointer comparisons | |
| // Exact type matches first: the common case, using cheap pointer comparisons |
Comment on lines
+439
to
+484
| class _DatetimeSubclass(datetime): | ||
| pass | ||
|
|
||
|
|
||
| class _DateSubclass(date): | ||
| pass | ||
|
|
||
|
|
||
| class _DecimalSubclass(Decimal): | ||
| pass | ||
|
|
||
|
|
||
| class _FractionSubclass(Fraction): | ||
| pass | ||
|
|
||
|
|
||
| class _UUIDSubclass(UUID): | ||
| pass | ||
|
|
||
|
|
||
| class _IPv4AddressSubclass(IPv4Address): | ||
| pass | ||
|
|
||
|
|
||
| class _IPv4NetworkSubclass(IPv4Network): | ||
| pass | ||
|
|
||
|
|
||
| class _IPv4InterfaceSubclass(IPv4Interface): | ||
| pass | ||
|
|
||
|
|
||
| class _IPv6AddressSubclass(IPv6Address): | ||
| pass | ||
|
|
||
|
|
||
| class _IPv6NetworkSubclass(IPv6Network): | ||
| pass | ||
|
|
||
|
|
||
| class _IPv6InterfaceSubclass(IPv6Interface): | ||
| pass | ||
|
|
||
|
|
||
| class _MIMETextSubclass(MIMEText): | ||
| pass |
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.
Changes
Since the Rust rewrite, encoding a subclass of any of the specially handled types raises
CBOREncodeError:cbor2 5.x encoded these like their base type (verified on 5.9.0, both the C extension and the pure-Python implementation —
_find_encoderusedissubclass()), so things likepandas.TimestamporDecimal/UUIDsubclasses used to work. It's also inconsistent with the primitives, whose subclasses (int,str,bytes, ...) still encode fine in 6.x. The 6.0 changelog doesn't list this among the backward-incompatible changes, so I'm assuming it's unintended.The fix keeps the exact-identity pass over the stdlib encoder table first, so the common case still only pays pointer comparisons, and adds an
isinstance()fallback pass for subclasses. The table is reordered so subclasses precede their bases in that fallback (IPv4Interface/IPv6Interfacebefore the address types, likedatetimebeforedate) — so an interface subclass is encoded as an interface, not silently downgraded to a bare address the way 5.x did it.Checklist
If this is a user-facing code change, like a bugfix or a new feature, please ensure that
you've fulfilled the following conditions (where applicable):
tests/) which would fail without your patchdocs/), in case of behavior changes or newfeatures
docs/versionhistory.rst).