Skip to content

Support type conversion with multiple possible types #3738

Closed

Description

@pekkaklarck

Currently our argument conversion logic allows specifying only one possible type for each argument. There are, however, use cases where declaring that, for example, either a list or a string is accepted and Robot should then try out different conversions. With type hints the natural syntax to use is Union like arg: Union[list, str].

The basic logic with multiple possible types would be going through all types one-by-one, trying to convert using each type, and going to the next one if conversion fails. There are, however, plenty of design decisions to be made about different details:

  1. Should we first check does the given argument match any of the possible types before doing conversion? If, for example, we have arg: Union[str, int] and the given argument is an integer, should we pass it as is (it matches one of the possible types) or should we test conversion with possible types one-by-one? In the latter case we'd convert the integer to a string because the first possible type is str. In this case checking does the argument match any of the types feels better.

    On the other hand, if we have arg: Union[int, str] and call with a string 2, checking does the given argument match any of the type would mean that we don't do any conversion because one of the types is str. If we'd go through types one-by-one, we'd convert it to an integer because int is before str. In this case testing conversion one-by-one works better.

    We could also make it so that we test conversions one-by-one if the given argument is a string and otherwise first check does the argument match any of the types. That feels a bit inconsistent and hard to explain, though. I got a feeling that going through types one-by-one works best. The key thing to remember is that str should be the last type to avoid converting all arguments to strings.

  2. What to do if we don't recognize one of the possible types? I think we should test conversion with all recognized types that are before it but return the value as-is if no conversion succeeds. For example, arg: Union[int, MyThing] would convert string 2 to an integer but string xxx would be left as-is. This would be consistent with how we handle arg: MyThing.

  3. How to support multiple types with the @keyword decorator? We could require using Union also with it, but then this syntax wouldn't be supported with Python 2 and needing to use typing may not always be desirable with Python 3 either. I think we could allow using a tuple to types like @keyword(types={'arg': (list, str)} as an alias for Union.

  4. If we support a tuple of types with @keyword, should we also support it with type hints like arg: (list, str)? Type checkers probably won't like that but I see no harm us supporting it assuming it doesn't require extra work.

  5. How to handle None as type? The typing module considers None an alias for NoneType in type hints and thus if there's and argument like arg: None, we actually get NoneType as the type because typing.get_type_hints() does conversion automatically. We probably want to do that same also with types specified using @keyword(types: {'arg': None}). When giving types using @keyword as a list, None currently means that the matched argument doesn't have type info and that probably cannot be changed.

Activity

  1. added this to the v4.0 milestone on Oct 25, 2020
  2. pekkaklarck commented on Oct 26, 2020

    @pekkaklarck
    MemberAuthor

    After the above commits this is now mostly done. Missing tasks:

    • Decide how to support @keyword.
    • Documentation.
  3. added a commit that references this issue on Nov 2, 2020
    24cec81
  4. KotlinIsland commented on Mar 18, 2021

    @KotlinIsland
    Contributor

    @pekkaklarck Everything can be converted to a string, should we add a special case where if string is in the valid types, run that conversion last?

  5. mkorpela commented on Mar 18, 2021

    @mkorpela
    Member

    There are similar other types such as boolean.

  6. pekkaklarck commented on Mar 18, 2021

    @pekkaklarck
    MemberAuthor

    We possibly could change this so that if the given argument is not a string, we only convert it if it isn't already one of the accepted types. Anyway, changes need a new issue and cannot be done before RF 4.1.

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

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions