Repository: altair-viz/altair
Pull request #259 (2016-11-02 18:34:44)

MAINT: clean up dir() specialization of top-level objects
Replaces part of #163


Patch:
diff --git a/altair/api.py b/altair/api.py
index 2657d601..ec7f5602 100644
--- a/altair/api.py
+++ b/altair/api.py
@@ -385,19 +385,7 @@ class Chart(schema.ExtendedUnitSpec, TopLevelMixin):
         self.data = data
 
     def __dir__(self):
-        base = super(Chart, self).__dir__()
-        methods = [
-            'to_altair', 'display',
-            'configure', 'configure_axis', 'configure_cell',
-            'configure_legend', 'configure_mark', 'configure_scale',
-            'configure_facet_axis', 'configure_facet_cell',
-            'configure_facet_grid', 'configure_facet_scale',
-            'transform_data',
-            'mark_area', 'mark_bar', 'mark_line', 'mark_point', 'mark_rule',
-            'mark_text', 'mark_tick', 'mark_circle', 'mark_square',
-            'encode',
-        ]
-        return base+methods
+        return [m for m in dir(self.__class__) if m not in dir(T.HasTraits)]
 
     @use_signature(MarkConfig)
     def mark_area(self, *args, **kwargs):
@@ -540,17 +528,7 @@ class LayeredChart(schema.LayerSpec, TopLevelMixin):
         self.data = data
 
     def __dir__(self):
-        base = super(Chart, self).__dir__()
-        methods = [
-            'to_dict', 'from_dict', 'to_altair', 'display',
-            'configure', 'configure_axis', 'configure_cell',
-            'configure_legend', 'configure_mark', 'configure_scale',
-            'configure_facet_axis', 'configure_facet_cell',
-            'configure_facet_grid', 'configure_facet_scale',
-            'transform_data',
-            'set_layers',
-        ]
-        return base+methods
+        return [m for m in dir(self.__class__) if m not in dir(T.HasTraits)]
 
     def set_layers(self, *layers):
         self.layers = list(layers)
@@ -604,17 +582,7 @@ class FacetedChart(schema.FacetSpec, TopLevelMixin):
         self.data = data
 
     def __dir__(self):
-        base = super(Chart, self).__dir__()
-        methods = [
-            'to_dict', 'from_dict', 'to_altair', 'display',
-            'configure', 'configure_axis', 'configure_cell',
-            'configure_legend', 'configure_mark', 'configure_scale',
-            'configure_facet_axis', 'configure_facet_cell',
-            'configure_facet_grid', 'configure_facet_scale',
-            'transform_data',
-            'set_facet',
-        ]
-        return base + methods
+        return [m for m in dir(self.__class__) if m not in dir(T.HasTraits)]
 
     @use_signature(Facet)
     def set_facet(self, *args, **kwargs):


Test patch:
diff --git a/altair/tests/test_api.py b/altair/tests/test_api.py
index eb0285f4..76376b04 100644
--- a/altair/tests/test_api.py
+++ b/altair/tests/test_api.py
@@ -514,3 +514,29 @@ def test_df_filter_multiple():
 
     # make sure outputs match
     assert chart1.to_dict() == chart2.to_dict()
+
+
+def test_chart_dir():
+    # test that the dir() method of top-level objects (and by extension the
+    # IPython tab-completion) exposes what we want to have exposed.
+    chart = dir(Chart())
+    layerchart = dir(LayeredChart())
+    facetchart = dir(FacetedChart())
+
+    for L in [chart, layerchart, facetchart]:
+        # T.HasTraits methods should not appear
+        assert 'has_own_traits' not in L
+        # TopLevel methods should appear
+        assert 'to_html' in L
+        # Common traits should appear
+        assert 'config' in L
+
+    # Specialized chart methods should appear
+    assert 'mark_point' in chart
+    assert 'set_layers' in layerchart
+    assert 'set_facet' in facetchart
+
+    # traits should appear
+    assert 'mark' in chart
+    assert 'layers' in layerchart
+    assert 'facet' in facetchart