Skip to content

Commit

Permalink
Give BoundBlock, StreamValue and RawDataView more helpful repr()s
Browse files Browse the repository at this point in the history
It seems quite likely that the lack of nice representations for these in the shell / debugger is what stopped people from realising that they could index StreamValue as an array, and led them towards stream_data instead ("ooh, this looks like an array of data... I know how to deal with this")
  • Loading branch information
gasman committed Dec 8, 2020
1 parent 37d4e84 commit 23849d8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
3 changes: 3 additions & 0 deletions wagtail/core/blocks/base.py
Expand Up @@ -472,6 +472,9 @@ def __str__(self):
"""Render the value according to the block's native rendering"""
return self.block.render(self.value)

def __repr__(self):
return "<block %s: %r>" % (self.block.name or type(self.block).__name__, self.value)


class DeclarativeSubBlocksMetaclass(BaseBlock):
"""
Expand Down
5 changes: 4 additions & 1 deletion wagtail/core/blocks/stream_block.py
Expand Up @@ -457,6 +457,9 @@ def insert(self, i, item):
self.stream_value._raw_data.insert(i, item)
self.stream_value._bound_blocks.insert(i, None)

def __repr__(self):
return repr(list(self))

def __init__(self, stream_block, stream_data, is_lazy=False, raw_text=None):
"""
Construct a StreamValue linked to the given StreamBlock,
Expand Down Expand Up @@ -606,7 +609,7 @@ def __len__(self):
return len(self._bound_blocks)

def __repr__(self):
return repr(list(self))
return "<%s %r>" % (type(self).__name__, list(self))

def render_as_block(self, context=None):
return self.stream_block.render(self, context=context)
Expand Down

0 comments on commit 23849d8

Please sign in to comment.