Possible bug in Copy Dictionary keyword

I’m not sure if this is the right place to report possible bugs, so apologies in advance if this was supposed to be reported somewhere else.

Robot Framework version: 4.0.2
Library: Collections
Keyword: Copy Dictionary
Usage: ${newDictionary}= Copy Dictionary ${myDictionary}[subDictionary1][subDictionary2]
Expected Behavior: newDictionary receives the value of subDictionary2 and myDictionary remains unchanged.
Actual Behavior: The values of both newDictionary AND myDictionary are changed to subDictionary2. It doesn’t matter whether deepcopy is set to True or False.

Has anybody else seen this?

It would be nice to have some reproducible test case, so I made one for you :wink:

*** Settings ***
Library           Collections

*** Variables ***
&{subDictionary1}    a=1    b=2
&{subDictionary2}    c=three    d=four
&{MyDictionary}    subDictionary1=${subDictionary1}    subDictionary2=${subDictionary2}

*** Test Cases ***
Copy Dictionary
    Log Many    ${MyDictionary}
    ${NewDictionary}=    Copy Dictionary    ${MyDictionary['subDictionary2']}
    Log Many    ${NewDictionary}
    Log Many    ${MyDictionary}

Copy From Nested Dictionary
    &{MyNestedDictionary}=    Create Dictionary    subDictionary1=${subDictionary1}
    Set To Dictionary    ${subDictionary1}    subDictionary2=${subDictionary2}
    ${NewDictionary}=    Copy Dictionary    ${MyNestedDictionary['subDictionary1']['subDictionary2']}
    Log Many    ${NewDictionary}
    Log Many    ${subDictionary1}
    Log Many    ${subDictionary2}
    Log Many    ${MyNestedDictionary}
    Should Be Equal    ${MyNestedDictionary['subDictionary1']}    ${subDictionary1}

Thank you. You’re right. I should’ve included a test example. And testing your code led me to the REAL culprit, which was my variable naming convention. Copy Dictionary had nothing to do with it.
The name of the new dictionary I was creating was similar to the original dictionary except it had an underscore character in it, which I assumed would be treated as a different variable. It wasn’t. I’ve been looking, but haven’t found this behavior documented anywhere yet. Here’s a test case to illustrate the issue.

*** Settings ***

*** Test Cases ***

Test Variable Names
   ${abc}=  Set Variable  a
   ${a b c}=  Set Variable  b
   ${a_b_c}=  Set Variable  c

   Log  ${abc}
   Log  ${a b c}
   Log  ${a_b_c}

   Should Not Be Equal  ${abc}  ${a b c}
   Should Not Be Equal  ${abc}  ${a_b_c}

I am sure it is documented in the User Guide, that undercores, spaces and Case is stripped (or ignored ) on variables.


Ah, so it is. My eyes missed that when I went through it the first time. Thank you for the correction.

Paragraph 2 under Section 2.6.2 - Using Variables

Robot Framework variables, similarly as keywords, are case-insensitive, and also spaces and underscores are ignored. However, it is recommended to use capital letters with global variables (for example, ${PATH} or ${TWO WORDS} ) and small letters with local variables that are only available in certain test cases or user keywords (for example, ${my var} ). Much more importantly, though, case should be used consistently.