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
Ability to register custom converters for keyword arguments #4088
This would be a very convenient feature and having it already in RF 5.0 would be great. About the design:
New argument to the @keyword decorator like converters proposed above is probably the best way to enable this with individual keywords.
Converters need to be settable on library level as well to avoid the need to do that with each keyword. Using the @library decorator is probably the best approach. It could have converters as well and converters set on that level would be used by default.
Converters could be matched to arguments either based on argument name, position or type. The former two can nowadays be used when specifying argument types with @keyword, but I believe in this case working based on the type is the best approach. Otherwise specifying converters on the library level would be pretty much impossible. With individual keywords mapping converters based on position could possibly be convenient as it requires less code:
Although there are potential use cases for mapping converters based on position and name, I believe it's best to not implement that at least in the beginning. Let's see if the use case gets actually important when this functionality works otherwise.
Do we need to instruct custom converter creators about error reporting? For example, should we tell them to use ValueError and TypeError only? Or should we just catch all exceptions and report them? The latter is probably easier and kind of needed anyway as we don't want any random exception in conversion to crash the whole execution.
changed the title [-]ability to register custom keyword argument type converters[/-][+]Ability to register custom converters for keyword arguments[/+]on Sep 15, 2021
but I believe in this case working based on the type is the best approach. Otherwise specifying converters on the library level would be pretty much impossible.
i agree. yesterday i actually attempted to create a decorator for keywords to add this functionality but got caught up in the rabbithole of runtime type-checking. for example:
how do you match the type of the dates parameter with the converter? you could create a custom version of issubclass that works on lists but then what about other types that use generics? something to think about...
Are you @DetachHead interested to look at implementing this? I have many other tasks for RF 5.0 and getting help here would be great. I'm obviously happy to review PR and help also otherwise.
One more thing about the design: Library level defaults for converters should probably be read from a class or module level attribute like ROBOT_LIBRARY_CONVERTERS. It would be set by @library decorator but could also be set otherwise. That's the same approach we use e.g. with library scope.
Cool! Let me know if you need any help. Notice also that I'll shortly remote Python 2 support and that is likely to affect pretty much all code at least on some level.
No worries. RF 5 work is now in good speed and Python 2 support has been removed. We'll concentrate on new syntax like TRY/EXCEPT first so there's plenty of time for you to get started. Keep us informed if you do!
*** test cases ***positive test
foo 01/01/2020
test invalid dates
foo 32/01/2020
currently in both tests it passes the value as a str (presumably because they were already strings to begin and since str is in the parameter's types it doesn't bother trying to convert it)
however in my case i want it to try and run the converter, and just fall back to a string if it fails.
That logic isn't related to custom converts but to how Unions are handled. If the given argument has any of the accepted types, it is passed as-is. That behavior has its problems but other alternatives had even more problems. One use case for custom converters actually is being able to control this fully yourself. See the following comment for a bit long explanation with links to relevant issues: #4090 (comment)
It would be great if people interested in this functionality would check the docs and comment if there's something to be enhanced or fixed (incl. typos).
This issue ought to now be done. How custom types will be shown/stored in Libdoc outputs is still likely to change, but that is covered by #4160. Documentation can still be enhanced and I leave this issue open for few days to wait for comments related to that.
Activity
pekkaklarck commentedon Sep 15, 2021
This would be a very convenient feature and having it already in RF 5.0 would be great. About the design:
New argument to the
@keyworddecorator likeconvertersproposed above is probably the best way to enable this with individual keywords.Converters need to be settable on library level as well to avoid the need to do that with each keyword. Using the
@librarydecorator is probably the best approach. It could haveconvertersas well and converters set on that level would be used by default.Converters could be matched to arguments either based on argument name, position or type. The former two can nowadays be used when specifying argument types with
@keyword, but I believe in this case working based on the type is the best approach. Otherwise specifying converters on the library level would be pretty much impossible. With individual keywords mapping converters based on position could possibly be convenient as it requires less code:Working based on position and by name would also allow using different converters even when arguments have same type:
Although there are potential use cases for mapping converters based on position and name, I believe it's best to not implement that at least in the beginning. Let's see if the use case gets actually important when this functionality works otherwise.
Libdoc should show documentation for these converters automatically. Possibly they could be included similarly as Enums and TypedDicts are at the moment (see Libdoc: Store information about enums and TypedDicts used as argument types in spec files #3607 and Libdoc: List enums and TypedDicts used as argument types in HTML automatically #3783, ping @Snooz82). The actual documentation shown for each converter could be simply taken from their docstrings.
Do we need to instruct custom converter creators about error reporting? For example, should we tell them to use ValueError and TypeError only? Or should we just catch all exceptions and report them? The latter is probably easier and kind of needed anyway as we don't want any random exception in conversion to crash the whole execution.
[-]ability to register custom keyword argument type converters[/-][+]Ability to register custom converters for keyword arguments[/+]DetachHead commentedon Sep 16, 2021
thanks for taking this idea on board!
i agree. yesterday i actually attempted to create a decorator for keywords to add this functionality but got caught up in the rabbithole of runtime type-checking. for example:
how do you match the type of the
datesparameter with the converter? you could create a custom version ofissubclassthat works on lists but then what about other types that use generics? something to think about...pekkaklarck commentedon Sep 16, 2021
It seems we can simply use equality check with generics:
Probably should use
issubclassas well to support derived types.pekkaklarck commentedon Sep 16, 2021
Are you @DetachHead interested to look at implementing this? I have many other tasks for RF 5.0 and getting help here would be great. I'm obviously happy to review PR and help also otherwise.
pekkaklarck commentedon Sep 16, 2021
One more thing about the design: Library level defaults for converters should probably be read from a class or module level attribute like
ROBOT_LIBRARY_CONVERTERS. It would be set by@librarydecorator but could also be set otherwise. That's the same approach we use e.g. with library scope.DetachHead commentedon Sep 17, 2021
@pekkaklarck i'm down to give it a go
pekkaklarck commentedon Sep 17, 2021
Cool! Let me know if you need any help. Notice also that I'll shortly remote Python 2 support and that is likely to affect pretty much all code at least on some level.
DetachHead commentedon Oct 16, 2021
@pekkaklarck sorry i haven't really had much time to work on this. i'm still interested to work on it but just don't know when i'll have the time
pekkaklarck commentedon Oct 19, 2021
No worries. RF 5 work is now in good speed and Python 2 support has been removed. We'll concentrate on new syntax like TRY/EXCEPT first so there's plenty of time for you to get started. Keep us informed if you do!
34 remaining items
Fix Union detection with custom converters. Part of #4088.
DetachHead commentedon Dec 16, 2021
what's the expected behavior in cases like this?
currently in both tests it passes the value as a
str(presumably because they were already strings to begin and sincestris in the parameter's types it doesn't bother trying to convert it)however in my case i want it to try and run the converter, and just fall back to a string if it fails.
pekkaklarck commentedon Dec 16, 2021
That logic isn't related to custom converts but to how Unions are handled. If the given argument has any of the accepted types, it is passed as-is. That behavior has its problems but other alternatives had even more problems. One use case for custom converters actually is being able to control this fully yourself. See the following comment for a bit long explanation with links to relevant issues:
#4090 (comment)
Custom converters (#4088): A bit better conversion errors.
Document custom argument conversion support #4088
pekkaklarck commentedon Jan 26, 2022
This functionality is now documented in the User Guide. We only generate the User Guide with final releases, but you the reStructuredText source files are rendered pretty well on GitHub. This particular documentation can be found here:
https://github.com/robotframework/robotframework/blob/master/doc/userguide/src/ExtendingRobotFramework/CreatingTestLibraries.rst#custom-argument-converters
It would be great if people interested in this functionality would check the docs and comment if there's something to be enhanced or fixed (incl. typos).
pekkaklarck commentedon Jan 26, 2022
This issue ought to now be done. How custom types will be shown/stored in Libdoc outputs is still likely to change, but that is covered by #4160. Documentation can still be enhanced and I leave this issue open for few days to wait for comments related to that.
pekkaklarck commentedon Jan 27, 2022
No comments so far and I close this issue. That doesn't mean comments weren't appreciated in the future!
Libdoc: Don't write custom types to legacy dataTypes in specs.
Test custom conversion with embedded args and dynamic libs. #4088
Libdoc: Show type info in modal dialog instead of listing them at the…