Skip to content

Commit

Permalink
Override ipython repr methods. (#1724)
Browse files Browse the repository at this point in the history
Closes #1716

This avoids expensive lookups against object stores.
  • Loading branch information
dcherian authored Mar 27, 2024
1 parent 2534413 commit bad8dd0
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/release.rst
Original file line number Diff line number Diff line change
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`.

Maintenance
~~~~~~~~~~~

Expand Down
41 changes: 41 additions & 0 deletions zarr/hierarchy.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit bad8dd0

Please sign in to comment.