Skip to content

Automatic type conversion based on argument default values #2932

Closed

Description

@pekkaklarck

RF 3.1 is going to get automatic type conversion based on function annotations (#2890). That's a really cool feature, but annotations being Python 3 feature means it doesn't benefit libraries that need to support Python 2. Even with Python 3 based libraries, this feature requires adding annotations.

In addition to getting type information from annotations, we could also get it from argument default values. For example, if we have a keyword like

def example(count=0, logged=False):
    # ...

we can see that argument count is an integer (or at least a number) and logged is a Boolean. This would be compatible with Python 2 and we could use existing information.

Implementing this would be pretty easy, because the actual type conversion could be done using same logic (and code) as when using annotations. The main open question I see regarding to this is handling values that cannot be converted to a matching type. When doing conversion based on explicit type annotations such values generally cause an error, but I'm not sure is that acceptable if we get type information implicitly from default values. That would also be a backwards incompatible change.

Activity

  1. added this to the v3.1 milestone on Aug 14, 2018
  2. pekkaklarck commented on Aug 14, 2018

    @pekkaklarck
    MemberAuthor

    One possible problem with this enhancement is that it can encourage library developers to add default values just to get automatic type conversion even if a default wouldn't otherwise be useful. It can also encourage using mutable default values like container=[] which generally aren't recommended in Python. If we consider this a real concern, we could make it possible to use, for example, the @keyword decorator to specify argument types. That would require a separate issue, though.

  3. pekkaklarck commented on Nov 4, 2018

    @pekkaklarck
    MemberAuthor

    This change can cause backwards incompatibility problems in some cases. For example, our own Telnet library has some keywords that have None as a default value but allow using string 'NONE' as a special value as well. Automatic conversion converted strings like that to None. Using None and 'NONE' (or False and 'FALSE') as values that work differently is not that good idea in general, but it is likely that also others have done something similar.

    It was already earlier possible to disable automatic conversion based on default values by specifying an explicit type to certain arguments, and commit 782010e made it possible to use @keyword(types=None) to disable automatic type conversion altogether. The latter approach was then used in 4803acc to fix the regression issue in Telnet. Others facing similar issues can do the same.

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