-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Automatic argument conversion based on type information passed to @keyword
decorator
#2947
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The example in the description would be equivalent to this example using Python 3 function annotations: def example(arg1: int, arg2, arg3: bool):
# ... |
One design decision to be made is how to handle specified types that don't match types we support. Here are some possibilities:
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 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. |
Initial implementation. Error handling missing.
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. |
Also decided to support giving arguments using a list like @keyword(types=[int, bool])
def example(arg1, arg2):
# ... In simple cases this is less verbose than using a dictionary like @keyword(types={'arg1': int, 'arg2': bool})
def example(arg1, arg2):
# ... Using a list with more arguments than there are types will cause an error, but having less types is fine and @keyword(types=[int, None, bool])
def example(arg1, arg2, arg3):
# ... |
- Old "enum" module is supported along with newer "enum34" and the standard module in Python 3.4+. Old code didn't work with the old module. - Tests requiring enum are tagged with "require-enum". - Enum requirement is documented in atest/README.rst and "enum34" was conditionally (< 3.0) added to atest/requirements.txt. - At the same time made lxml installation in atest/requirement.txt conditional (CPython and PyPy). Related to type conversion issues #2890 and #2947.
- collections.abc.Sequence is now mapped directly to list, not to list or tuple. - collections.abc.Iterable is not supported anymore. - Explicit tests for Integral, Real and ByteString. - Test cleanup.
Makes it easier to disable type conversion altogether, including based on default values, when it is not desired. For example, eases fixing regression in Telnet caused by automatic conversion when `None` is a default value. Change is related to issue #2947 (specifying types using the `@keyword` decorator) and to issue #2932 (getting types from default values).
Changed functionality so that using |
Uh oh!
There was an error while loading. Please reload this page.
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 passingtypes
as a dictionary mapping argument names to types:Additional design decisions related to this proposal:
robot_types
attribute (consistent with the existingrobot_name
androbot_tags
) that can be also set otherwise.arg2
.*varargs
and**kws
.UPDATE: Decided to allow specifying types also as a list to make simple cases less verbose. See the comment below for details.
The text was updated successfully, but these errors were encountered: