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
I would like to register a custom type in general, rather than with a specific keyword library.
I like creating keyword libraries for certain, more complex, data types. Keywords than include factory keywords, operator keywords and some for managing storage. Other keyword libraries can then refer to these types and take them as argument, in which case the converters are super handy, but they aren't available at that place.
In my current solution I reverted to subclassing TypeConverter but this is not part of the public API. It would be better to have the option to register a custom type in general, so that any keyword library that uses the type can also benefit from the converters.
This would be a good enhancement. A relatively simple solution would be exposing TypeConverter publicly so that it could be used as a base class and registered as well:
Nothing above is too complicated, but there's quite a bit to do. I won't personally have time for that withing RF 7.0 timeframe, but I'd be happy to help and review PRs if you @JFoederer or someone else want to take a look at this.
Yes, that would work, because that is the exact method I am now using as a work around. My proposal is to first bring my own library up-to-date with the RF7 changes, secretly still using the unexposed interface. If that works fine we at least have a working example and it gives us some time to think a bit broader and investigate possible alternatives, without the time pressure. It would be great to formalize this towards RF8 though.
Sounds good. I don't think this is too big a task and it shouldn't have any backwards compatibility concerns, so it could be done already in RF 7.1. We basically just need to make sure the TypeConverter API is as lean as possible and we don't accidentally expose internal logic publicly.
There most likely isn't time to get this done in RF 7.0. That said, if you have ideas how to enhance the TypeConver now so that it would work better as an official public API, let me know. If we can make the API stable already now, then it's easy for users to support also RF 7.0 by just importing TypeConverter from a different place.
A few considerations regarding libdoc around this topic:
When registering a converter for a new type using @TypeConverter.register, libdoc generation currently fails. It expects the type to be present in STANDARD_TYPE_DOCS from robot.libdocpkg.standardtypes. It should be possible to add a custom types list that is extended automatically by the register decorator, making use of the type's docstring.
When using generic types it is useful to have access to type_name from robot.utils for user reporting.
Generic types are decoupled from their nested types. So, when you have MyType[int] and MyType[float], then usages link to all keywords using MyType, regardless of the nested type. This is probably ok for some situations, but not correct for others.
Potential issue with libdoc. If the non-technical type name contains spaces, then the html render fails to display the type's docs in the type info dialog.
Global converters obviously should be able to conveniently provide their own documentation. I guess they could have a doc attribute and we could use __doc__ as a fallback.
There's currently a hard error if a type isn't in STANDARD_TYPE_DOCS to make sure we don't forget adding those docs. That's a good thing in general, because I would have otherwise forgotten that several times (last time with the recently added Literal). I can see that it's rather annoying in your use case, and we can think about making the API nicer even before officially supporting global converters. Possibly we could move the standard docs to TypeConverter docstrings already now.
robot.utils is considered stable (we make changes and remove outdated utils, but always with a deprecation period), which means that type_name can be used just fine.
Can you elaborate where MyType part of MyType[int] and MyType[float] linking to same documentation would be an issue? Linking types to type docs is far from trivial, and I would only touch it if there are really good reasons to do that. It anyway shouldn't be part of global converter support.
Type names with spaces not being shown correctly in HTML is a bug that should be fixed. Please submit an issue.
I started work on this issue and draft pull request #5442 has the basics working. Based on this first result, I feel it shouldn't be necessary to expose TypeConverter in full. There are however some hooks missing for more advanced scenarios, so a way needs to be found to communicate extra info to the converter functions when needed.
Some aren't essential, but could be useful for optimisation, like the no_conversion_needed method. The only essential one I encountered so far is info about nested types and access to their respective converters when using generic types.
If at all possible, I'd prefer global converters to be just callables the same way as normal custom converters. If we absolutely need to expose TypeConverter to be used as a baseclass, then that approach should also work with normal custom converters. In that case I believe we'd also need two separate issues.
I would also prefer to keep it simple. Exposing less and keeping it easy to use. So, we drop the idea as suggested early in this thread to expose TypeConverter. Agreed.
First results from the pull request are promising. It already offers functional conversion and works with LibDoc. I will continue on that path and see what else is needed.
Yes indeed, you remember correctly. That would be the next step in the pull request, but do I understand correctly that you prefer to deliver the already implemented part first? That should also be possible after adding the relevant tests.
I do have the parameterized types working as well, but that still exposes too much now. My plan is the following: when defining the converter function you can define an extra predefined attribute def my_converter(value, nested_types=True): When present, the nested_type argument will contain the list of sub-types when the converter is called.
The second part that is needed to support my use case without bypassing the api, is having programmatic access to already available converters. I was thinking to add a function to the utils library: type_convert(value, target_type). This will either return a value of the target type, or a ValueError if conversion is not supported.
Global converters are registered exactly as normal custom converters. That is covered by this issue. I believe this would be a very good issue to RF 7.4, because it makes the newly added variable conversion much more powerful.
Parameterized types work the same way regardless are they global or per-library. Please submit a separate issue about this so we have a separate place where to discuss it. I have some ideas how it could possibly work at least in simple cases, but let's have that discussion in that separate issue. We should try to get it into RF 7.4 as well, but I believe we should concentrate on global converters first. PRs should also be separate.
Ok, I will create a separate issue as follow up for parameterized types.
For this one we will need to be more specific about what you mean by: Global converters are registered exactly as normal custom converters.
Obviously, from a user perspective you can't register it in the same way, because then it is linked to a library. From a technical perspective, I also doubt that you want it exactly the same. The intended behaviour is much closer to how standard types work, which involves way less passing around of the converters.
Having said that, I do think that the current draft PR maybe mixes standard types and global custom converters too much. So, what do we really need and want? Both from a user's and technical perspective.
I read here in typeconverters.py: type_name = None # Used also by Libdoc. Can be overridden by instances.
In the PR there are some quirks that could be related to this:
The use of STANDARD_TYPE_DOCS (code link), which is only there for LibDoc support.
The need for __init__() in the proxy class (code link), which is only there for type_name.
@pekkaklarck, can you shed some light on this? Why type_name is special and how tightly this is connected to LibDoc and/or other reasons? I do see other situations where type_name isn't showing up as expected.
Sorry, I've been busy with other things and haven't had time to look at this properly. Have you @JFoederer continued working with this and have you found answers to your questions? I don't remember every use case for the type_name attribute, but one usage is with parameterized types. If you use something like list[int], we want to show that as the name and not just list. It's certainly possible that this information could be preserved otherwise and the attribute could be removed, but that should be done separately.
RF 7.4 has been delayed so much that we must now get it released as soon as possible. I'm afraid this issue would require so much work still that it's best to move it to RF 7.5 scope. The next step I'd like to see is the design how this would work in practice. Should there be a command line option that registers converters that are then available everywhere, including in variable conversion, or how should it work?
@pekkaklarck, no, I did not continue on this issue and I agree with the shift to a later version. I do need some input before I can continue.
My proposed approach still includes using the decorator for registering types. No command line options needed, Although we did see a possible future conflict when expanding to include parameterized types. I need to read the back the details on that.
If possible, I would like to get rid of the separate STANDARD_TYPE_DOCS and the current way of registering standard types. This is currently quite a tricky bit of code, that I still don't fully grasp. Ideally we have a single registration method that we use internally for the standard types and can also be used by users to register their domain specific types.
Even with a decorator there needs to be a way to run the code before execution starts. Because things like listener and pre-run modifier imports support argument conversion, custom converters should be loaded even before them.
The main problem with the decorator approach is that it's different than just using a type-callable dictionary we use with custom converters registered by libraries. There needed to be good reasons for that and I don't see what they would be. If the reason is the parameterization support, I would first try to solve it with the current type-callable approach.
The STANDARD_TYPE_DOCS is there to allow Libdoc to show gneric documentation for standard types. With custom types the documentation comes from the type or the converter callable, and that's another reason to use use the current approach. If we must come up with a new way to register converters, it should support documentation as well.
I need to recalibrate my thinking, following these last additions. For instance, I had not considered things like listener and pre-run modifier imports. @pekkaklarck, can you explain a bit more what exactly you mean by the concept of the type-callable approach? I feel I haven't picked that up sufficently from just reading the code.
Activity
pekkaklarck commentedon Nov 22, 2023
This would be a good enhancement. A relatively simple solution would be exposing
TypeConverterpublicly so that it could be used as a base class and registered as well:Would that work for you @JFoederer?
Exposing
TypeConverterviarobot.apiwould be trivial, but there would also be some other work needed:TypeConverterto make sure its public API makes sense. We cannot easily change it anymore if it's exposed publicly.Nothing above is too complicated, but there's quite a bit to do. I won't personally have time for that withing RF 7.0 timeframe, but I'd be happy to help and review PRs if you @JFoederer or someone else want to take a look at this.
JFoederer commentedon Nov 24, 2023
Yes, that would work, because that is the exact method I am now using as a work around. My proposal is to first bring my own library up-to-date with the RF7 changes, secretly still using the unexposed interface. If that works fine we at least have a working example and it gives us some time to think a bit broader and investigate possible alternatives, without the time pressure. It would be great to formalize this towards RF8 though.
pekkaklarck commentedon Nov 24, 2023
Sounds good. I don't think this is too big a task and it shouldn't have any backwards compatibility concerns, so it could be done already in RF 7.1. We basically just need to make sure the TypeConverter API is as lean as possible and we don't accidentally expose internal logic publicly.
There most likely isn't time to get this done in RF 7.0. That said, if you have ideas how to enhance the TypeConver now so that it would work better as an official public API, let me know. If we can make the API stable already now, then it's easy for users to support also RF 7.0 by just importing TypeConverter from a different place.
JFoederer commentedon Dec 11, 2023
A few considerations regarding libdoc around this topic:
@TypeConverter.register, libdoc generation currently fails. It expects the type to be present inSTANDARD_TYPE_DOCSfromrobot.libdocpkg.standardtypes. It should be possible to add a custom types list that is extended automatically by theregisterdecorator, making use of the type's docstring.type_namefromrobot.utilsfor user reporting.MyType[int]andMyType[float], then usages link to all keywords usingMyType, regardless of the nested type. This is probably ok for some situations, but not correct for others.JFoederer commentedon Dec 12, 2023
Potential issue with libdoc. If the non-technical type name contains spaces, then the html render fails to display the type's docs in the type info dialog.
pekkaklarck commentedon Dec 12, 2023
Quick comments to the topics above:
docattribute and we could use__doc__as a fallback.STANDARD_TYPE_DOCSto make sure we don't forget adding those docs. That's a good thing in general, because I would have otherwise forgotten that several times (last time with the recently addedLiteral). I can see that it's rather annoying in your use case, and we can think about making the API nicer even before officially supporting global converters. Possibly we could move the standard docs toTypeConverterdocstrings already now.robot.utilsis considered stable (we make changes and remove outdated utils, but always with a deprecation period), which means thattype_namecan be used just fine.MyTypepart ofMyType[int]andMyType[float]linking to same documentation would be an issue? Linking types to type docs is far from trivial, and I would only touch it if there are really good reasons to do that. It anyway shouldn't be part of global converter support.JFoederer commentedon Dec 16, 2023
Issue submitted as requested for the html rendering.
JFoederer commentedon Jun 1, 2025
I started work on this issue and draft pull request #5442 has the basics working. Based on this first result, I feel it shouldn't be necessary to expose
TypeConverterin full. There are however some hooks missing for more advanced scenarios, so a way needs to be found to communicate extra info to the converter functions when needed.Some aren't essential, but could be useful for optimisation, like the
no_conversion_neededmethod. The only essential one I encountered so far is info about nested types and access to their respective converters when using generic types.pekkaklarck commentedon Jun 1, 2025
If at all possible, I'd prefer global converters to be just callables the same way as normal custom converters. If we absolutely need to expose
TypeConverterto be used as a baseclass, then that approach should also work with normal custom converters. In that case I believe we'd also need two separate issues.JFoederer commentedon Jun 1, 2025
I would also prefer to keep it simple. Exposing less and keeping it easy to use. So, we drop the idea as suggested early in this thread to expose
TypeConverter. Agreed.First results from the pull request are promising. It already offers functional conversion and works with LibDoc. I will continue on that path and see what else is needed.
pekkaklarck commentedon Jun 9, 2025
Do I remember right that parameterized types are important for you? In that case I believe we should have a separate issue about them.
JFoederer commentedon Jun 10, 2025
Yes indeed, you remember correctly. That would be the next step in the pull request, but do I understand correctly that you prefer to deliver the already implemented part first? That should also be possible after adding the relevant tests.
I do have the parameterized types working as well, but that still exposes too much now. My plan is the following: when defining the converter function you can define an extra predefined attribute
def my_converter(value, nested_types=True):When present, thenested_typeargument will contain the list of sub-types when the converter is called.The second part that is needed to support my use case without bypassing the api, is having programmatic access to already available converters. I was thinking to add a function to the utils library:
type_convert(value, target_type). This will either return a value of the target type, or aValueErrorif conversion is not supported.For background: My use case for parameterized types is about Inline keywords, where this is the code that currently goes outside the api.
pekkaklarck commentedon Jun 11, 2025
This is my thinking how this should go:
Global converters are registered exactly as normal custom converters. That is covered by this issue. I believe this would be a very good issue to RF 7.4, because it makes the newly added variable conversion much more powerful.
Parameterized types work the same way regardless are they global or per-library. Please submit a separate issue about this so we have a separate place where to discuss it. I have some ideas how it could possibly work at least in simple cases, but let's have that discussion in that separate issue. We should try to get it into RF 7.4 as well, but I believe we should concentrate on global converters first. PRs should also be separate.
JFoederer commentedon Jun 13, 2025
Ok, I will create a separate issue as follow up for parameterized types.
For this one we will need to be more specific about what you mean by: Global converters are registered exactly as normal custom converters.
Obviously, from a user perspective you can't register it in the same way, because then it is linked to a library. From a technical perspective, I also doubt that you want it exactly the same. The intended behaviour is much closer to how standard types work, which involves way less passing around of the converters.
Having said that, I do think that the current draft PR maybe mixes standard types and global custom converters too much. So, what do we really need and want? Both from a user's and technical perspective.
JFoederer commentedon Jun 16, 2025
I read here in typeconverters.py:
type_name = None # Used also by Libdoc. Can be overridden by instances.In the PR there are some quirks that could be related to this:
STANDARD_TYPE_DOCS(code link), which is only there for LibDoc support.__init__()in the proxy class (code link), which is only there for type_name.@pekkaklarck, can you shed some light on this? Why
type_nameis special and how tightly this is connected to LibDoc and/or other reasons? I do see other situations where type_name isn't showing up as expected.pekkaklarck commentedon Nov 6, 2025
Sorry, I've been busy with other things and haven't had time to look at this properly. Have you @JFoederer continued working with this and have you found answers to your questions? I don't remember every use case for the
type_nameattribute, but one usage is with parameterized types. If you use something likelist[int], we want to show that as the name and not justlist. It's certainly possible that this information could be preserved otherwise and the attribute could be removed, but that should be done separately.RF 7.4 has been delayed so much that we must now get it released as soon as possible. I'm afraid this issue would require so much work still that it's best to move it to RF 7.5 scope. The next step I'd like to see is the design how this would work in practice. Should there be a command line option that registers converters that are then available everywhere, including in variable conversion, or how should it work?
JFoederer commentedon Nov 7, 2025
@pekkaklarck, no, I did not continue on this issue and I agree with the shift to a later version. I do need some input before I can continue.
My proposed approach still includes using the decorator for registering types. No command line options needed, Although we did see a possible future conflict when expanding to include parameterized types. I need to read the back the details on that.
If possible, I would like to get rid of the separate
STANDARD_TYPE_DOCSand the current way of registering standard types. This is currently quite a tricky bit of code, that I still don't fully grasp. Ideally we have a single registration method that we use internally for the standard types and can also be used by users to register their domain specific types.pekkaklarck commentedon Nov 7, 2025
Even with a decorator there needs to be a way to run the code before execution starts. Because things like listener and pre-run modifier imports support argument conversion, custom converters should be loaded even before them.
pekkaklarck commentedon Nov 7, 2025
The main problem with the decorator approach is that it's different than just using a type-callable dictionary we use with custom converters registered by libraries. There needed to be good reasons for that and I don't see what they would be. If the reason is the parameterization support, I would first try to solve it with the current type-callable approach.
The STANDARD_TYPE_DOCS is there to allow Libdoc to show gneric documentation for standard types. With custom types the documentation comes from the type or the converter callable, and that's another reason to use use the current approach. If we must come up with a new way to register converters, it should support documentation as well.
JFoederer commentedon Dec 7, 2025
I need to recalibrate my thinking, following these last additions. For instance, I had not considered things like listener and pre-run modifier imports. @pekkaklarck, can you explain a bit more what exactly you mean by the concept of the type-callable approach? I feel I haven't picked that up sufficently from just reading the code.