| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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
There was a problem hiding this comment.
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 ✗
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.
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 ✓ |
Two lines inserted at the top of stringify_element. No existing string-handling logic is touched; all string-key paths are unchanged.
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Problem
Fixes #472.
DeepDiff raises TypeError when comparing dicts that contain bytes keys:
Root Cause
stringify_element() in deepdiff/path.py uses "'" in param to detect single quotes before choosing a quoting style. For bytes (and memoryview) objects the in operator requires a bytes needle; passing a str raises TypeError.
The crash path:
→ _diff_dict detects key change → _report_result → _skip_this → level.path() → DictRelationship.get_param_repr() bytes ∈ deepdiff.helper.strings → stringify_element(b'foo', quote_str="'{}'") → "'" in b'foo' # TypeErrorFix
repr(b'foo') → "b'foo'" is a valid Python expression that:
Verification