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 argument conversion based on type information passed to @keyword decorator #2947
We have lately been working with automatic argument type conversion based on function annotations (#2890) and argument default values (#2932). This is very useful, but function annotations work only with Python 3 and it doesn't make sense to add default values to all arguments. Having a generic and Python version independent type conversion would be handy, and because the actual type conversion functionality is already implemented we'd only need a way to specify types.
My proposal is to support specifying types using the @keyword decorator by passing types as a dictionary mapping argument names to types:
Additional design decisions related to this proposal:
Type information is be stored to the function object as robot_types attribute (consistent with the existing robot_name and robot_tags) that can be also set otherwise.
This explicit Robot specific type information has priority over type information got from annotations or default values.
It is not necessarily to specify types for all arguments. The above example doesn't specify type for arg2.
Like with function annotations, it is possible to specify types for *varargs and **kws.
It is an error to specify a type for a non-existing arguments.
UPDATE: Decided to allow specifying types also as a list to make simple cases less verbose. See the comment below for details.
One design decision to be made is how to handle specified types that don't match types we support. Here are some possibilities:
Make all unrecognized values always an error.
Make values that are not types an error. For example, int and str would be fine but 2 and 'foo' would cause an error.
Ignore non-type values altogether. Only use types we recognize for conversion. Show all types in Libdoc outputs.
Store all values and show them in Libdoc outputs. Only use values we recognize for conversion.
This is related to handling function annotations (#2890) that we don't recognize as supported types. Initially that design worked so that all annotations were shown in Libdoc outputs but only types we recognized were used for conversion (same as 4. above). That was later changed to throw all non-type annotations away, use values we recognize for conversion, and show all type annotation in Libdoc outputs (same as 3.). Because function annotations can be used also for other purposes than specifying types (see PEP-3107), we cannot make unrecognized annotations an error. With explicit types specified via the @keyword decorator that could be done, but I'm not sure is that a good idea.
I'm currently thinking we should preserve all types (and annotations) and show them in Libdoc outputs, but naturally only use types we recognize for conversion. This would allow using type information (and annotations) for documentation purposes as illustrated by PEP-3107 and would also be forward compatible with possible new types we support in the future. The only drawback I see is that someone could specify an incorrect type and it wouldn't be reported. I don't think that's very likely and the problem ought to be easy to see if any tests are created for the keyword.
Decided to implement this so that all given type information is preserved and shown by Libdoc. Obviously only types we recognize can be used for conversion.
Using a list with more arguments than there are types will cause an error, but having less types is fine and None as a type means no type. For example, this is equivalent to the example in the original description:
Changed functionality so that using @keyword(types=None) disables argument conversion altogether, including getting type information based on default values.
Activity
pekkaklarck commentedon Aug 29, 2018
The example in the description would be equivalent to this example using Python 3 function annotations:
pekkaklarck commentedon Aug 29, 2018
One design decision to be made is how to handle specified types that don't match types we support. Here are some possibilities:
intandstrwould be fine but2and'foo'would cause an error.This is related to handling function annotations (#2890) that we don't recognize as supported types. Initially that design worked so that all annotations were shown in Libdoc outputs but only types we recognized were used for conversion (same as 4. above). That was later changed to throw all non-type annotations away, use values we recognize for conversion, and show all type annotation in Libdoc outputs (same as 3.). Because function annotations can be used also for other purposes than specifying types (see PEP-3107), we cannot make unrecognized annotations an error. With explicit types specified via the
@keyworddecorator that could be done, but I'm not sure is that a good idea.I'm currently thinking we should preserve all types (and annotations) and show them in Libdoc outputs, but naturally only use types we recognize for conversion. This would allow using type information (and annotations) for documentation purposes as illustrated by PEP-3107 and would also be forward compatible with possible new types we support in the future. The only drawback I see is that someone could specify an incorrect type and it wouldn't be reported. I don't think that's very likely and the problem ought to be easy to see if any tests are created for the keyword.
Provide argument type info via `@keyword` decorator. #2947
Show all types/annotations in Libdoc outputs.
pekkaklarck commentedon Aug 30, 2018
Decided to implement this so that all given type information is preserved and shown by Libdoc. Obviously only types we recognize can be used for conversion.
pekkaklarck commentedon Aug 30, 2018
Also decided to support giving arguments using a list like
In simple cases this is less verbose than using a dictionary like
Using a list with more arguments than there are types will cause an error, but having less types is fine and
Noneas a type means no type. For example, this is equivalent to the example in the original description:Type conversion error handling
Types via @Keyword override annotations
Tests for return value type/annotation.
Enum related type conversion fixes/enhancements.
Type conversion: Enhanced error reporting. #2890 #2947
Smallish type conversion changes #2890 #2947
Disable type conversion with `@keyword(types=None)`.
pekkaklarck commentedon Nov 4, 2018
Changed functionality so that using
@keyword(types=None)disables argument conversion altogether, including getting type information based on default values.Test and doc that type conversion works with embedded args.
Type conversion: Allow specifying type as string
Remote: Automatic argument conversion support