Skip to content
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

Special operator's methods (like __add__) don't work in jitclass #7372

Closed
polkovnikov opened this issue Sep 5, 2021 · 1 comment · Fixed by #5877
Closed

Special operator's methods (like __add__) don't work in jitclass #7372

polkovnikov opened this issue Sep 5, 2021 · 1 comment · Fixed by #5877

Comments

@polkovnikov
Copy link

polkovnikov commented Sep 5, 2021

Reporting a bug

Seems that jitclass works incorrectly with all special methods like __add__, __mul__, etc. Minimal reproducible example:

import numba

@numba.experimental.jitclass([('x', numba.int64)])
class C:
    def __init__(self, x):
        self.x = x
    def __add__(self, b):
        return self.x + b.x

@numba.njit
def f():
    #C(3) + C(5)
    return C(13).__add__(C(17))
print(f())

if 1:
    @numba.jit(forceobj = True)
    def g():
        #return C(3) + C(5)
        return C(13).__add__(C(17))
    print(g())

print(C(7).__add__(C(11)))
print(C(3) + C(5))

Call of njitted-function f() works and prints correct answer. Call of jitted-function g() (in object mode) with same call of __add__ prints exception AttributeError: 'C' object has no attribute '__add__'. Same exception is printed for the following not-jitted code C(7).__add__(C(11)). Seems that this __add__ method is somehow removed from class for jit/no-jit case.

And finally code C(3) + C(5) works nowhere, not in njit, not in jit, not in unjitted code. It prints error TypeError: unsupported operand type(s) for +: 'C' and 'C' (for jit/no-jit) and long error dump for njit. It means python doesn't detect __add__ method as special method that implements + operator.

Regular methods of jitclass (without special __ ... __ underscores) work correctly in njit/jit/no-jit cases.

Probably it is not a bug but unimplemented feature of yet experimental jitclass.

@gmarkall
Copy link
Member

gmarkall commented Sep 6, 2021

I believe this is implemented in #5877.
Similar requests for other methods: #1606, #5690.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants