Repository navigation
Fix crash when argument conversion fails with other exception than ValueError - #5820
Conversation
There was a problem hiding this comment.
🟢 Approval recommended
The localized exception handling addresses the reported failures, preserves recursion error propagation, and has focused regression coverage with no unresolved findings.
No open issues 🎉
What changed in this PR
Fixes #5819 by normalizing built-in argument conversion failures to ValueError, allowing existing error handlers to report failures instead of aborting execution.
Changes:
- Wraps unexpected conversion exceptions in the standard conversion error message while preserving
RecursionError. - Adds regression coverage for importer and public API conversion failures.
- Tests invalid keyword arguments, typed command-line variables, and union fallback behavior.
| File | Description |
|---|---|
utest/utils/test_importer_util.py |
Tests importer error reporting for conversion overflow. |
utest/running/test_typeinfo.py |
Tests normalization to ValueError through TypeInfo. |
src/robot/running/arguments/typeconverters.py |
Normalizes conversion exceptions while re-raising RecursionError. |
atest/testdata/keywords/type_conversion/unions.robot |
Adds union fallback cases after conversion overflow. |
atest/testdata/keywords/type_conversion/unions.py |
Adds the timedelta-or-float test keyword. |
atest/testdata/keywords/type_conversion/annotations.robot |
Covers timedelta overflow and unhashable set items. |
atest/robot/variables/variable_types.robot |
Checks typed command-line variable error reporting. |
atest/robot/keywords/type_conversion/unions.robot |
Registers verification of the new union test. |
Review effort: Balanced
|
The handling added to |
|
This looks good in general (I didn't look at tests too closely), but there are some things to discuss:
What do you @d-biehl think about the above and how should we continue with this? One way is that I merge this PR in its current shape and the refactor the related code a bit. I'd remove the RecursionError handling and would also consider handling the |
|
Thanks for the review! My thoughts:
I can add the |
|
The *** Settings ***
Library Collections
*** Test Cases ***
Pathological string to list argument
Log List ${{'-' * 6000 + '1'}}
Next test
No OperationWith the change, the first test fails normally with |
|
Replies to your replies:
How should we proceed? Are you planning to do some changes to this PR still? If not, I'll take a final look and merge it. We can do further adjustments still after that. |
|
Regarding to the point 4 discussed above, I got an idea how we could handle RecursionError and other such errors that we consider severe enough that we don't want to try other conversions. We could give I tested the above so that I added except RecursionError as error:
if reraise_errors:
raise
return self._handle_error(value, name, kind, error)to
I think the above is pretty good. The only small nit is that I'd prefer the the final error message to be prefixed with |
|
I reported the DateTime issue as #5834 and fixed it already. The fix probably requires updating tests added by this PR. |
|
Checks are failing because GitHub Actions doesn't anymore support Python 3.9 on |
After robotframework#5834, too large `timedelta` values don't cause `OverflowError` anymore. Use `float` with a huge integer and `set` with unhashable items instead.
`ast.literal_eval` raises `MemoryError` if the expression is too complex to parse. Report it as an invalid expression. Part of robotframework#5819.
d06bc72 to
a0cb344
Compare
There was a problem hiding this comment.
🟡 Changes recommended
The new unit test can fail on PyPy because parsing may raise a deliberately propagated RecursionError instead of the asserted ValueError; mocking literal_eval to raise MemoryError would make it deterministic.
1 open issue
Review effort: Balanced
| def test_failing_conversion_with_too_complex_expression(self): | ||
| expr = "-" * 6000 + "1" | ||
| assert_raises_with_msg( | ||
| ValueError, | ||
| f"Argument 'x' got value '{expr}' that cannot be converted " | ||
| f"to list[int]: Invalid expression.", | ||
| TypeInfo.from_type_hint("list[int]").convert, | ||
| expr, | ||
| "x", | ||
| ) |
There was a problem hiding this comment.
What do you think about the above @d-biehl? If we handle MemoryError in _literal_eval, patching ast.literal_eval to always raise MemoryError would probably be a good idea. This test likely fails on PyPy otherwise.
The reason I didn't commit the suggestion is that I'm not sure should we handle MemoryError in _literal_eval or should we handle it the same way as we handle RecursionError. If MemoryError is a symptom that the whole interpreter is running out of memory, handling it like RecursionError would make sense. If the error is local to ast.literal_eval parser, then it's better to handle it in _literal_eval.
|
I updated the PR:
About #5834: The |
|
Sounds good to me. Let's proceed like this:
|
|
OK, sounds good to me. Let me know if there's anything else I should do. |
pekkaklarck
left a comment
There was a problem hiding this comment.
Looks very good to me. The main reason I didn't merge this yet is that I'm not sure about handling MemoryError. I see two alternatives:
- We let it go through from
_literal_evaland also let it go through on the higher level similarly asRecursionError. - We handle it in
_literal_eval, but useToo complex expressionor something similar instead ofInvalid expression.
The first approach would avoid the possibility that converting something fails for MemoryError and then we try alternative converter and that succeeds. That cannot happen if you have e.g. arg: list[str] | str, though, because if the argument is a list we use it as-is and non-list arguments don't go to _literal_eval. It could happen if you have arg: list[str] | CustomType, though. Possibly also other converters can fail for MemoryError and in those cases passing the error through like we pass RecursionError could make sense.
| Run Should Fail | ||
| ... -v "BAD_VALUE: int:bad" ${DATADIR}/misc/pass_and_fail.robot | ||
| ... Command line variable '\${BAD_VALUE: int}' got value 'bad' that cannot be converted to integer. | ||
| VAR ${big} 1${{'0' * 400}} |
There was a problem hiding this comment.
This probably could be
VAR ${big: int} 1e400
IntegerConverted uses Decimal for converting stuff that int cannot convert so it handles huge numbers in the scientific format correctly.
There was a problem hiding this comment.
It would be good to add this kind of test as well:
*** Variables ***
${BIG_INT: int} 1e400
${BIG_FLOAT: float} ${BIG_INT} # This fails for "int too large to convert to float"Currently this crashes the whole execution and with these fixes it shouldn't.
| List ${NO_CLASS} arg_type=NoClass | ||
|
|
||
| Too complex expression | ||
| [Documentation] The value is so long that the error message is cut. |
There was a problem hiding this comment.
Also in this case the exception could be specified here using FAIL GLOB: (or something similar).
| # Converters should raise `ValueError`, but, for example, `set()` can | ||
| # raise `TypeError` and `float()` can raise `OverflowError`. | ||
| message = get_error_message() | ||
| return self._handle_error(value, name, kind, ValueError(message)) |
There was a problem hiding this comment.
It's a bit odd to first convert the error to a string, then to ValueError and then handle it in _handle_error. I think _handle_error should just handle all kind of errors. I can refactor this myself. There are also other things like using return with a method that always raises.
| value = literal_eval(value) | ||
| except (ValueError, SyntaxError): | ||
| # Original errors aren't too informative in these cases. | ||
| except (ValueError, SyntaxError, MemoryError): |
There was a problem hiding this comment.
Wouldn't it be safer to let MemoryError go through? In higher level we could then handle it the same way as RecursionError? If we handle MemoryError here, perhaps it would be better to emit a different error like Too complex expression. instead of Invalid expression..
| def test_failing_conversion_with_too_complex_expression(self): | ||
| expr = "-" * 6000 + "1" | ||
| assert_raises_with_msg( | ||
| ValueError, | ||
| f"Argument 'x' got value '{expr}' that cannot be converted " | ||
| f"to list[int]: Invalid expression.", | ||
| TypeInfo.from_type_hint("list[int]").convert, | ||
| expr, | ||
| "x", | ||
| ) |
There was a problem hiding this comment.
What do you think about the above @d-biehl? If we handle MemoryError in _literal_eval, patching ast.literal_eval to always raise MemoryError would probably be a good idea. This test likely fails on PyPy otherwise.
The reason I didn't commit the suggestion is that I'm not sure should we handle MemoryError in _literal_eval or should we handle it the same way as we handle RecursionError. If MemoryError is a symptom that the whole interpreter is running out of memory, handling it like RecursionError would make sense. If the error is local to ast.literal_eval parser, then it's better to handle it in _literal_eval.
|
About 2: I can't do this right now, but I can take it later. I think it's an edge case that hardly anyone hits in practice, so it could also wait until after 7.5.1. I can open a separate issue for it with the cases discussed above, so they don't get lost. If you want to have it in 7.5.1, it's probably better if you do it yourself. |
|
We got so many lists that I'm not sure what you mean with 2. 😂 Anyway, this is what I plan to do next:
|
|
yes exactly that recursion error and memory thing! 🤣 |
|
I ought to now have everything discussed above done. In the end I decided to change handling MemoryError so that's it's not caught in Earlier that I had prototyped handling RecursionErrors so that I'll wait all tests to run locally with CPython and PyPy and then push my changes. |
Don't let RecursionError slip though so that it could crash the whole execution. These errors are still considered more severe than normal conversion failures and internally they stop further conversion attempts. Also handle MemoryError the same way as RecursionError and refactor related code. Part of issue #5819. See also PR #5820.

Fixes #5819.
TypeConverter.convertnow turns exceptions other thanValueErrorinto aValueErrorwith the normal conversion error message.CustomConverteralready does this for custom converters. Callers that handleValueErrortherefore work also when a built-in converter fails with, for example,OverflowError(timedeltawith1e20) orTypeError(setwith unhashable items). Callers includeutils.Importer, typed command line variables, and the Variables section.TypeInfo.convertnow raises onlyValueErroron conversion failures, as documented.RecursionErroris re-raised. Otherwise unions could silently fall back to another member type, such asstr, with very deeply nested values of recursive type aliases.For example,
robot --listener Listener.py:1e20with atimedeltaargument used to crash withUnexpected error. It now reports the error and continues:Library imports keep the context added in #5815. Their message now also names the argument:
Tests:
TestTypeInfo.test_failing_conversion_with_other_exception_than_value_errorandTestInstantiation.test_argument_conversion_failing_with_other_exception_than_value_error, which covers theutils.Importerpath used by listeners, model modifiers and parsers.Invalid timedeltaandInvalid setinkeywords/type_conversion/annotations.robot.Union with item failing with other exception than ValueErrortest inkeywords/type_conversion/unions.robot.Invalid value on CLIinvariables/variable_types.robot.unhashable typemessage changed in Python 3.14, so it is matched with a pattern.I ran the full unit and acceptance test suites with Python 3.13 on Linux. All 2444 unit tests pass. Of the 7144 acceptance tests, 7135 pass. The 9 failing ones are Screenshot library tests, and they fail the same way on unmodified master, because taking screenshots is not supported in my environment. All unit tests and the acceptance tests under
keywords/type_conversion,variables,output/listener_interface,cli/model_modifiersandtest_libraries/library_import_failing.robotalso pass with Python 3.10, 3.14 (free-threaded) and 3.15. The changed files pass ruff, black and isort with the project configuration.AI/tooling disclosure: This contribution was prepared with assistance from Claude Code. I reviewed the result manually, understand the changes, verified them locally, and confirm that I have the right to submit this contribution under the project license.