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

Override ipython repr methods. #1724

Merged
merged 2 commits into from Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/release.rst
Expand Up @@ -18,6 +18,12 @@ Release notes
Unreleased
----------

Enhancements
~~~~~~~~~~~~

* Override IPython ``_repr_*_`` methods to avoid expensive lookups against object stores.
By :user:`Deepak Cherian <dcherian>` :issue:`1716`.

.. _release_2.17.1:

2.17.1
Expand Down
41 changes: 41 additions & 0 deletions zarr/hierarchy.py
Expand Up @@ -515,6 +515,13 @@ def _delitem_nosync(self, item):
raise KeyError(item)

def __getattr__(self, item):
# https://github.com/jupyter/notebook/issues/2014
# Save a possibly expensive lookup (for e.g. against cloud stores)
# Note: The _ipython_display_ method is required to display the right info as a side-effect.
# It is simpler to pretend it doesn't exist.
if item in ["_ipython_canary_method_should_not_exist_", "_ipython_display_"]:
raise AttributeError

# allow access to group members via dot notation
try:
return self.__getitem__(item)
Expand Down Expand Up @@ -1331,6 +1338,40 @@ def move(self, source, dest):

self._write_op(self._move_nosync, source, dest)

# Override ipython repr methods, GH1716
# https://ipython.readthedocs.io/en/stable/config/integrating.html#custom-methods
# " If the methods don’t exist, the standard repr() is used. If a method exists and
# returns None, it is treated the same as if it does not exist."
def _repr_html_(self):
return None

def _repr_latex_(self):
return None

def _repr_mimebundle_(self, **kwargs):
return None

def _repr_svg_(self):
return None

def _repr_png_(self):
return None

def _repr_jpeg_(self):
return None

def _repr_markdown_(self):
return None

def _repr_javascript_(self):
return None

def _repr_pdf_(self):
return None

def _repr_json_(self):
return None


def _normalize_store_arg(store, *, storage_options=None, mode="r", zarr_version=None):
if zarr_version is None:
Expand Down
21 changes: 21 additions & 0 deletions zarr/tests/test_hierarchy.py
@@ -1,4 +1,5 @@
import atexit
import operator
import os
import sys
import pickle
Expand Down Expand Up @@ -87,6 +88,26 @@ def create_group(
)
return g

def test_ipython_repr_methods(self):
g = self.create_group()
for method in [
"html",
"json",
"javascript",
"markdown",
"svg",
"png",
"jpeg",
"latex",
"pdf",
"mimebundle",
]:
assert operator.methodcaller(f"_repr_{method}_")(g) is None
with pytest.raises(AttributeError):
g._ipython_display_()
with pytest.raises(AttributeError):
g._ipython_canary_method_should_not_exist_()

def test_group_init_1(self):
store, chunk_store = self.create_store()
g = self.create_group(store, chunk_store=chunk_store)
Expand Down