fix(path): handle bytes/memoryview keys in stringify_element (#472) - #614
Open
Mukller wants to merge 1 commit into
Open
fix(path): handle bytes/memoryview keys in stringify_element (#472)#614Mukller wants to merge 1 commit into
Mukller wants to merge 1 commit into
Conversation
…ed#472) stringify_element() checks `"'" in param` to detect single-quoted strings before choosing a quote style. When param is bytes (e.g. a dict key b"foo"), the str needle `"'"` raises TypeError: a bytes-like object is required. bytes and memoryview have a canonical repr() that round-trips safely (b"foo" → repr → "b'foo'") and needs no additional quoting, so we return repr(param) immediately for those types before the str-search. Reproducer: from deepdiff import DeepDiff DeepDiff({b"foo": 1}, {b"foobar": 1}) # TypeError before, works after
Mukller
commented
Jul 29, 2026
Mukller
left a comment
Author
There was a problem hiding this comment.
Code Review
Bug Path
DeepDiff({b'foo': 1}, {b'foobar': 1})
_diff_dict
→ _report_result (item added/removed)
→ _skip_this → level.path()
→ DictRelationship.get_param_repr()
→ stringify_element(b'foo', quote_str="'{}'")
bytes ∈ helper.strings → enters function
"'" in b'foo' → TypeError ✗
Root Cause Detail
In Python, "'" in some_bytes requires the left operand to be bytes too, not str. The function was written for str params but helper.strings = (str, bytes, memoryview) causes bytes callers to reach the same code path.
Fix Correctness
For bytes / memoryview, repr() gives the canonical Python expression that is already round-trippable:
repr(b'foo') # "b'foo'"
repr(b"it's") # "b\"it's\"" (repr auto-chooses safe quote style)
repr(memoryview(b'x')) # "<memory at …>" (not parseable, but avoids crash)| Input | Before | After |
|---|---|---|
b'foo' |
TypeError ✗ |
"b'foo'" → path root[b'foo'] ✓ |
b"it's" |
TypeError ✗ |
'b"it\'s"' → path root[b"it's"] ✓ |
'foo' |
"'foo'" ✓ |
unchanged ✓ |
"it's" |
'"it\'s"' ✓ |
unchanged ✓ |
Scope
Two lines inserted at the top of stringify_element. No existing string-handling logic is touched; all string-key paths are unchanged.
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.
Problem
Fixes #472.
DeepDiffraisesTypeErrorwhen comparing dicts that containbyteskeys:Root Cause
stringify_element()indeepdiff/path.pyuses"'" in paramto detect single quotes before choosing a quoting style. Forbytes(andmemoryview) objects theinoperator requires abytesneedle; passing astrraisesTypeError.The crash path:
Fix
repr(b'foo')→"b'foo'"is a valid Python expression that:b''notation is already correct)literal_evalroot[b'foo']when wrapped byparam_repr_formatVerification