Code Monkey home page Code Monkey logo

Comments (7)

fabiocaccamo avatar fabiocaccamo commented on May 20, 2024 1

@simkimsia thank you for reporting this. 
I guess this problem is not related to casting, but to the fact that to keep the pointer to the input dict benedict now stores data inside a wrapped dict -> https://github.com/fabiocaccamo/python-benedict/blob/master/benedict/dicts/base/base_dict.py

This should be a completely backward compatibility and transparent behavior.
In case of emergency, there is a new dict() method to access the wrapped dict, so you could just do:

normal_dict = {'list': {'scope': {'pk': '109'}}
benedict_dict = benedict(normal_dict)

instance = SomeModel.objects.create(linkage=benedict_dict.dict())
retrieved = SomeModel.objects.last()

In any case this is just a workaround, and casting benedict to dict should work fine.

def test_cast_benedict_to_dict(self):
    b = benedict({
        'a': 1,
        'b': {
            'c': {
                'd': 2,
            },
        },
    })
    d = dict(b)
    self.assertTrue(isinstance(d, dict))
    self.assertEqual(type(d), dict)
    self.assertTrue(b == d)
    self.assertFalse(b is d)

It's my intention to keep benedict 100% dict compatible, so we must understand what breaks the django JSONField behavior to fix it.

Could you debug better the cause of this problem?

from python-benedict.

simkimsia avatar simkimsia commented on May 20, 2024 1

Thank you for prompt reply

@fabiocaccamo

I try but no promises as this is not high on priority once I have a workaround

Hope you understand

from python-benedict.

simkimsia avatar simkimsia commented on May 20, 2024 1

@fabiocaccamo

I think I found the cause.

THis is my code to replicate the error using pytest 3.4.1

import json

from benedict import benedict


"""
doc_string for test_python_class
"""


class TestBenedict:
    """
    doc_string
    """

    def teardown_method(self, test_method):
        pass

    def test_cast_benedict_to_dict(self):
        """
        some doc string
        """
        b = benedict({
            'a': 1,
            'b': {
                'c': {
                    'd': 2,
                },
            },
        })
        d = dict(b)
        assert isinstance(d, dict)
        assert type(d) == dict
        assert b == d
        assert b is not d

    def test_json_dumps_benedict(self):
        """
        some doc string
        """
        b = benedict({
            'a': 1,
            'b': {
                'c': {
                    'd': 2,
                },
            },
        })
        dumped = json.dumps(b)
        print(dumped)
        assert isinstance(dumped, str)
        assert dumped == '{"a": 1, "b": {"c": {"d": 2}}}'

This is the 0.20.0 effort when I run pytest test_benedict.py -s
image

This is the 0.19.0 effort when I run the same command and same test code

image

Sorry I know the best way is to create a PR and write a failing test, but I am not sure how to do that in your current setup.

So it was easier for me to write a failing test as a standalone.

Perhaps you can advise in documentation on how to write failing tests as PR.

Long story of the cause

JSONField uses an encoder for the dumping of the json input. The default encoder comes from psycopg2 which by default simply uses json.dumps at this line https://github.com/psycopg/psycopg2/blob/e50f3129c25aa764b3acb6205f515bab56c3f6d2/lib/_json.py#L59

Hence I wrote a test to test that json.dumps works for 0.19 but fails for 0.20. The test is a success as shown above.

from python-benedict.

fabiocaccamo avatar fabiocaccamo commented on May 20, 2024 1

@simkimsia the dict() method is used internally and if someone needs to use it, it means that there is some problem, so I intentionally omitted it from the README.

Thank you very much for the test, it's very useful!

from python-benedict.

simkimsia avatar simkimsia commented on May 20, 2024 1

I confirm this works ✅

from python-benedict.

fabiocaccamo avatar fabiocaccamo commented on May 20, 2024

@simkimsia don't worry, if you find the time, just check what is called by the JSONField.

from python-benedict.

simkimsia avatar simkimsia commented on May 20, 2024

if you find the time, just check what is called by the JSONField.

Yes, I will try to do so.

By the way, I see that you made no commits. Yet you cite a method .dict() as workaround I don't find in the readme.

Am I mistaken?

image

Update

Ok I see it in the BaseDict class https://github.com/fabiocaccamo/python-benedict/blob/master/benedict/dicts/base/base_dict.py#L51 Perhaps would be useful to update the README? I leave that to you. Looks like I chose a convoluted way to export back to regular dict in my own workaround

from python-benedict.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.