From c1eb764b0904ecb38cb5a85a0e52df881f820de3 Mon Sep 17 00:00:00 2001 From: Alexander Zaft Date: Tue, 8 Nov 2022 07:53:34 +0100 Subject: [PATCH] fixed pylint warnings Change-Id: Ibb3da77e9a53b7293a280659defc029416e30e3b --- .pylintrc | 2 +- secop/gui/cfg_editor/config_file.py | 10 ++++++---- secop/gui/mainwindow.py | 5 +++-- secop/gui/nodectrl.py | 4 ++-- secop/gui/paramview.py | 2 +- secop/gui/valuewidgets.py | 26 +++++++++++++------------- secop/io.py | 4 ++-- secop/lib/__init__.py | 4 ++-- secop/lib/pidfile.py | 4 ++-- secop/lib/sequence.py | 2 +- secop/modules.py | 1 + secop/params.py | 1 + secop/parse.py | 2 ++ secop/persistent.py | 4 ++-- secop/proxy.py | 1 + secop/server.py | 2 +- secop/version.py | 12 ++++++------ secop_demo/cryo.py | 2 +- secop_psi/ppmssim.py | 2 +- secop_psi/softcal.py | 2 +- test/conftest.py | 4 ++-- test/test_handler.py | 6 +++--- test/test_lib_enum.py | 2 +- test/test_logging.py | 1 + test/test_modules.py | 2 +- test/test_msg.py | 2 +- 26 files changed, 59 insertions(+), 50 deletions(-) diff --git a/.pylintrc b/.pylintrc index d9c9efc..30d8a3a 100644 --- a/.pylintrc +++ b/.pylintrc @@ -52,7 +52,7 @@ disable=missing-docstring ,unneeded-not ,unidiomatic-typecheck ,undefined-loop-variable - + ,consider-using-f-string [REPORTS] # Set the output format. Available formats are text, parseable, colorized, msvs diff --git a/secop/gui/cfg_editor/config_file.py b/secop/gui/cfg_editor/config_file.py index 3418e12..1a2d43f 100644 --- a/secop/gui/cfg_editor/config_file.py +++ b/secop/gui/cfg_editor/config_file.py @@ -76,7 +76,7 @@ def write_config(file_name, tree_widget): temp_itm_lines[key] = dict_value itm_lines.clear() itm_lines.update(temp_itm_lines) - with open(file_name, 'w') as configfile: + with open(file_name, 'w', encoding='utf-8') as configfile: configfile.write('\n'.join(itm_lines.values())) @@ -88,7 +88,8 @@ def read_config(file_path): node.addChild(ifs) node.addChild(mods) config = configparser.ConfigParser() - config.read_file(open(file_path)) + with open(file_path, encoding='utf-8') as config_file: + config.read_file(config_file) for section in config.sections(): kind = section.split(' ', 1)[0] @@ -155,8 +156,8 @@ def get_value(config, section, option): def get_comments(node, ifs, mods, file_path): - configfile = open(file_path, 'r') - all_lines = configfile.readlines() + with open(file_path, 'r', encoding='utf-8') as configfile: + all_lines = configfile.readlines() current_comment = None all_ifs = get_all_children_with_names(ifs) all_mods = get_all_children_with_names(mods) @@ -192,6 +193,7 @@ def insert_comment(index, line, all_lines, current_comment, node, all_ifs, all_m all_ifs, all_mods) +# pylint: disable=inconsistent-return-statements def insert_section_comment(line, current_comment, node, all_ifs, all_mods): try: if line.startswith('[%s' % NODE): diff --git a/secop/gui/mainwindow.py b/secop/gui/mainwindow.py index d478534..f2e3e3b 100644 --- a/secop/gui/mainwindow.py +++ b/secop/gui/mainwindow.py @@ -91,6 +91,7 @@ class QSECNode(QObject): def syncCommunicate(self, action, ident='', data=None): reply = self.conn.request(action, ident, data) + # pylint: disable=not-an-iterable return secop.client.encode_msg_frame(*reply).decode('utf-8') def decode_message(self, msg): @@ -106,8 +107,8 @@ class QSECNode(QObject): def nodeStateChange(self, online, state): self.stateChange.emit(self.nodename, online, state) - def unhandledMessage(self, *msg): - self.unhandledMsg.emit('%s %s %r' % msg) + def unhandledMessage(self, action, specifier, data): + self.unhandledMsg.emit('%s %s %r' % (action, specifier, data)) class MainWindow(QMainWindow): diff --git a/secop/gui/nodectrl.py b/secop/gui/nodectrl.py index af6d3da..0b24ae0 100644 --- a/secop/gui/nodectrl.py +++ b/secop/gui/nodectrl.py @@ -39,7 +39,7 @@ from secop.gui.util import Value, loadUi class NodeCtrl(QWidget): def __init__(self, node, parent=None): - super(NodeCtrl, self).__init__(parent) + super().__init__(parent) loadUi(self, 'nodectrl.ui') self._node = node @@ -189,7 +189,7 @@ class NodeCtrl(QWidget): class ReadableWidget(QWidget): def __init__(self, node, module, parent=None): - super(ReadableWidget, self).__init__(parent) + super().__init__(parent) self._node = node self._module = module diff --git a/secop/gui/paramview.py b/secop/gui/paramview.py index afa3356..a931cbe 100644 --- a/secop/gui/paramview.py +++ b/secop/gui/paramview.py @@ -29,7 +29,7 @@ from secop.gui.util import loadUi class ParameterView(QWidget): def __init__(self, node, module, parameter, parent=None): - super(ParameterView, self).__init__(parent) + super().__init__(parent) loadUi(self, 'paramview.ui') self._node = node self._module = module diff --git a/secop/gui/valuewidgets.py b/secop/gui/valuewidgets.py index 856e6bb..cd16f7d 100644 --- a/secop/gui/valuewidgets.py +++ b/secop/gui/valuewidgets.py @@ -36,7 +36,7 @@ from secop.gui.util import loadUi class StringWidget(QLineEdit): def __init__(self, datatype, readonly=False, parent=None): - super(StringWidget, self).__init__(parent) + super().__init__(parent) self.datatype = datatype if readonly: self.setEnabled(False) @@ -51,7 +51,7 @@ class StringWidget(QLineEdit): class TextWidget(QTextEdit): def __init__(self, datatype, readonly=False, parent=None): - super(TextWidget, self).__init__(parent) + super().__init__(parent) self.datatype = datatype if readonly: self.setEnabled(False) @@ -72,7 +72,7 @@ class BlobWidget(StringWidget): # or derive from widget and switch between combobox and radiobuttons? class EnumWidget(QComboBox): def __init__(self, datatype, readonly=False, parent=None): - super(EnumWidget, self).__init__(parent) + super().__init__(parent) self.datatype = datatype self._map = {} @@ -92,7 +92,7 @@ class EnumWidget(QComboBox): class BoolWidget(QCheckBox): def __init__(self, datatype, readonly=False, parent=None): - super(BoolWidget, self).__init__(parent) + super().__init__(parent) self.datatype = datatype if readonly: self.setEnabled(False) @@ -106,7 +106,7 @@ class BoolWidget(QCheckBox): class IntWidget(QSpinBox): def __init__(self, datatype, readonly=False, parent=None): - super(IntWidget, self).__init__(parent) + super().__init__(parent) self.datatype = datatype if readonly: self.setEnabled(False) @@ -122,7 +122,7 @@ class IntWidget(QSpinBox): class FloatWidget(QDoubleSpinBox): def __init__(self, datatype, readonly=False, parent=None): - super(FloatWidget, self).__init__(parent) + super().__init__(parent) self.datatype = datatype if readonly: self.setEnabled(False) @@ -139,7 +139,7 @@ class FloatWidget(QDoubleSpinBox): class TupleWidget(QFrame): def __init__(self, datatype, readonly=False, parent=None): - super(TupleWidget, self).__init__(parent) + super().__init__(parent) self.datatypes = datatype.members @@ -164,7 +164,7 @@ class TupleWidget(QFrame): class StructWidget(QGroupBox): def __init__(self, datatype, readonly=False, parent=None): - super(StructWidget, self).__init__(parent) + super().__init__(parent) self.layout = QGridLayout() self.subwidgets = {} @@ -197,7 +197,7 @@ class StructWidget(QGroupBox): class ArrayWidget(QGroupBox): def __init__(self, datatype, readonly=False, parent=None): - super(ArrayWidget, self).__init__(parent) + super().__init__(parent) self.datatype = datatype.members self.layout = QVBoxLayout() @@ -234,7 +234,7 @@ def get_widget(datatype, readonly=False, parent=None): class msg(QDialog): def __init__(self, stuff, parent=None): - super(msg, self).__init__(parent) + super().__init__(parent) loadUi(self, 'cmddialog.ui') print(dir(self)) self.setWindowTitle('Please enter the arguments for calling command "blubb()"') @@ -253,12 +253,12 @@ class msg(QDialog): def accept(self): print('accepted') - super(msg, self).accept() + super().accept() def reject(self): print('rejected') - super(msg, self).reject() + super().reject() def done(self, how): print('done(%r)' % how) - return super(msg, self).done(how) + return super().done(how) diff --git a/secop/io.py b/secop/io.py index 1552049..9fee063 100644 --- a/secop/io.py +++ b/secop/io.py @@ -274,7 +274,7 @@ class StringIO(IOBase): self._conn.send(cmd + self._eol_write) self.comLog('> %s', cmd.decode(self.encoding)) reply = self._conn.readline(self.timeout) - except ConnectionClosed as e: + except ConnectionClosed: self.closeConnection() raise CommunicationFailedError('disconnected') from None reply = reply.decode(self.encoding) @@ -372,7 +372,7 @@ class BytesIO(IOBase): self._conn.send(request) self.comLog('> %s', hexify(request)) reply = self._conn.readbytes(replylen, self.timeout) - except ConnectionClosed as e: + except ConnectionClosed: self.closeConnection() raise CommunicationFailedError('disconnected') from None self.comLog('< %s', hexify(reply)) diff --git a/secop/lib/__init__.py b/secop/lib/__init__.py index 34e6901..aae453c 100644 --- a/secop/lib/__init__.py +++ b/secop/lib/__init__.py @@ -131,14 +131,14 @@ class GeneralConfig: def get(self, key, default=None): """access for keys not known to exist""" try: - return self.__getitem__(key) + return self[key] except KeyError: return default def getint(self, key, default=None): """access and convert to int""" try: - return int(self.__getitem__(key)) + return int(self[key]) except KeyError: return default diff --git a/secop/lib/pidfile.py b/secop/lib/pidfile.py index 76382d1..ccbb977 100644 --- a/secop/lib/pidfile.py +++ b/secop/lib/pidfile.py @@ -32,7 +32,7 @@ def read_pidfile(pidfile): or None upon errors (file not existing)""" try: - with open(pidfile, 'r') as f: + with open(pidfile, 'r', encoding='utf-8') as f: return int(f.read()) except (OSError, IOError): return None @@ -45,7 +45,7 @@ def remove_pidfile(pidfile): def write_pidfile(pidfile, pid): """write the given pid to the given pidfile""" - with open(pidfile, 'w') as f: + with open(pidfile, 'w', encoding='utf-8') as f: f.write('%d\n' % pid) atexit.register(remove_pidfile, pidfile) diff --git a/secop/lib/sequence.py b/secop/lib/sequence.py index 5d416ee..68e79ad 100644 --- a/secop/lib/sequence.py +++ b/secop/lib/sequence.py @@ -124,7 +124,7 @@ class SequencerMixin: def seq_is_alive(self): """Can be called to check if a sequence is currently running.""" - return self._seq_thread and self._seq_thread.isAlive() + return self._seq_thread and self._seq_thread.is_alive() def read_status(self): if self.seq_is_alive(): diff --git a/secop/modules.py b/secop/modules.py index 77e8bbc..363daa8 100644 --- a/secop/modules.py +++ b/secop/modules.py @@ -319,6 +319,7 @@ class Module(HasAccessibles): # 2) check and apply properties specified in cfgdict as # ' = ' + # pylint: disable=consider-using-dict-items for key in self.propertyDict: value = cfgdict.pop(key, None) if value is None: diff --git a/secop/params.py b/secop/params.py index 6ab6712..6a0bf5d 100644 --- a/secop/params.py +++ b/secop/params.py @@ -455,6 +455,7 @@ class Command(Accessible): - when the argument type is StructOf, the function is called with keyworded arguments - the validity of the argument/s is/are checked """ + # pylint: disable=unnecessary-dunder-call func = self.__get__(module_obj) if self.argument: # validate diff --git a/secop/parse.py b/secop/parse.py index 4180471..fb31d2b 100644 --- a/secop/parse.py +++ b/secop/parse.py @@ -64,6 +64,8 @@ class Parser: if number is None: return None, text try: + # TODO: check allthough length is unset in it. 1, number is None, never reaching the try + # pylint: disable=used-before-assignment return int(text[:length]), text[length:] except ValueError: return number, text[length:] diff --git a/secop/persistent.py b/secop/persistent.py index 2d0f12f..5ef3ab5 100644 --- a/secop/persistent.py +++ b/secop/persistent.py @@ -93,7 +93,7 @@ class PersistentMixin(HasAccessibles): when a hardware powerdown is detected """ try: - with open(self.persistentFile, 'r') as f: + with open(self.persistentFile, 'r', encoding='utf-8') as f: self.persistentData = json.load(f) except Exception: self.persistentData = {} @@ -134,7 +134,7 @@ class PersistentMixin(HasAccessibles): if not os.path.isdir(persistentdir): os.makedirs(persistentdir, exist_ok=True) try: - with open(tmpfile, 'w') as f: + with open(tmpfile, 'w', encoding='utf-8') as f: json.dump(self.persistentData, f, indent=2) f.write('\n') os.rename(tmpfile, self.persistentFile) diff --git a/secop/proxy.py b/secop/proxy.py index 8ecacbf..10fe83f 100644 --- a/secop/proxy.py +++ b/secop/proxy.py @@ -154,6 +154,7 @@ class SecNode(Module): def request(self, msg): """send a request, for debugging purposes""" reply = self.secnode.request(*decode_msg(msg.encode('utf-8'))) + # pylint: disable=not-an-iterable return encode_msg_frame(*reply).decode('utf-8') diff --git a/secop/server.py b/secop/server.py index c46a5e0..e2be9c6 100644 --- a/secop/server.py +++ b/secop/server.py @@ -39,7 +39,7 @@ from secop.modules import Attached try: from daemon import DaemonContext try: - import daemon.pidlockfile as pidlockfile + from daemon import pidlockfile except ImportError: import daemon.pidfile as pidlockfile except ImportError: diff --git a/secop/version.py b/secop/version.py index 236c939..27be677 100644 --- a/secop/version.py +++ b/secop/version.py @@ -38,10 +38,10 @@ GIT_REPO = os.path.join(os.path.dirname(__file__), '..', '.git') def get_git_version(abbrev=4, cwd=None): try: print("REPO:", GIT_REPO) - p = Popen(['git', '--git-dir=%s' % GIT_REPO, - 'describe', '--abbrev=%d' % abbrev], - stdout=PIPE, stderr=PIPE) - stdout, _stderr = p.communicate() + with Popen(['git', '--git-dir=%s' % GIT_REPO, + 'describe', '--abbrev=%d' % abbrev], + stdout=PIPE, stderr=PIPE) as p: + stdout, _stderr = p.communicate() version = stdout.strip().decode('utf-8', 'ignore') print("git:", version) # mangle version to comply with pep440 @@ -55,14 +55,14 @@ def get_git_version(abbrev=4, cwd=None): def read_release_version(): try: - with open(RELEASE_VERSION_FILE) as f: + with open(RELEASE_VERSION_FILE, encoding='utf-8') as f: return f.readline().strip() except Exception: return None def write_release_version(version): - with open(RELEASE_VERSION_FILE, 'w') as f: + with open(RELEASE_VERSION_FILE, 'w', encoding='utf-8') as f: f.write("%s\n" % version) diff --git a/secop_demo/cryo.py b/secop_demo/cryo.py index 20533af..b53ba32 100644 --- a/secop_demo/cryo.py +++ b/secop_demo/cryo.py @@ -355,5 +355,5 @@ class Cryostat(CryoBase): def shutdown(self): # should be called from server when the server is stopped self._stopflag = True - if self._thread and self._thread.isAlive(): + if self._thread and self._thread.is_alive(): self._thread.join() diff --git a/secop_psi/ppmssim.py b/secop_psi/ppmssim.py index 05f6bf8..d096acf 100644 --- a/secop_psi/ppmssim.py +++ b/secop_psi/ppmssim.py @@ -188,7 +188,7 @@ class PpmsSim: else: self.status.pos = 5 - self.st = sum([self.status[i] << (i * 4) for i in range(4)]) + self.st = sum(self.status[i] << (i * 4) for i in range(4)) self.r1 = self.t * 0.1 self.i1 = self.t % 10.0 self.r2 = 1000 / self.t diff --git a/secop_psi/softcal.py b/secop_psi/softcal.py index 0619767..0534308 100644 --- a/secop_psi/softcal.py +++ b/secop_psi/softcal.py @@ -144,7 +144,7 @@ class CalCurve: try: parser = cls(**args) - with open(filename) as f: + with open(filename, encoding='utf-8') as f: for line in f: parser.parse(line) except Exception as e: diff --git a/test/conftest.py b/test/conftest.py index 6289b9b..eba9faf 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -15,7 +15,7 @@ def constants(): del c -@pytest.fixture(scope="session") # pylint: disable=redefined-builtin +@pytest.fixture(scope="session") def globals(): - return dict() + return {} diff --git a/test/test_handler.py b/test/test_handler.py index 6d00fa7..2a5359a 100644 --- a/test/test_handler.py +++ b/test/test_handler.py @@ -108,7 +108,7 @@ def test_handler(): assert m.read_b() == 7 assert data.pop() == 'b' - assert data == [] + assert not data def test_common_handler(): @@ -137,7 +137,7 @@ def test_common_handler(): assert m.a == 3 assert m.b == 2 assert data.pop() == 'write_hdl' - assert m.writeDict == {} + assert not m.writeDict m.write_b(4) assert m.a == 3 @@ -159,7 +159,7 @@ def test_common_handler(): assert m.b == 2.2 assert data.pop() == 'read_hdl' - assert data == [] + assert not data def test_nopoll(): diff --git a/test/test_lib_enum.py b/test/test_lib_enum.py index 7077834..580dd86 100644 --- a/test/test_lib_enum.py +++ b/test/test_lib_enum.py @@ -49,7 +49,7 @@ def test_EnumMember(): a += 2 # this shall work - assert 2 == (a + 1) # pylint: disable=C0122 + assert 2 == (a + 1) assert (a - 1) == 0 assert a assert a + a diff --git a/test/test_logging.py b/test/test_logging.py index 7c07bb4..a0c9c1c 100644 --- a/test/test_logging.py +++ b/test/test_logging.py @@ -54,6 +54,7 @@ class Connection: @pytest.fixture(name='init') def init_(monkeypatch): + # pylint: disable=unnecessary-dunder-call logger.__init__() class Playground: diff --git a/test/test_modules.py b/test/test_modules.py index cbe43e5..c3364e7 100644 --- a/test/test_modules.py +++ b/test/test_modules.py @@ -542,7 +542,7 @@ def test_generic_access(): assert obj.read_unhandled() assert updates == {'obj': {'param': 'potato'}} updates.clear() - assert updates == {} + assert not updates def test_duplicate_handler_name(): diff --git a/test/test_msg.py b/test/test_msg.py index 05c09c7..aee0e87 100644 --- a/test/test_msg.py +++ b/test/test_msg.py @@ -40,7 +40,7 @@ MSG = [ [(m.EVENTREPLY, 'mod:par', [123, dict(t=12.25)]), b'update mod:par [123, {"t": 12.25}]'], [(m.HEARTBEATREQUEST, '0', None), b'ping 0'], [(m.HEARTBEATREPLY, None, [None, dict(t=11.75)]), b'pong [null, {"t": 11.75}]'], - [(m.ERRORPREFIX + m.WRITEREQUEST, 'm:p', ['ErrClass', 'text', dict()]), + [(m.ERRORPREFIX + m.WRITEREQUEST, 'm:p', ['ErrClass', 'text', {}]), b'error_change m:p ["ErrClass", "text", {}]'], ] @pytest.mark.parametrize('msg, line', MSG)