← index #7304PR #5661
Related · high · value 0.113
QUERY · ISSUE

`tuple + class MyTuple(tuple)` performs a wrong type cast

openby dimpoloopened 2021-05-24updated 2026-03-24
py-core
class MyTuple(tuple):
    pass

print((1,2) + MyTuple((3,4)))  # prints (1, 2, (nil))

Relevant code:
https://github.com/micropython/micropython/blob/3b950ed2959c603ff30dd6052c157e7981c4d2d6/py/objtuple.c#L145-L153
mp_obj_tuple_binary_op checks rhs with mp_obj_is_subclass_fast and then casts it to mp_obj_tuple_t

CANDIDATE · PULL REQUEST

py: Simplify tuple subclass equality testing

mergedby dpgeorgeopened 2020-02-18updated 2020-02-19
py-core

This removes code that handles tuple-subclass equality test.

Since commit 3aab54bf434e7f025a91ea05052f1bac439fad8c this piece of code is no longer needed because the top-level function mp_obj_equal_not_equal() now handles the case of user types, and will never call tuple's binary_op function with MP_BINARY_OP_EQUAL and a non-tuple on the RHS.

Tests are added for additional coverage of related parts of this function.

@nickovs FYI, looks to be another little win for the improved equality testing.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied