Faulty exception: Implicit string conversion: bad_implicit_conversion(...)
Dear MicroPython community,
I stumbled upon a false exception when trying to implicitly convert a string to integer.
Error message should be: "can't convert 'str' object to int implicitly"
Rather than: "can't convert 'int' object to str implicitly" (see Traceback)
I'd be happy to see a fixture in future version or get any feedback of how to solve the issue.
Script:
def func(input1, input2):
return input1/input2
print(func(1,2))
print(func("1",2))
Result:
Traceback (most recent call last):
File "../implicit_conv_exception.py", line 7, in <module>
File "../implicit_conv_exception.py", line 4, in func
TypeError: can't convert 'int' object to str implicitly
Details:
- Executed on Micropython v1.17
- Port: unix
- Methode causing error: STATIC NORETURN void bad_implicit_conversion(mp_obj_t self_in)
py: Use correct exception message for implicit conversion to str or b…
…ytes
When terse error reporting is not active use the actual target type in
the exception message instead of always reporting conversion 'to str'
fails even if it happens to actually be 'to bytes'. See #2957.
Note in the 3 functions near the end of the file I used 'str' as target type no matter what but that seems reasonable.
Also maybe it's worth having a check_implicit_conversion to reduce some duplication? Like
void check_bad_implicit_conversion(mp_obj_t src, mp_obj_type_t *targetType) {
mp_obj_type_t *srcType = mp_obj_get_type(src);
if (srcType != targetType) {
bad_implicit_conversion(srcType, targetType);
}
}
so all occurrences of
if (mp_obj_get_type(args[1]) != self_type) {
bad_implicit_conversion(args[1], self_type);
}
can be replaced with just check_bad_implicit_conversion(args[1], self_type)