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

Cannot overwrite the __str__ method in jitclass #8531

Closed
pietrodantuono opened this issue Oct 24, 2022 · 5 comments
Closed

Cannot overwrite the __str__ method in jitclass #8531

pietrodantuono opened this issue Oct 24, 2022 · 5 comments
Labels
no action required No action was needed to resolve.

Comments

@pietrodantuono
Copy link

pietrodantuono commented Oct 24, 2022

Hello,

I am not able to overwrite the string representation of a jitclass. I do not know if this is a wanted behaviour though.

from numba.experimental import jitclass
from numba import float64

spec = [("alpha", float64)]


@jitclass(spec)
class Foo:
    def __init__(self, alpha):
        self.alpha = alpha
    
    def __str__(self):
        return "Class Foo"


foo = Foo(1.)

print(str(foo))

Produces:

<numba.experimental.jitclass.boxing.Foo object at 0xS0M3wh3r3>

Expected:

Class Foo

It seems that the __str__() method is skipped entirely, in fact the following does not produce any ZeroDivisionError.

...
    def __str__(self):
        return 0 ** (-2)

Tested on:

Platform: Linux-5.15.0-52-generic-x86_64-with-glibc2.35
Python version: 3.9.7 (default, Sep 20 2022, 16:21:01) 
[GCC 11.2.0]

Numba version: 0.55.2
@stuartarchibald
Copy link
Contributor

Thanks for the report @pietrodantuono. I think this was implemented in 0.56:

numba/CHANGE_LOG

Lines 80 to 84 in f4b6fe0

Numba specific feature enhancements:
* The experimental ``jitclass`` feature gains support for a large number of
``builtin`` methods e.g. declaring ``__hash__`` or ``__getitem__`` for a
``jitclass`` type.

and it (jitclass __str__ method) seems to work for me locally on main. If you can, perhaps try updating to 0.56 and seeing if that now works as expected with the above?

@pietrodantuono
Copy link
Author

Thank you @stuartarchibald! After updating to 0.56.3, the __str__ method works!

My bad, I did not notice it from the Release notes

Numba specific feature enhancements:

The experimental jitclass feature gains support for a large number of builtin methods e.g. declaring __hash__ or __getitem__ for a jitclass type.

(https://numba.readthedocs.io/en/stable/release-notes.html#version-0-56-0-25-july-2022)

Next question: what about __repr__? Any possibility for it to be implemented in a future release?
It'd be really nice to have it for usage within Jupyter Notebook.

@stuartarchibald
Copy link
Contributor

Thank you @stuartarchibald! After updating to 0.56.3, the __str__ method works!

My bad, I did not notice it from the Release notes

No problem, glad it is working now.

Next question: what about __repr__? Any possibility for it to be implemented in a future release? It'd be really nice to have it for usage within Jupyter Notebook.

I suggest opening a feature request if it is important to you. I think it's technically possible to support it.

@stuartarchibald stuartarchibald added no action required No action was needed to resolve. and removed needtriage labels Oct 24, 2022
@pietrodantuono
Copy link
Author

Done!

Ref. #8533

@stuartarchibald
Copy link
Contributor

Done!

Ref. #8533

Great, many thanks. I'll close this issue as resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no action required No action was needed to resolve.
Projects
None yet
Development

No branches or pull requests

2 participants