Robot Framework passing numbers as strings to a imported python library

3,940 views
Skip to first unread message

SuperGeo

unread,
Jan 23, 2018, 11:37:07 AM1/23/18
to robotframework-users
Hello,

In my robot file I am importing directly a python library using

Library  |  my_python_lib

And using a function like

lib_func  |   arg1=0.1    |    arg2=5

However the function is for some reason receiving the numbers 0.1 and 5 as strings, when  they should be int or float.

It this normal behavior and, if so, how to make Robot Framework send  them to my python library as int/floats?

Thank you for the attention.

Bryan Oakley

unread,
Jan 23, 2018, 11:41:55 AM1/23/18
to victor....@gmail.com, robotframework-users
It is normal behavior. Everything in robot is passed around as a string unless explicitly created as some other type.

You can coerce literal values to numbers by enclosing them in ${}, for example

    lib_func | arg1=${0.1}  arg2=${5}

This is covered in the user guide in a section titled Number variables

--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-users+unsub...@googlegroups.com.
To post to this group, send email to robotframework-users@googlegroups.com.
Visit this group at https://groups.google.com/group/robotframework-users.
For more options, visit https://groups.google.com/d/optout.

Tatu Aalto

unread,
Jan 23, 2018, 11:43:51 AM1/23/18
to victor....@gmail.com, robotframework-users
Ugh

What you did experience is correct and by design. If you want to give number as argument, then you the number variable format [1]. Usually it's easier convert the arguments to the desired type in the library side.

-Tatu
Send from my mobile

Pekka Klärck

unread,
Jan 23, 2018, 12:19:21 PM1/23/18
to victor....@gmail.com, robotframework-users
Hi,

As others have already commented, the behavior is by design. It's in
general hard to tell should a value like 42 in a plain text file be
interpreted as a string or an integer. Robot Framework has decided to
consider everything to be a string. We can argue about the decision,
but due to huge backwards compatibility problems it cannot be changed.

I have, however, been lately thinking that we could start using Python
3 annotations to see what types the library expects. If, for example,
you'd have a keyword

def example(arg: int):
pass

and used it like

Example 42

Robot could automatically convert that string 42 into an integer. We
already do this for Java based libraries implemented like

public void example(int arg) {
}

so this shouldn't be overly complicated. I guess we could add some way
to support this also with Python 2, but on the other hand this would
be a great feature motivating people to migrate to Python 3. Anyway,
this is too big a change for RF 3.0.3 but would fit nicely into RF
3.1.

Cheers,
.peke
> --
> You received this message because you are subscribed to the Google Groups
> "robotframework-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to robotframework-u...@googlegroups.com.
> To post to this group, send email to robotframe...@googlegroups.com.
--
Agile Tester/Developer/Consultant :: http://eliga.fi
Lead Developer of Robot Framework :: http://robotframework.org

Victor Maryama

unread,
Jan 23, 2018, 3:39:06 PM1/23/18
to Pekka Klärck, robotframework-users
Thanks everyone for the answers!

I tried to search in the documentation but could not find, thanks for the pointers.

In fact dealing with this inside the lib is not desirable because it is used for other pure python purposes and it would not be worth to add this treatment from str to number for every argument. Also writing a "glue" lib is too much effort, so for now I guess I will stick to ${number} notation.

Pekka Klärck

unread,
Jan 23, 2018, 4:44:01 PM1/23/18
to Victor Maryama, robotframework-users
2018-01-23 22:38 GMT+02:00 Victor Maryama <victor....@gmail.com>:
> Thanks everyone for the answers!
>
> I tried to search in the documentation but could not find, thanks for the
> pointers.

Everything is documented in the User Guide:
http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#argument-types

> In fact dealing with this inside the lib is not desirable because it is used
> for other pure python purposes and it would not be worth to add this
> treatment from str to number for every argument. Also writing a "glue" lib
> is too much effort, so for now I guess I will stick to ${number} notation.

I understand you don't want to add this kind of type conversion into a
generic Python module. I would say splitting it into a generic module
and Robot library would still be a good idea and shouldn't be to big a
task. In addition to handling arguments, the library layer could
handle logging.

Cheers,
.peke
Reply all
Reply to author
Forward
0 new messages