Skip to content

No int conversion for int-derived types available #1782

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
chrysn opened this issue Jan 14, 2016 · 3 comments
Closed

No int conversion for int-derived types available #1782

chrysn opened this issue Jan 14, 2016 · 3 comments

Comments

@chrysn
Copy link
Contributor

chrysn commented Jan 14, 2016

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:

class A(int):
    __repr__ = lambda self: "<A %d>"%self
    __add__ = lambda self, other: A(int(self) + other)

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.

@dpgeorge
Copy link
Member

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...

@pfalcon
Copy link
Contributor

pfalcon commented Aug 29, 2018

Do you consider adding int functionality alongside bool and len as MP_UNARY_OP_INT a viable solution?

#4084 argues that indeed it is.

@dpgeorge
Copy link
Member

Support for the __int__ special method was added by #4088

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

No branches or pull requests

3 participants