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
Add public argument conversion API for libraries and other tools #4942
We are using TypeConverter class in Robot Framework Browser in multiple places, to convert arguments of *varagrs when calling internal keywords.
Unfortunately this API is no longer be able to digest real type values.
We need “dynamic” type conversion because of many reasons.
With dynamic i mean we do Robot type conversion inside Browser lib when Robot is not able to do.
We do have situations where we deprecate positional args in favour of named only ones. In these cases we introduce a *varags argument that collects all old positional args and assigns them in the old order to the variables inside. Therefore there is no type hinting in the arguments. but we have a Catalog of the original types which can be used to convert.
We do have keywords like Promise To and Wait For Condition which calls our keywords internally and has *args to take the arguments. These two keywords iterate over the called keywords interface and try to convert the arguments according to their type. Also in this case we want/need to support dynamic argument conversion.
For us it is not really important, if exactly this function converter_for() will stay compatible or if a new function will be implemented to get to get the proper converter for a Python type.
However it would be more convenient for our users to stay compatible, so they do not necessarily need to update Browser, when updating RF.
A Public API for this would be nice too.
Or maybe even just a convenient function for Converting value to type as a static method.
Something like TypeConverters.convert_to_type(name: str, value: Any, destination_type: type)
TypeConverter.converter_for() was changed to accept a TypeInfo object instead of a concrete types or their string aliases as part of #4711 that added support for specifying types as strings like 'list[int]' and 'int | float'. It would have been strange if TypeConnverter would handle parsing such strings in addition to doing actual conversion. Instead of that, we moved all logic related to parsing and validating types to the TypeInfo class that was earlier used only by Libdoc. Now responsibilities are clear and TypeConverter only needs to care about actual type conversion.
The TypeConverter isn't part of the public API, but it's nevertheless bad that the change breaks Browser and apparently also the RobotCode plugin. Because conversion functionality clearly is used also externally, the real fix is exposing it as a public, stable API. I think it's best to do it via the TypeInfo class that already has a convert method and can be used like this:
Making this a public API basically requires the following:
Expose TypeInfo via robot.api.
Go trough its public methods to see is there anything to be changed before making it public (easier now than afterwards).
Add docstrings.
Add unit tests for the exposed functionality.
Mention TypeInfo.convert in the User Guide.
We could also consider changing TypeConverter.converter_for so that it would handle plain types as earlier. Due to cyclic module dependencies that would be a bit annoying, though, and there seem to be some other changes to TypeConverter that would also need to be reverted or worked around. I'm not sure is that worth the effort, because affected libraries and tools should nevertheless switch to the public API at some point.
changed the title [-]TypeConverter internal API backwards incompatible with real types in RF 7.0a1[/-][+]Add public argument conversion API for libraries and other tools[/+]on Nov 13, 2023
TypeInfo has now been exposed via robot.api, its documentation has been enhanced, and some bugs with it fixed. Commit 383d432 contains tests that can be used as examples how the API works also in a bit more advanced cases.
Changing TypeConverter so that it would be compatible with RF < 7 would be somewhat big task and would also require changing some of its functions so that they wouldn't be as convenient to use internally as nowadays. Now that we have the public API available, I don't consider that worth the effort. We we can return to that later if needed, but now I consider this issue done.
Activity
pekkaklarck commentedon Nov 13, 2023
TypeConverter.converter_for()was changed to accept aTypeInfoobject instead of a concrete types or their string aliases as part of #4711 that added support for specifying types as strings like'list[int]'and'int | float'. It would have been strange ifTypeConnverterwould handle parsing such strings in addition to doing actual conversion. Instead of that, we moved all logic related to parsing and validating types to theTypeInfoclass that was earlier used only by Libdoc. Now responsibilities are clear andTypeConverteronly needs to care about actual type conversion.The
TypeConverterisn't part of the public API, but it's nevertheless bad that the change breaks Browser and apparently also the RobotCode plugin. Because conversion functionality clearly is used also externally, the real fix is exposing it as a public, stable API. I think it's best to do it via theTypeInfoclass that already has aconvertmethod and can be used like this:Making this a public API basically requires the following:
TypeInfoviarobot.api.TypeInfo.convertin the User Guide.We could also consider changing
TypeConverter.converter_forso that it would handle plain types as earlier. Due to cyclic module dependencies that would be a bit annoying, though, and there seem to be some other changes toTypeConverterthat would also need to be reverted or worked around. I'm not sure is that worth the effort, because affected libraries and tools should nevertheless switch to the public API at some point.[-]TypeConverter internal API backwards incompatible with real types in RF 7.0a1[/-][+]Add public argument conversion API for libraries and other tools[/+]Expose `TypeInfo` publicly for type conversion purposes.
Fix language config with `TypeInfo.convert`.
Use current lang config with `TypeInfo.convert` by default
Mention public conversion API in User Guide. #4942
pekkaklarck commentedon Nov 16, 2023
TypeInfohas now been exposed viarobot.api, its documentation has been enhanced, and some bugs with it fixed. Commit 383d432 contains tests that can be used as examples how the API works also in a bit more advanced cases.Changing
TypeConverterso that it would be compatible with RF < 7 would be somewhat big task and would also require changing some of its functions so that they wouldn't be as convenient to use internally as nowadays. Now that we have the public API available, I don't consider that worth the effort. We we can return to that later if needed, but now I consider this issue done.