Skip to content

Support global scope type converters #4944

Open

Description

@JFoederer

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.

Activity

  1. pekkaklarck commented on Nov 22, 2023

    @pekkaklarck
    Member

    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:

    from robot.api import TypeConverter
    
    @TypeConverter.register
    class MyConverter(TypeConverter):
        ...

    Would that work for you @JFoederer?

    Exposing TypeConverter via robot.api would be trivial, but there would also be some other work needed:

    • Go though TypeConverter to make sure its public API makes sense. We cannot easily change it anymore if it's exposed publicly.
    • Add documentation to all methods and attributes in the public API.
    • Add documentation, with examples, to the User Guide.
    • Tests. This could probably tested pretty thoroughly with unit tests using the already exposed `TypeInfo´ (Add public argument conversion API for libraries and other tools #4942), but some acceptance tests would be needed 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.

  2. JFoederer commented on Nov 24, 2023

    @JFoederer
    ContributorAuthor

    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.

  3. pekkaklarck commented on Nov 24, 2023

    @pekkaklarck
    Member

    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.

  4. JFoederer commented on Dec 11, 2023

    @JFoederer
    ContributorAuthor

    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.
  5. JFoederer commented on Dec 12, 2023

    @JFoederer
    ContributorAuthor

    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.

  6. pekkaklarck commented on Dec 12, 2023

    @pekkaklarck
    Member

    Quick comments to the topics above:

    • 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.
  7. JFoederer commented on Dec 16, 2023

    @JFoederer
    ContributorAuthor

    Issue submitted as requested for the html rendering.

  8. JFoederer commented on Jun 1, 2025

    @JFoederer
    ContributorAuthor

    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.

  9. pekkaklarck commented on Jun 1, 2025

    @pekkaklarck
    Member

    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.

  10. JFoederer commented on Jun 1, 2025

    @JFoederer
    ContributorAuthor

    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.

  11. pekkaklarck commented on Jun 9, 2025

    @pekkaklarck
    Member

    Do I remember right that parameterized types are important for you? In that case I believe we should have a separate issue about them.

  12. JFoederer commented on Jun 10, 2025

    @JFoederer
    ContributorAuthor

    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.

    For background: My use case for parameterized types is about Inline keywords, where this is the code that currently goes outside the api.

  13. pekkaklarck commented on Jun 11, 2025

    @pekkaklarck
    Member

    This is my thinking how this should go:

    1. 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.

    2. 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.

  14. added this to the v7.4 milestone on Jun 11, 2025
  15. JFoederer commented on Jun 13, 2025

    @JFoederer
    ContributorAuthor

    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.

  16. JFoederer commented on Jun 16, 2025

    @JFoederer
    ContributorAuthor

    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.

  17. pekkaklarck commented on Nov 6, 2025

    @pekkaklarck
    Member

    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?

  18. modified the milestones: v7.4, v7.5 on Nov 6, 2025
  19. JFoederer commented on Nov 7, 2025

    @JFoederer
    ContributorAuthor

    @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.

  20. pekkaklarck commented on Nov 7, 2025

    @pekkaklarck
    Member

    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.

  21. pekkaklarck commented on Nov 7, 2025

    @pekkaklarck
    Member

    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.

  22. JFoederer commented on Dec 7, 2025

    @JFoederer
    ContributorAuthor

    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.

  23. modified the milestones: v7.5, v7.6 on Jun 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions