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

Defining __getattr__ breaks _repr_html_ #2014

Closed
mcleonard opened this issue Dec 25, 2016 · 7 comments
Closed

Defining __getattr__ breaks _repr_html_ #2014

mcleonard opened this issue Dec 25, 2016 · 7 comments

Comments

@mcleonard
Copy link

I created a class where I am storing rows of data with column headers. I want to display the contents in an HTML table in notebooks, so I'm using _repr_html_. I'd also like to access columns using the headers as attributes, like foo.column1. However, defining __getattr__ stops _repr_html_ from being called and the notebook falls back to __repr__. This shouldn't happen because _repr_html_ is defined in the class instance, so __getattr__ shouldn't be called. I checked if __getattr__ is being called and it appears that _ipython_canary_method_should_not_exist_ is being called and then it falls back to the normal __repr__.

Here's an example:
screen shot 2016-12-24 at 5 48 26 pm

screen shot 2016-12-24 at 5 48 40 pm

@mcleonard
Copy link
Author

Ah, nevermind. I needed to raise AttributeError in __getattr__.

@takluyver
Copy link
Member

Yep. To clarify, your __getattr__ implementation effectively pretends that the object has any attribute we check for (so hasattr(a, x) works for any x). If we detect that an object is doing that, we don't trust it when it claims to have methods like _repr_html_.

@minrk minrk added this to the No Action milestone Jan 2, 2017
@connerxyz
Copy link

Okay but what's the solution?

@connerxyz
Copy link

If it checks for permissive attribute handling to mitigate potentially not having a __repr_html__ implementation, why doesn't it check for the __repr_html__ implementation itself?

@zlatko-minev
Copy link

So what is the resolution? I have the same issue

@kechan
Copy link

kechan commented May 20, 2020

So what is the resolution? I have the same issue

In my def getattr(self, attr), I just did this:

if attr == '_ipython_canary_method_should_not_exist_':
  return

to suppress that message. This is a hack I don't know any unintended consequence. For my case, I will post back if i see any weird behavior downstream.

@takluyver
Copy link
Member

In general, your __getattr__ should either accept only names in some category, e.g.

if name in self.columns:
    return self.columns[name]
raise AttributeError(name)

Or at least have a broad refusal for e.g. anything that starts with an underscore:

if name.startswith('_'):
    raise AttributeError(name)

Special-casing the 'canary' method defeats the point of it. IPython deliberately doesn't trust objects which claim to have an attribute with any name, because it can cause all sorts of problems if we call special methods which aren't really defined and do something unexpected.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 26, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants