Kaamos
Kaamos

Reputation: 45

RF 3.0.4 nested dictionary syntax error if first key is number, dot notation doesn't work

I am using rf 3.0.4. I upgraded because of the dot notation upgrade (before I was using rf 2.9). My problem is when I want to access a nested dictionary item and the first key (it is an id from db) is a number, I got a syntax error.

I have a nested dictionary: &{Attributes}

So what I want to do:

${Attributes.1000.name}

The syntax error I get:

Resolving variable '${Attributes.1000.name}' failed: SyntaxError: invalid syntax (<string>, line 1)

And what is working:

${Attributes["1000"]["name"]}

I'd like to use the dot notation, because it is more readable. Do any of you know why it doesn't work?

Upvotes: 1

Views: 208

Answers (1)

A. Kootstra
A. Kootstra

Reputation: 6981

It seems to me to be a limitation of Robot Framework. When a dictionary key item starts with a number (even when a string) then it will fail. In the below two test cases this is shown.

To me this sounds like a defect and you may want to log this as an issue with the project's GitHub issue log.

*** Settings ***
Library    Collections    

*** Variables ***
${name}             MyName
&{person}           name=${name} 
&{person_valid}     A1000=${person}    A2000=${person}
&{person_invalid}   1000A=${person}    2000A=${person}

*** Test Cases ***
TC - Valid
    ${pers}           Set Variable    ${person_valid.A1000}
    Dictionaries Should Be Equal    ${pers}    ${person}     

    ${pers_name_1}    Set Variable    ${person_valid["A1000"]["name"]}
    Should Be Equal As Strings    ${pers_name_1}    ${name}     

    ${pers_name_2}    Set Variable    ${person_valid.A1000.name}
     Should Be Equal As Strings    ${pers_name_2}    ${name}     

TC - Fails
    Run Keyword And Expect Error    
    ...    Resolving variable '\${person_invalid.1000A}' failed: SyntaxError: invalid syntax (<string>, line 1)   
    ...    Set Variable    ${person_invalid.1000A}    

    Run Keyword And Expect Error    
    ...    Resolving variable '\${person_valid.1000A.name}' failed: SyntaxError: invalid syntax (<string>, line 1)   
    ...    Set Variable    ${person_valid.1000A.name} 

Upvotes: 3

Related Questions