Skip to content

Don't convert value if it is already a valid type. - #3895

Merged
pekkaklarck merged 1 commit into
robotframework:masterfrom
KotlinIsland:dont_convert_type_when_valid
Mar 30, 2021
Merged

pekkaklarck merged 1 commit into
robotframework:masterfrom
KotlinIsland:dont_convert_type_when_valid

Conversation

@KotlinIsland

@KotlinIsland KotlinIsland commented Mar 18, 2021 •

Copy link
Copy Markdown
Contributor

#3867
#3738

If a value is already a valid type from a type union then there is no need to convert it.

@KotlinIsland
KotlinIsland force-pushed the dont_convert_type_when_valid branch 2 times, most recently from d5483a6 to 5af3d2c Compare March 18, 2021 07:06
@codecov-io

codecov-io commented Mar 18, 2021 •

Copy link
Copy Markdown

Codecov Report

Merging #3895 (1c5d9ee) into master (8e0c2a8) will decrease coverage by 0.06%.
The diff coverage is 0.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3895      +/-   ##
==========================================
- Coverage   75.22%   75.16%   -0.05%     
==========================================
  Files         222      222              
  Lines       18789    18791       +2     
  Branches     3070     3071       +1     
==========================================
- Hits        14132    14123       -9     
- Misses       4128     4137       +9     
- Partials      529      531       +2     
Impacted Files Coverage Δ
src/robot/running/arguments/typeconverters.py 60.30% <0.00%> (-0.36%) ⬇️
src/robot/utils/robotinspect.py 47.62% <0.00%> (-19.04%) ⬇️
src/robot/version.py 55.56% <0.00%> (-11.11%) ⬇️
src/robot/libdocpkg/datatypes.py 94.57% <0.00%> (-1.08%) ⬇️
src/robot/utils/importer.py 83.81% <0.00%> (-0.95%) ⬇️
src/robot/libdocpkg/robotbuilder.py 87.21% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8e0c2a8...1c5d9ee. Read the comment docs.

@KotlinIsland
KotlinIsland force-pushed the dont_convert_type_when_valid branch 4 times, most recently from d4642c2 to 4451ba0 Compare March 18, 2021 12:00
@pekkaklarck

Copy link
Copy Markdown
Member

The current functionality is there by design. The motivation is that we want conversion to be done if you have type like Union[int, str] and use argument like 42. I don't want that to be changed, but I don't like too much that currently if you have Union[int, float] and use it with ${42.9} that gets converted to an integer 42. Possibly we should always convert strings but with others with check is the type valid already. Anyway, this change needs a separate issue and cannot be done before RF 4.1.

@KotlinIsland

KotlinIsland commented Mar 18, 2021 •

Copy link
Copy Markdown
Contributor Author

The current functionality breaks optional strings Optional[str] completely.

If the purpose of this is to allow int/float/enum/None literals to override str then can we skip forced conversion when value is not a string

    def _no_conversion_needed(self, value):
        if isinstance(value, str):
            return False
        # None is not a valid type, convert to NoneType
        valid_instance_tuple = tuple(type(it) if it is None else it for it in self.args)
        return isinstance(value, valid_instance_tuple)

Or just always put str last in the conversion? I don't like this option as it's adding more complexity for the user.

    def _convert(self, value, explicit_type=True):
        if str in self.args:
            args = tuple(arg for arg in self.args if arg != str) + str
        else:
            args = self.args
        for typ in args:
            converter = TypeConverter.converter_for(typ)
            if not converter:
                return value
            try:
                return converter.convert('', value, explicit_type)
            except ValueError:
                pass
        raise ValueError

@KotlinIsland
KotlinIsland force-pushed the dont_convert_type_when_valid branch from 4451ba0 to 78887b7 Compare March 18, 2021 23:25
@pekkaklarck

Copy link
Copy Markdown
Member

How does the current functionality break Optional[str]? This anyway needs an issue to discuss the design.

@KotlinIsland

Copy link
Copy Markdown
Contributor Author

Optional[str] becomes Union[str, None] making it impossible to pass None from robot.

@KotlinIsland

Copy link
Copy Markdown
Contributor Author

issue: #3897

@pekkaklarck

Copy link
Copy Markdown
Member

I agree ${None} being converted to a string is not good if you use Optional[str], but I wouldn't say that totally breaks it. There's also a simple workaround that you omit Optional and just use arg=None. Anyway, let's discuss this more in #3897.

@pekkaklarck

Copy link
Copy Markdown
Member

After thinking this more, I believe the approach this PR originally used is the best one. See my comment to issue #3897 for more details. I also submitted a separate issue about the problem with arg: Optional[str] = None (#3908).

if isinstance(value, str):
return False
# None is not a valid type, convert to NoneType
valid_instance_tuple = tuple(type(it) if it is None else it for it in self.args)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This could be written like

NoneType = type(None)
types = tuple(typ if typ is not None else NoneType for typ in self.args)

to avoid the need for the comment.

@KotlinIsland
KotlinIsland force-pushed the dont_convert_type_when_valid branch 2 times, most recently from 73eb67b to 5fd1f93 Compare March 29, 2021 23:44
@KotlinIsland
KotlinIsland force-pushed the dont_convert_type_when_valid branch from 5fd1f93 to 1c5d9ee Compare March 30, 2021 07:15
@pekkaklarck

Copy link
Copy Markdown
Member

Looks great. Docs related to Union conversion need to still be updated but I can do that myself. I'll also make sure we have tests covering the arg: Optional[str] = None case (#3908).

@pekkaklarck

Copy link
Copy Markdown
Member

One of the tests was actually failing. I should've checked CI status more before merging, but I was anyway planning to enhance tests a bit so I'll fix the test at the same time.

pekkaklarck added a commit that referenced this pull request Mar 30, 2021
- Fix a test failing after PR #3895.
- Explicit test for `arg: Optional[str] = None`. Fixes #3908.
- Enhance tests a bit in general.
- Same order of tests in "robot" and "testdata" sides.
- Whitespace cleanup.
@KotlinIsland
KotlinIsland deleted the dont_convert_type_when_valid branch March 30, 2021 23:24
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.

3 participants