Skip to content
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

Adds enum support to the typeconverter. #2908

Merged
merged 3 commits into from
Jul 28, 2018
Merged

Adds enum support to the typeconverter. #2908

merged 3 commits into from
Jul 28, 2018

Conversation

jeroenroosen
Copy link
Contributor

@jeroenroosen jeroenroosen commented Jul 23, 2018

This PR adds Enum support to the typeconverter and relevant tests.

Because the enum I added for the test starts with an upper case character I wasn't able to use the existing template for the expected failure.

Copy link
Member

@pekkaklarck pekkaklarck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good in general! See the individual review comments for things I'd like to still be changed.

Run Keyword And Expect Error
... ValueError: Argument 'argument' cannot be converted to Foo, got 'foobar'.
... Enum foobar

*** Keywords ***
Conversion Should Fail
[Arguments] ${kw} ${arg}
Copy link
Member

@pekkaklarck pekkaklarck Jul 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You ought to be able to add new argument ${type}=${kw.lower()} here and use this keyword also with the new Invalid Enum test like

Conversion Should Fail    Enum    foobar    type=Foo

or with [Template] if you want to test multiple values like existing tests do.

@@ -14,6 +14,7 @@
# limitations under the License.

from ast import literal_eval
from enum import EnumMeta
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enums being new in Python 3.4, this is probably the first change not compatible with Python 3.3. Good that we decided to drop its support along with Python 2.6 in #2276.

@@ -39,12 +40,17 @@ def convert(self, positional, named):
def _convert(self, name, value):
if name not in self._argspec.types or not is_string(value):
return value
if value.upper() == 'NONE':
return None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is moving these two lines related to enums? For me it feels a bit wrong to convert NONE string to None always if an argument has annotation. For example, if we have def example(arg: str), we should pass all strings (and all other arguments) without changes. This conversions is ought to be safe if the annotation is a type we are going to convert and know wouldn't work with strings in the first place.

try:
return type_[value]
except KeyError:
self._raise_convert_failed(name, type_.__name__, value)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although enums need to be recognized differently than other types, it would be better to have a separate _conver_enum helper for conversion. Code here could hen be change to something like:

if isinstance(type_, EnumMeta):
    converter = self._convert_enum
else:
    converter = self._converters.get(type_)

@jeroenroosen
Copy link
Contributor Author

I incorporated your feedback, is this enum converter function what you had in mind?

@pekkaklarck
Copy link
Member

Didn't realize _convert_enum needs a different signature than other convert functions. How you handled that with _enum_converter_for works fine.

@pekkaklarck
Copy link
Member

Didn't notice earlier that you haven't added new tests to atest/robot side. In these cases they are really simple, could even say dummy, and I can add them myself as well. If you are interested to learn more about Robot's acceptance test system and want to add them yourself, check atest/README.rst first. Otherwise this PR is ready to be merged.

@pekkaklarck
Copy link
Member

I'll add the missing tests myself. I got some extra time for coding and want to merge this PR to avoid conflicts.

@pekkaklarck pekkaklarck merged commit 5da877a into robotframework:type-conversion Jul 28, 2018
@jeroenroosen jeroenroosen deleted the type-conversion branch July 29, 2018 09:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants