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
Automatic type conversion based on argument default values #2932
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
defexample(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.
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.
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.
Activity
pekkaklarck commentedon Aug 14, 2018
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@keyworddecorator to specify argument types. That would require a separate issue, though.Type conversion enhancements and fixes (#2890 and #2932)
Cleanup type conversion tests (#2890 and #2932)
Type conversion fix
Type conversion enhancements
Clean-up argument parsing.
Refactor argument type conversion. #2890 #2932
Disable type conversion with `@keyword(types=None)`.
Regression fix to Telnet bug caused by #2932
pekkaklarck commentedon Nov 4, 2018
This change can cause backwards incompatibility problems in some cases. For example, our own
Telnetlibrary has some keywords that haveNoneas a default value but allow using string'NONE'as a special value as well. Automatic conversion converted strings like that toNone. UsingNoneand'NONE'(orFalseand'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 inTelnet. Others facing similar issues can do the same.