Skip to content

Commit

Permalink
Merge pull request #179 from jitseniesen/python2
Browse files Browse the repository at this point in the history
PR: Remove Python 2 compatibility code
  • Loading branch information
jitseniesen committed Jul 20, 2022
2 parents 6bfbc0a + 79322ed commit d3d28d5
Show file tree
Hide file tree
Showing 11 changed files with 17 additions and 86 deletions.
9 changes: 2 additions & 7 deletions spyder_unittest/backend/runnerbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,9 @@
import tempfile

# Third party imports
from importlib.util import find_spec as find_spec_or_loader
from qtpy.QtCore import (QObject, QProcess, QProcessEnvironment, QTextCodec,
Signal)
from spyder.py3compat import to_text_string

try:
from importlib.util import find_spec as find_spec_or_loader
except ImportError: # Python 2
from pkgutil import find_loader as find_spec_or_loader


# if generating coverage report, use this name for the TestResult
Expand Down Expand Up @@ -244,7 +239,7 @@ def read_all_process_output(self):
"""Read and return all output from `self.process` as unicode."""
qbytearray = self.process.readAllStandardOutput()
locale_codec = QTextCodec.codecForLocale()
return to_text_string(locale_codec.toUnicode(qbytearray.data()))
return locale_codec.toUnicode(qbytearray.data())

def stop_if_running(self):
"""Stop testing process if it is running."""
Expand Down
6 changes: 1 addition & 5 deletions spyder_unittest/backend/tests/test_pytestrunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import os
import os.path as osp
import sys
from unittest.mock import Mock

# Third party imports
import pytest
Expand All @@ -20,11 +21,6 @@
COV_TEST_NAME)
from spyder_unittest.widgets.configdialog import Config

try:
from unittest.mock import Mock
except ImportError:
from mock import Mock # Python 2


def test_pytestrunner_is_installed():
assert PyTestRunner(None).is_installed()
Expand Down
6 changes: 1 addition & 5 deletions spyder_unittest/backend/tests/test_pytestworker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

# Standard library imports
import os
from unittest.mock import call, create_autospec, MagicMock, Mock

# Third party imports
import pytest
Expand All @@ -15,11 +16,6 @@
from spyder_unittest.backend.pytestworker import SpyderPlugin, main
from spyder_unittest.backend.zmqstream import ZmqStreamWriter

try:
from unittest.mock import call, create_autospec, MagicMock, Mock
except ImportError:
from mock import call, create_autospec, MagicMock, Mock # Python 2


class EmptyClass:
pass
Expand Down
6 changes: 1 addition & 5 deletions spyder_unittest/backend/tests/test_runnerbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

# Standard library imports
import os
from unittest.mock import Mock

# Third party imports
import pytest
Expand All @@ -15,11 +16,6 @@
from spyder_unittest.backend.runnerbase import RunnerBase
from spyder_unittest.widgets.configdialog import Config

try:
from unittest.mock import Mock
except ImportError:
from mock import Mock # Python 2


def test_runnerbase_with_nonexisting_module():
class FooRunner(RunnerBase):
Expand Down
4 changes: 1 addition & 3 deletions spyder_unittest/backend/zmqstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
process can then use the stream to send its result to the reader.
"""

from __future__ import print_function

# Standard library imports
import sys

Expand Down Expand Up @@ -68,7 +66,7 @@ class ZmqStreamReader(QObject):

def __init__(self):
"""Constructor; also constructs ZMQ stream."""
super(QObject, self).__init__()
super().__init__()
self.context = zmq.Context()
self.socket = self.context.socket(zmq.PAIR)
self.port = self.socket.bind_to_random_port('tcp://*')
Expand Down
6 changes: 1 addition & 5 deletions spyder_unittest/tests/test_unittestplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@
# Third party imports
import pytest
from spyder.plugins.projects.api import EmptyProject
from unittest.mock import MagicMock

# Local imports
from spyder_unittest.unittestplugin import UnitTestPlugin
from spyder_unittest.widgets.configdialog import Config

try:
from unittest.mock import MagicMock
except ImportError:
from mock import MagicMock # Python 2


class PluginForTesting(UnitTestPlugin):
CONF_FILE = False
Expand Down
26 changes: 1 addition & 25 deletions spyder_unittest/unittestplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""Unit testing Plugin."""

# Standard library imports
from os import getcwd
import os.path as osp

# Third party imports
Expand All @@ -14,7 +15,6 @@
from spyder.config.base import get_translation
from spyder.config.gui import is_dark_interface
from spyder.plugins.mainmenu.api import ApplicationMenus
from spyder.py3compat import PY2, getcwd

# Local imports
from spyder_unittest.widgets.configdialog import Config
Expand Down Expand Up @@ -98,30 +98,6 @@ def on_initialize(self):
register_shortcut=True)
# TODO: shortcut="Shift+Alt+F11"

# --- Optional SpyderDockablePlugin methods -------------------------------

@staticmethod
def check_compatibility():
"""
Check compatibility of the plugin.
This checks that the plugin is not run under Python 2.
Returns
-------
(bool, str)
The first value tells Spyder if the plugin has passed the
compatibility test defined in this method. The second value
is a message that must explain users why the plugin was
found to be incompatible (e.g. 'This plugin does not work
with PyQt4'). It will be shown at startup in a QMessageBox.
"""
if PY2:
msg = _('The unittest plugin does not work with Python 2.')
return (False, msg)
else:
return (True, '')

# ----- Set up interactions with other plugins ----------------------------

@on_plugin_available(plugin=Plugins.Editor)
Expand Down
11 changes: 4 additions & 7 deletions spyder_unittest/widgets/configdialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

# Standard library imports
from collections import namedtuple
from os import getcwd
from pkgutil import find_loader as find_spec_or_loader
import os.path as osp

# Third party imports
Expand All @@ -20,13 +22,8 @@
QHBoxLayout, QLabel, QLineEdit, QPushButton,
QVBoxLayout, QCheckBox)
from spyder.config.base import get_translation
from spyder.py3compat import getcwd, to_text_string
from spyder.utils import icon_manager as ima

try:
from importlib.util import find_spec as find_spec_or_loader
except ImportError: # Python 2
from pkgutil import find_loader as find_spec_or_loader

try:
_ = get_translation('spyder_unittest')
Expand Down Expand Up @@ -62,7 +59,7 @@ def __init__(self, frameworks, config, parent=None):
Initial configuration
parent : QWidget
"""
super(ConfigDialog, self).__init__(parent)
super().__init__(parent)
self.setWindowTitle(_('Configure tests'))
layout = QVBoxLayout(self)

Expand Down Expand Up @@ -145,7 +142,7 @@ def framework_changed(self, index):

def select_directory(self):
"""Display dialog for user to select working directory."""
basedir = to_text_string(self.wdir_lineedit.text())
basedir = self.wdir_lineedit.text()
if not osp.isdir(basedir):
basedir = getcwd()
title = _("Select directory")
Expand Down
6 changes: 1 addition & 5 deletions spyder_unittest/widgets/tests/test_datatree.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,14 @@
# Third party imports
from qtpy.QtCore import QModelIndex, QPoint, Qt
from qtpy.QtGui import QContextMenuEvent
from unittest.mock import Mock
import pytest

# Local imports
from spyder_unittest.backend.runnerbase import Category, TestResult
from spyder_unittest.widgets.datatree import (COLORS, COLORS_DARK,
TestDataModel, TestDataView)

try:
from unittest.mock import Mock
except ImportError:
from mock import Mock # Python 2


@pytest.fixture
def view_and_model(qtbot):
Expand Down
6 changes: 1 addition & 5 deletions spyder_unittest/widgets/tests/test_unittestgui.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# Standard library imports
import os
import sys
from unittest.mock import Mock

# Third party imports
from qtpy.QtCore import Qt, QProcess
Expand All @@ -19,11 +20,6 @@
from spyder_unittest.widgets.configdialog import Config
from spyder_unittest.widgets.unittestgui import UnitTestWidget

try:
from unittest.mock import Mock
except ImportError:
from mock import Mock # Python 2


@pytest.fixture
def widget(qtbot):
Expand Down
17 changes: 3 additions & 14 deletions spyder_unittest/widgets/unittestgui.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
# (see LICENSE.txt for details)
"""Unit Testing widget."""

from __future__ import with_statement

# Standard library imports
import copy
import os.path as osp
Expand All @@ -19,34 +17,25 @@
from spyder.config.base import get_conf_path, get_translation
from spyder.utils import icon_manager as ima
from spyder.plugins.variableexplorer.widgets.texteditor import TextEditor
from spyder.py3compat import PY3

# Local imports
from spyder_unittest.backend.frameworkregistry import FrameworkRegistry
from spyder_unittest.backend.noserunner import NoseRunner
from spyder_unittest.backend.pytestrunner import PyTestRunner
from spyder_unittest.backend.runnerbase import Category, TestResult
from spyder_unittest.backend.unittestrunner import UnittestRunner
from spyder_unittest.widgets.configdialog import Config, ask_for_config
from spyder_unittest.widgets.datatree import TestDataModel, TestDataView

# This import uses Python 3 syntax, so importing it under Python 2 throws
# a SyntaxError which means that the plugin's check_compatibility method
# will never run.
if PY3:
from spyder_unittest.backend.pytestrunner import PyTestRunner

# This is needed for testing this module as a stand alone script
try:
_ = get_translation('spyder_unittest')
except KeyError:
import gettext
_ = gettext.gettext

# Supported testing framework
if PY3:
FRAMEWORKS = {NoseRunner, PyTestRunner, UnittestRunner}
else:
FRAMEWORKS = {NoseRunner, UnittestRunner}
# Supported testing frameworks
FRAMEWORKS = {NoseRunner, PyTestRunner, UnittestRunner}


class UnitTestWidgetActions:
Expand Down

0 comments on commit d3d28d5

Please sign in to comment.