Skip to content

Commit fa659c4

Browse files
committed
__repr__ refinements
- Remove keyword unpacking `**`. This is optional now that a dict can be passed to the constructor directly. - Remove literal properties (e.g. scatter.type). These are read-only and can't be specified in the constructor.
1 parent 6d06759 commit fa659c4

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

plotly/basedatatypes.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from . import offline as pyo
1313
from _plotly_utils.basevalidators import (
1414
CompoundValidator, CompoundArrayValidator, BaseDataValidator,
15-
BaseValidator
15+
BaseValidator, LiteralValidator
1616
)
1717
from . import animation
1818
from .callbacks import (Points, BoxSelector, LassoSelector,
@@ -2734,7 +2734,7 @@ def _build_repr_for_class(props, class_name, parent_path_str=None):
27342734
# complaint indent
27352735
body = ' ' + pprint_res[1:-1].replace('\n', '\n ')
27362736

2737-
repr_str = class_name + '(**{\n ' + body + '\n})'
2737+
repr_str = class_name + '({\n ' + body + '\n})'
27382738

27392739
return repr_str
27402740

@@ -2743,8 +2743,18 @@ def __repr__(self):
27432743
Customize object representation when displayed in the
27442744
terminal/notebook
27452745
"""
2746+
2747+
# Get all properties
2748+
props = self._props if self._props is not None else {}
2749+
2750+
# Remove literals (These can't be specified in the constructor)
2751+
props = {p: v for p, v in props.items()
2752+
if p in self._validators and
2753+
not isinstance(self._validators[p], LiteralValidator)}
2754+
2755+
# Build repr string
27462756
repr_str = BasePlotlyType._build_repr_for_class(
2747-
props=self._props if self._props is not None else {},
2757+
props=props,
27482758
class_name=self.__class__.__name__,
27492759
parent_path_str=self._parent_path_str)
27502760

plotly/tests/test_core/test_graph_objs/test_repr.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def test_trace_repr(self):
1313
'opacity': [e / N for e in range(N)]})
1414

1515
expected = """\
16-
Scatter(**{
16+
Scatter({
1717
'marker': {'color': 'green',
1818
'opacity': [0.0, 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08,
1919
0.09, 0.1, 0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17,
@@ -27,7 +27,6 @@ def test_trace_repr(self):
2727
0.81, 0.82, 0.83, 0.84, 0.85, 0.86, 0.87, 0.88, 0.89,
2828
0.9, 0.91, 0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98,
2929
0.99]},
30-
'type': 'scatter',
3130
'y': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
3231
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
3332
38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
@@ -45,10 +44,9 @@ def test_trace_repr_elided(self):
4544
'opacity': [e / N for e in range(N)]})
4645

4746
expected = """\
48-
Scatter(**{
47+
Scatter({
4948
'marker': {'color': 'green',
5049
'opacity': [0.0, 0.001, 0.002, ..., 0.997, 0.998, 0.999]},
51-
'type': 'scatter',
5250
'y': [0, 1, 2, ..., 997, 998, 999]
5351
})"""
5452
self.assertEqual(scatt.__repr__(), expected)

0 commit comments

Comments
 (0)