You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support automatic argument conversion with dynamic libraries #2068
As discussed on the mailing list, it would be nice to define a new (probably optional) keyword for dynamic libraries, in order to specify the type of the arguments needed for a keyword (as a list of strings, maybe?).
Even though RF performs type coercion automatically when calling directly Java libraries, it is not the case when calling a Jython library that itself wraps some Java libraries.
The look-up inside the Java library would use the getDeclaredMethods() and getParamterTypes(), with a small conversion to perform between, for instance, double and float.
RF could expect as an output something like ['int', 'float', 'str', 'str'], to perform a type cast if the method get_keyword_argument_types is present on the API, probably before the run_keyword is actually called.
I don't know what to do for list or dict items, though.
I don't know the codebase much. Would this class be the place to put it? Or is there a place I missed which calls the external keywords? I could give a shot at a Pull-Request with some minimal starting help, if no one else has time.
Just adding a new method to the dynamic API isn't enough because that type information needs to be used too. The existing argument coercion logic used by Java libraries probably can be reused, but it anyway needs to be hooked up with other libraries.
I also consider adding argument coercion to the normal Python library API more important than adding it to the dynamic API. That ought to be a bit easier too, because there we could just use an attribute like robot_types or robot_argument_types to specify arguments. That needs a separate issue, though.
I agree that adding argument coercion for static Python/Jython library would be nice too - though for my current project, I need the dynamic bit. I can open the issue to trigger a discussion, though, if you want.
I am aware that the type information needs to be used, I just don't know where it would be more appropriate (or I misunderstood your sentence). I am searching for the place where it would make more sense to add it. Since dynamic libraries keywords are already called through their run_keyword method, just there would make sense, but which class exactly is reponsible for this?
Is there still interest towards this? The first step should be adding support for custom argument types for Python libraries (that needs a separate issue) and then it could be extended also to dynamic libs. The required changes are so big that they can be done only in a major release like RF 3.1 initially planned for next autumn. I can help with implementation but at least currently don't think I have time to implement it fully myself.
No interest related to this lately. As I commented above, a precondition to adding argument type information into the dynamic API is first adding some way to add argument type information to Python libraries. If anyone gets interested in that, please submit an issue and we can start discussing how it could be implemented. Until that I close this issue.
We now have #2890 about argument conversion with Python libraries and can start thinking how to add argument conversion support to the dynamic (and remote) API as well.
Earlier we have been discussing about adding separate get_keyword_argument_types method to the dynamic API, but on Slack there was an interesting idea about embedding the type information into the spec returned by the existing get_keyword_arguments. That might be more convenient to use and also easier to implement.
The way I see it, for dynamic libraries the get_keyword_arguments can be extended through:
a more complex return, for example a dict containing two lists, one of the arguments, the other with the types (matching would be done by index, with no defined types having empty value
embedded values in each argument, in the form arg:type=default
a simpler return, with two lists, the first list as it is now, which maintains backwards compatibility with older scripts and libraries and the second list containing the types of each argument.
What would be then important for the dynamic libraries (which would like to provide this info) would be to maintain the proper order of both lists, so indexing would provide the correct information. I like the idea of a more complex return, but I am more inclined towards points 2 and 3 as they seem more easier to implement and handle. If we go with point 2, this has the benefit of maintaining backwards compatibility with previous dynamic APIs and it wouldn't affect libraries that do not make use of type hinting (so backwards compatibility with Py2.7 as well).
The challenging points for robot-framework would be:
warn/error out when the conversion fails and not execute the kw or raise a warning and send the str as before?
what to do with the more complex types (list, dict) when the provided value may or may not be convertible - using eval() or ast.literal_eval() would perform an instant conversion given the fact that they are properly formatted in str form (i.e. ast.literal_eval("{'a': 3, 'b': 4}") returns a dict. The issue with both eval() and literal_eval() comes when evaluating standard strings, as eval("hello world") will raise a SyntaxError.
be as efficient as possible to not create a too big of an overhead as to make the whole idea not worthwhile
I would personally go with a warning for point 1, which would then printout something like: "Warning: converting scalar type to x might fail without {} or []" - or anything around this idea.
Overall, I'm very interested in this idea (type conversions) and I do believe it can help the framework (and developers) in general, with:
properly logging information during the execution (as opposed to having the libraries dealing with logging, which may be configured incorrectly)
awesome documentation (UserGuide) which already handles various types of arguments, but which are somewhat
decide directly in the framework where/when/how a keyword is executed, which should prove easier to debug (avoid having to re-run the tests with --loglevel TRACE)
provide a better overall solution which makes the move towards Py3.5+ more feasible and useful
It would be great if more devs would chip in with their feedback regarding dynamic (or static) libraries.
Edit: sorry for the huge wall of boring text :)
I want the return value from get_keyword_arguments to be a list of strings as it is nowadays. If it's impossible to embed needed type information there, we can add separate get_keyword_argument_types.
I'd like it to be possible to declare that an argument is a list or dict. If the passed argument then is a string like [1, 2, 3] or {'a': 1, 'b': 2}, we can then convert that to a list or dict, respectively. The mentioned ast.literal_eval ought to work fine.
changed the title [-]Feature request: add a `get_keyword_argument_types` for dynamic libraries[/-][+]Support automatic argument conversion with dynamic libraries[/+]on Jul 7, 2018
Type conversion based on function annotations (#2890) is now pretty much done, and we are adding conversion based on argument default values (#2932) and explicit information passed via the @keyword decorator (#2947) as well. After these issues are done, we can concentrate on this issue, and the first task is deciding how to get argument types via the dynamic API. We got at least two possibilities:
Add separate get_keyword_argument_types method like initially proposed by this issue.
Support specifying type information in the argument spec returned by the get_keyword_arguments method like arg1: int, arg2, arg3: bool=True.
The latter has a benefit that we wouldn't need to add (incl. test and document) new method to the dynamic API. New method wouldn't needed in the remote API (and remote servers) either. There are some problems with it as well:
Libraries returning type information via get_keyword_arguments wouldn't be compatible with earlier Robot Framework versions.
It's harder for libraries to construct correct argument spec with type information than just return it directly via a dedicated method.
On Robot's end parsing type information from the argument spec is harder (but not that hard) compared to getting it from a dedicated method.
At the moment I feel a dedicated method is better idea. Comments from others are appreciated!
Development started by making it possible to specify types using strings (8808007) including some common aliases. That's likely useful with dynamic libraries when the type information is got from external systems.
Also made a decision to go with a dedicated method to that returns types. The method will be named get_keyword_types.
Activity
pekkaklarck commentedon Jul 21, 2015
Just adding a new method to the dynamic API isn't enough because that type information needs to be used too. The existing argument coercion logic used by Java libraries probably can be reused, but it anyway needs to be hooked up with other libraries.
I also consider adding argument coercion to the normal Python library API more important than adding it to the dynamic API. That ought to be a bit easier too, because there we could just use an attribute like
robot_typesorrobot_argument_typesto specify arguments. That needs a separate issue, though.baudren commentedon Jul 21, 2015
I agree that adding argument coercion for static Python/Jython library would be nice too - though for my current project, I need the dynamic bit. I can open the issue to trigger a discussion, though, if you want.
I am aware that the type information needs to be used, I just don't know where it would be more appropriate (or I misunderstood your sentence). I am searching for the place where it would make more sense to add it. Since dynamic libraries keywords are already called through their
run_keywordmethod, just there would make sense, but which class exactly is reponsible for this?pekkaklarck commentedon Jun 1, 2016
Is there still interest towards this? The first step should be adding support for custom argument types for Python libraries (that needs a separate issue) and then it could be extended also to dynamic libs. The required changes are so big that they can be done only in a major release like RF 3.1 initially planned for next autumn. I can help with implementation but at least currently don't think I have time to implement it fully myself.
pekkaklarck commentedon Jan 30, 2017
No interest related to this lately. As I commented above, a precondition to adding argument type information into the dynamic API is first adding some way to add argument type information to Python libraries. If anyone gets interested in that, please submit an issue and we can start discussing how it could be implemented. Until that I close this issue.
pekkaklarck commentedon Jul 5, 2018
We now have #2890 about argument conversion with Python libraries and can start thinking how to add argument conversion support to the dynamic (and remote) API as well.
Earlier we have been discussing about adding separate
get_keyword_argument_typesmethod to the dynamic API, but on Slack there was an interesting idea about embedding the type information into the spec returned by the existingget_keyword_arguments. That might be more convenient to use and also easier to implement.basic-settings commentedon Jul 6, 2018
The way I see it, for dynamic libraries the
get_keyword_argumentscan be extended through:dictcontaining two lists, one of the arguments, the other with the types (matching would be done by index, with no defined types having empty valuearg:type=defaultWhat would be then important for the dynamic libraries (which would like to provide this info) would be to maintain the proper order of both lists, so indexing would provide the correct information. I like the idea of a more complex return, but I am more inclined towards points 2 and 3 as they seem more easier to implement and handle. If we go with point 2, this has the benefit of maintaining backwards compatibility with previous dynamic APIs and it wouldn't affect libraries that do not make use of type hinting (so backwards compatibility with Py2.7 as well).
The challenging points for robot-framework would be:
stras before?list,dict) when the provided value may or may not be convertible - usingeval()orast.literal_eval()would perform an instant conversion given the fact that they are properly formatted instrform (i.e.ast.literal_eval("{'a': 3, 'b': 4}")returns a dict. The issue with botheval()andliteral_eval()comes when evaluating standard strings, aseval("hello world")will raise aSyntaxError.isinstance(True, int)returns True, becauseboolis a subtype ofint, in which casetype(int) == intis more appropriate (https://docs.python.org/3/reference/datamodel.html#the-standard-type-hierarchy)I would personally go with a warning for point 1, which would then printout something like: "Warning: converting scalar type to x might fail without {} or []" - or anything around this idea.
Overall, I'm very interested in this idea (type conversions) and I do believe it can help the framework (and developers) in general, with:
--loglevel TRACE)It would be great if more devs would chip in with their feedback regarding dynamic (or static) libraries.
Edit: sorry for the huge wall of boring text :)
pekkaklarck commentedon Jul 6, 2018
Few quick comments:
I want the return value from
get_keyword_argumentsto be a list of strings as it is nowadays. If it's impossible to embed needed type information there, we can add separateget_keyword_argument_types.As I commented Automatic argument conversion based on Python 3 function annotations #2890, I think conversion errors should be real errors, not warnings.
I'd like it to be possible to declare that an argument is a list or dict. If the passed argument then is a string like
[1, 2, 3]or{'a': 1, 'b': 2}, we can then convert that to a list or dict, respectively. The mentionedast.literal_evalought to work fine.In general I think it's better to concentrate on Automatic argument conversion based on Python 3 function annotations #2890 first. After that this issue is mainly about figuring out how to pass type information via the dynamic API.
[-]Feature request: add a `get_keyword_argument_types` for dynamic libraries[/-][+]Support automatic argument conversion with dynamic libraries[/+]pekkaklarck commentedon Aug 29, 2018
Type conversion based on function annotations (#2890) is now pretty much done, and we are adding conversion based on argument default values (#2932) and explicit information passed via the
@keyworddecorator (#2947) as well. After these issues are done, we can concentrate on this issue, and the first task is deciding how to get argument types via the dynamic API. We got at least two possibilities:Add separate
get_keyword_argument_typesmethod like initially proposed by this issue.Support specifying type information in the argument spec returned by the
get_keyword_argumentsmethod likearg1: int, arg2, arg3: bool=True.The latter has a benefit that we wouldn't need to add (incl. test and document) new method to the dynamic API. New method wouldn't needed in the remote API (and remote servers) either. There are some problems with it as well:
get_keyword_argumentswouldn't be compatible with earlier Robot Framework versions.At the moment I feel a dedicated method is better idea. Comments from others are appreciated!
Type conversion: Allow specifying type as string
pekkaklarck commentedon Nov 20, 2018
Development started by making it possible to specify types using strings (8808007) including some common aliases. That's likely useful with dynamic libraries when the type information is got from external systems.
Also made a decision to go with a dedicated method to that returns types. The method will be named
get_keyword_types.