Repository: spyder-ide/spyder

Issue:
Update plots plugin to python 3
Take into account the following for the migration:

Update super() use
Remove the py3compat module
Update any code (and remove if necessary) that uses if PY2 etc...
Check division operator
Remove future imports
Check tests
Check for Py2 of Py3 codes


Discussion:

Resolving patch:
diff --git a/spyder/plugins/plots/plugin.py b/spyder/plugins/plots/plugin.py
--- a/spyder/plugins/plots/plugin.py
+++ b/spyder/plugins/plots/plugin.py
@@ -32,7 +32,7 @@ class Plots(SpyderPluginWidget):
     DISABLE_ACTIONS_WHEN_HIDDEN = False
 
     def __init__(self, parent):
-        SpyderPluginWidget.__init__(self, parent)
+        super().__init__(parent)
 
         # Widgets
         self.stack = QStackedWidget(self)
diff --git a/spyder/plugins/plots/widgets/figurebrowser.py b/spyder/plugins/plots/widgets/figurebrowser.py
--- a/spyder/plugins/plots/widgets/figurebrowser.py
+++ b/spyder/plugins/plots/widgets/figurebrowser.py
@@ -11,7 +11,6 @@
 """
 
 # ---- Standard library imports
-from __future__ import division
 import datetime
 import os.path as osp
 import sys
@@ -30,7 +29,6 @@
 # ---- Local library imports
 from spyder.config.base import _
 from spyder.config.manager import CONF
-from spyder.py3compat import is_unicode, to_text_string
 from spyder.utils import icon_manager as ima
 from spyder.utils.qthelpers import (
     add_actions, add_shortcut_to_tooltip, create_action, create_toolbutton,
@@ -46,7 +44,7 @@ def save_figure_tofile(fig, fmt, fname):
         qimg = svg_to_image(fig)
         qimg.save(fname)
     else:
-        if fmt == 'image/svg+xml' and is_unicode(fig):
+        if fmt == 'image/svg+xml' and isinstance(fig, str):
             fig = fig.encode('utf-8')
 
         with open(fname, 'wb') as f:
@@ -78,7 +76,7 @@ class FigureBrowser(QWidget):
 
     def __init__(self, parent=None, options_button=None, plugin_actions=[],
                  background_color=None):
-        super(FigureBrowser, self).__init__(parent)
+        super().__init__(parent)
 
         self.shellwidget = None
         self.is_visible = True
@@ -369,7 +367,7 @@ def get_shortcut_data(self):
 
     def option_changed(self, option, value):
         """Handle when the value of an option has changed"""
-        setattr(self, to_text_string(option), value)
+        setattr(self, str(option), value)
         self.shellwidget.set_namespace_view_settings()
         if self.setup_in_progress is False:
             self.sig_option_changed.emit(option, value)
@@ -468,7 +466,7 @@ class FigureViewer(QScrollArea):
     sig_zoom_changed = Signal(int)
 
     def __init__(self, parent=None, background_color=None):
-        super(FigureViewer, self).__init__(parent)
+        super().__init__(parent)
         self.setAlignment(Qt.AlignCenter)
         self.viewport().setObjectName("figviewport")
         self.viewport().setStyleSheet(
@@ -650,7 +648,7 @@ class ThumbnailScrollBar(QFrame):
     _min_scrollbar_width = 100
 
     def __init__(self, figure_viewer, parent=None, background_color=None):
-        super(ThumbnailScrollBar, self).__init__(parent)
+        super().__init__(parent)
         self._thumbnails = []
 
         self.background_color = background_color
@@ -974,7 +972,7 @@ class FigureThumbnail(QWidget):
     sig_save_figure = Signal(object, str)
 
     def __init__(self, parent=None, background_color=None):
-        super(FigureThumbnail, self).__init__(parent)
+        super().__init__(parent)
         self.canvas = FigureCanvas(self, background_color=background_color)
         self.canvas.installEventFilter(self)
         self.canvas.sig_clear_fig_requested.connect(self.emit_remove_figure)
@@ -1054,7 +1052,7 @@ class FigureCanvas(QFrame):
     sig_save_fig_requested = Signal()
 
     def __init__(self, parent=None, background_color=None):
-        super(FigureCanvas, self).__init__(parent)
+        super().__init__(parent)
         self.setLineWidth(2)
         self.setMidLineWidth(1)
         self.setObjectName("figcanvas")


Test patch:
diff --git a/spyder/plugins/plots/widgets/tests/test_plots_widgets.py b/spyder/plugins/plots/widgets/tests/test_plots_widgets.py
--- a/spyder/plugins/plots/widgets/tests/test_plots_widgets.py
+++ b/spyder/plugins/plots/widgets/tests/test_plots_widgets.py
@@ -11,12 +11,8 @@
 """
 
 # Standard library imports
-from __future__ import division
 import os.path as osp
-try:
-    from unittest.mock import Mock
-except ImportError:
-    from mock import Mock  # Python 2
+from unittest.mock import Mock
 
 # Third party imports
 import pytest
@@ -30,7 +26,6 @@
 # Local imports
 from spyder.plugins.plots.widgets.figurebrowser import (FigureBrowser,
                                                         FigureThumbnail)
-from spyder.py3compat import to_text_string
 
 
 # =============================================================================
@@ -76,7 +71,7 @@ def add_figures_to_browser(figbrowser, nfig, tmpdir, fmt='image/png'):
     fext = '.svg' if fmt == 'image/svg+xml' else '.png'
     figs = []
     for i in range(nfig):
-        figname = osp.join(to_text_string(tmpdir), 'mplfig' + str(i) + fext)
+        figname = osp.join(str(tmpdir), 'mplfig' + str(i) + fext)
         figs.append(create_figure(figname))
         figbrowser._handle_new_figure(figs[-1], fmt)
 
@@ -110,7 +105,7 @@ def test_handle_new_figures(figbrowser, tmpdir, fmt, fext):
     assert figbrowser.figviewer.figcanvas.fig is None
 
     for i in range(3):
-        figname = osp.join(to_text_string(tmpdir), 'mplfig' + str(i) + fext)
+        figname = osp.join(str(tmpdir), 'mplfig' + str(i) + fext)
         fig = create_figure(figname)
         figbrowser._handle_new_figure(fig, fmt)
         assert len(figbrowser.thumbnails_sb._thumbnails) == i + 1
@@ -132,7 +127,7 @@ def test_save_figure_to_file(figbrowser, tmpdir, mocker, fmt, fext):
     expected_qpix.loadFromData(fig, fmt.upper())
 
     # Save the figure to disk with the figure browser.
-    saved_figname = osp.join(to_text_string(tmpdir), 'spyfig' + fext)
+    saved_figname = osp.join(str(tmpdir), 'spyfig' + fext)
     mocker.patch('spyder.plugins.plots.widgets.figurebrowser.getsavefilename',
                  return_value=(saved_figname, fext))
 
@@ -162,7 +157,7 @@ def test_save_all_figures(figbrowser, tmpdir, mocker, fmt):
     # Save all figures.
     mocker.patch(
         'spyder.plugins.plots.widgets.figurebrowser.getexistingdirectory',
-        return_value=to_text_string(tmpdir.mkdir('all_saved_figures')))
+        return_value=str(tmpdir.mkdir('all_saved_figures')))
     fignames = figbrowser.save_all_figures()
     assert len(fignames) == len(figs)
     for fig, figname in zip(figs, fignames):
@@ -290,7 +285,7 @@ def test_scroll_down_to_newest_plot(figbrowser, tmpdir, qtbot):
     nfig = 8
     for i in range(8):
         newfig = create_figure(
-            osp.join(to_text_string(tmpdir), 'new_mplfig{}.png'.format(i)))
+            osp.join(str(tmpdir), 'new_mplfig{}.png'.format(i)))
         figbrowser._handle_new_figure(newfig, 'image/png')
         qtbot.wait(500)
 
@@ -329,7 +324,7 @@ def test_save_thumbnails(figbrowser, tmpdir, qtbot, mocker, fmt):
     fext = '.svg' if fmt == 'image/svg+xml' else '.png'
 
     # Select and save the second thumbnail of the scrollbar.
-    figname = osp.join(to_text_string(tmpdir), 'figname' + fext)
+    figname = osp.join(str(tmpdir), 'figname' + fext)
     mocker.patch('spyder.plugins.plots.widgets.figurebrowser.getsavefilename',
                  return_value=(figname, fext))
     figbrowser.thumbnails_sb.set_current_index(1)