GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Can't compare dicts with both single and double quotes in keys · Issue #430 · qlustered/deepdiff

Can't compare dicts with both single and double quotes in keys #430

New issue
Copy link
New issue
Copy link
Closed
Closed
Can't compare dicts with both single and double quotes in keys#430
Copy link
Assignees
seperman

Description

@kammala
kammala
opened on Nov 8, 2023
Issue body actions

Describe the bug
DeepDiff falls with an exception while comparing dicts with keys containing both single and double quotes.

To Reproduce

import deepdiff

deepdiff.DeepDiff({'''a'"a''': 1}, {'''a'"a''': 2})

Expected behavior
A comparison result is being returned.

OS, DeepDiff version and Python version (please complete the following information):

  • DeepDiff Version: 6.7.0

Additional context
The branch to handle both single and double quotes in stringify_element doesn't define result variable which is used later in return statement which causes exception (UnboundLocalError: local variable 'result' referenced before assignment) to be triggered.

Activity

  1. seperman
    self-assigned this
    on Nov 14, 2023
  2. seperman
    added a commit that references this issue on Nov 14, 2023

    fixes #430

    This issue will close once commit 32182c8 is merged into the 'master' branch.
    32182c8
  3. seperman
    closed this as completedin 32182c8on Nov 14, 2023
  4. asterbini commented on Nov 14, 2023

    @asterbini
    asterbini
    on Nov 14, 2023
    More actions

    I get an error on stringify_element when param is a byte string.
    With the following change (in the first 2 lines) it works.

    def stringify_element(param, quote_str=None):
        if isinstance(param, str):
            has_quote = "'" in param
            has_double_quote = '"' in param
        else:
            has_quote = b"'" in param
            has_double_quote = b'"' in param
        if has_quote and has_double_quote:
            new_param = []
            for char in param:
                if char in {'"', "'"}:
                    new_param.append('\\')
                new_param.append(char)
            result = ''.join(new_param)
        elif has_quote:
            result = f'"{param}"'
        elif has_double_quote:
            result = f"'{param}'"
        else:
            result = param if quote_str is None else quote_str.format(param)
        return result
    
  5. seperman commented on Nov 15, 2023

    @seperman
    seperman
    on Nov 15, 2023 via email
    Member
    More actions
    Cool,Do you want to make a PR for it and add a test case?Thanks!Sep DehpourOn Nov 14, 2023, at 12:32 PM, Andrea Sterbini ***@***.***> wrote: I get an error on stringify_element when param is a byte string. With the following change (in the first 2 lines) it works. def stringify_element(param, quote_str=None): if isinstance(param, str): has_quote = "'" in param has_double_quote = '"' in param else: has_quote = b"'" in param has_double_quote = b'"' in param if has_quote and has_double_quote: new_param = [] for char in param: if char in {'"', "'"}: new_param.append('\\') new_param.append(char) result = ''.join(new_param) elif has_quote: result = f'"{param}"' elif has_double_quote: result = f"'{param}'" else: result = param if quote_str is None else quote_str.format(param) return result —Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you modified the open/close state.Message ID: ***@***.***>
  6. asterbini commented on Nov 15, 2023

    @asterbini
    asterbini
    on Nov 15, 2023
    More actions

    I am not so familiar with all the DeepDiff code, thus let me try just suggesting a correction that seems to work assuming one is using 'utf8' encoding in bytes and bytearray

    def stringify_element(param, quote_str=None):
        if isinstance(param, (bytes, bytearray)):   ## handle bytes
            param = param.decode('utf8')            ## here
        has_quote = "'" in param
        has_double_quote = '"' in param
        if has_quote and has_double_quote:
            new_param = []
            for char in param:
                if char in {'"', "'"}:
                    new_param.append('\\')
                new_param.append(char)
            result = ''.join(new_param)
        elif has_quote:
            result = f'"{param}"'
        elif has_double_quote:
            result = f"'{param}'"
        else:
            result = param if quote_str is None else quote_str.format(param)
        return result
    

    Test:

    B = {'Foo\'\"':42}
    A = {b'Foo\'\"':42}
    deepdiff.DeepDiff(A, B)
    Out[8]: 
    {'dictionary_item_added': [root[Foo\'\"]],
     'dictionary_item_removed': [root[Foo\'\"]]}
    
  7. netbsd-srcmastr
    added a commit that references this issue on Jan 28, 2024

    py-deepdiff: updated to 6.7.1

    730a080
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @seperman
    seperman

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.