BstLabs / py-jdict

JavaScript-like Python dictionary
MIT License
18 stars 2 forks source link

[BUG] AttributeError: 'dict' object has no attribute 'file' for native dict type #24

Open arzuhuseyn opened 2 years ago

arzuhuseyn commented 2 years ago

Can not access children elements Whenever I try to access inner elements (access to the first element is fine) I got an error.

To Reproduce

# Send get request and get back the json information about the uploaded file
def _get_file_info() -> dict:
    a = {
        "status": True,
        "data": {
            "file": {
                "url": {
                    "short": "https://anonfiles.com/t3l5S2Gex3",
                    "full": "https://anonfiles.com/t3l5S2Gex3/test_txt",
                },
                "metadata": {
                    "size": {"bytes": 19, "readable": "19 B"},
                    "name": "test_txt",
                    "id": "t3l5S2Gex3",
                },
            }
        },
    }
    return jdict(a)

Expected result / Error

data = _convert_to_jdict()
print("ID: ", data.data.file.metadata.id) # Expected: t3l5S2Gex3
Traceback (most recent call last):
  File "/Users/johndoe/Development/Projects/py-jdict/test.py", line 42, in <module>
    print("ID: ", data.data.file.metadata.id)
AttributeError: 'dict' object has no attribute 'file'
asterkin commented 2 years ago

Not a bug. jdict does not perform deep conversion of dict, since this is normally done by changing json default decoder. If you want to manually craft nested jdict, you just:

a = jdict(
  status=True,
  data=jdict(
      file=jdict(
         url=jdict(
             short="...",
             full="..."
         ),
         metadata=jdict(
             size=jdict(bytes=19, readable="19 B"),
             name="test_txt",
             id="t..."
           )

deep conversion (aka factory method) could be implemented as a separate function or jdict static method, but Im not sure how practically important is it. We made jdict to be as lean as possible and support json de-serialization with minimal overhead. But nothing is excluded upfront. Could you provide a rea use case scenario when such feature would be useful?

arzuhuseyn commented 2 years ago

Not a bug. jdict does not perform deep conversion of dict, since this is normally done by changing json default decoder. If you want to manually craft nested jdict, you just:

a = jdict(
  status=True,
  data=jdict(
      file=jdict(
         url=jdict(
             short="...",
             full="..."
         ),
         metadata=jdict(
             size=jdict(bytes=19, readable="19 B"),
             name="test_txt",
             id="t..."
           )
...
deep conversion (aka factory method) could be implemented as a separate function or jdict static method, but Im not sure how practically important is it. We made jdict to be as lean as possible and support json de-serialization with minimal overhead. But nothing is excluded upfront. Could you provide a rea use case scenario when such feature would be useful?

Hey @asterkin ,

Thanks for the quick response.

I guess I misunderstood the point of this library. On the readme file, you mentioned this library is able to generate a JavaScript-like Python dictionary. Which, you can simply call the js dictionaries like I was mentioned before.

const myObj = {
 a: 1,
 b: 2,
 c: {
   d: 3,
   f: 4,
  },
}

console.log(myObj.c.f) // Should return 4