You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, it is hard to get back the integer value of a class inheriting from int, especially when overriding more and more methods. Consider this example:
Both fail with "TypeError: can't convert A to int". No __int__ or __index__ methods are available; the best workaround I've found is replacing int(self) with +self or self * 1 – provided those are not overridden too.
Use cases: mainly enum-style objects, and everything that wraps semantics around an integer (playground examples are is_prime() and is_odd(), more realistic ones are is_successful() when wrapping response codes).
I've started looking into the code, and my impression is that the mp_obj_get_int function should have one more case. My initial approach was to use the mechanisms around mp_obj_class_lookup to find any native base object if it exists before failing; it turned out that if I went that way, I'd probably have to go all the way and implement a slot for it – not too friendly on RAM usage for a rarely used operation.
Looking for solutions in the other issues (sorry if I'm writing this a second time – I could swear I had attached a note about this to a duplicate bug report yesterday, but failed to find either my comment or the report I though I had found), #238 seems to provide a nice way: Treat int-conversion like a unary operator. (That's suspiciously close to the +a workaround).
Do you consider adding __int__ functionality alongside __bool__ and __len__ as MP_UNARY_OP_INT a viable solution? If so, I'd be happy to implement it.
The text was updated successfully, but these errors were encountered:
Subclassing native types (in this case int) is something that only has basic support. You can find quite a few cases that fail like the one presented here. I don't know a good way to solve it in general...
Currently, it is hard to get back the integer value of a class inheriting from int, especially when overriding more and more methods. Consider this example:
Both fail with "TypeError: can't convert A to int". No
__int__
or__index__
methods are available; the best workaround I've found is replacingint(self)
with+self
orself * 1
– provided those are not overridden too.Use cases: mainly enum-style objects, and everything that wraps semantics around an integer (playground examples are
is_prime()
andis_odd()
, more realistic ones areis_successful()
when wrapping response codes).I've started looking into the code, and my impression is that the
mp_obj_get_int
function should have one more case. My initial approach was to use the mechanisms aroundmp_obj_class_lookup
to find any native base object if it exists before failing; it turned out that if I went that way, I'd probably have to go all the way and implement a slot for it – not too friendly on RAM usage for a rarely used operation.Looking for solutions in the other issues (sorry if I'm writing this a second time – I could swear I had attached a note about this to a duplicate bug report yesterday, but failed to find either my comment or the report I though I had found), #238 seems to provide a nice way: Treat int-conversion like a unary operator. (That's suspiciously close to the
+a
workaround).Do you consider adding
__int__
functionality alongside__bool__
and__len__
as MP_UNARY_OP_INT a viable solution? If so, I'd be happy to implement it.The text was updated successfully, but these errors were encountered: