Dictionary has no key

Hello all,
I am running into a rather embarrassing issue, which I just can’t figure out and thus resorted to kindly asking for help on the forums.

There is a dictionary, from which I would like to get an attribute. And that works fine until a certain point/repetition. At that point, I get:
failed: AttributeError: 'dict' object has no attribute 'task_time'
Even though the attribute seems to be present in the dictionary.

Also, as a counter example, here is a part from logs from an earlier iteration, which gives the expected result:

Any tips will be much appreciated, as I ran out of ideas what else to try.
Thank you very much,
A

Robot’s internal dict type is DotDict - robot.utils package — Robot Framework 7.3.2 documentation which explicitly enables dot notation to access dict members. Maybe the case where your dot notation fails, under the hood the dict is a python dict, not robot’s DotDict ?

1 Like

Thank you @rasjani , different dictionary type seems to have been the reason behind the issue. It happened at a point where I used a Python function to order dictionaries based on a value of dictionary. Didn’t even realize that would change the data type.

You have already answered my question really. Though I would like to ask for a follow up advice, or a comment. A quick solution I came up with was to re-create the dictionary (after Python sorting) in order to keep Robot dict type, which looks like this.

FOR    ${dict_list}    IN    @{tasks_original}
    ${dict}=    Create Dictionary    &{dict_list}
    Append To List    ${tasks}    ${dict}
END

My question is, is there a simpler solution to achieve the same? Perhaps an inbuilt function, or one line of code instead of the for loop?

Thank you once again!

As an example, this returns python dict
foo.py

def ret_dict():
  return {"a": 1, "b": 2, "c": 3}

And

*** Settings ***
Library   foo.py

*** Test Cases ***
Test DotDict
   ${x}=    ret dict
   TRY
    Log To Console   X.A: ${x.a}
   EXCEPT
     Log To Console   This Still Fails
   END
   ${y}=  evaluate   robot.utils.DotDict(${x})     modules=robot
  Log To Console   Y.A: ${y.a}

So, you just need to “typecast” it to robot.utils.DotDict() ..

1 Like

Thank you for your help and proposed solution @rasjani !

I needed to a bit of workarounds because of data structure the dictionary was present in. But you got me on the right track quickly after your first response already.
Best regards,
JC