From c66529172cec46da43d778b703969e082287c239 Mon Sep 17 00:00:00 2001 From: Nicholas Gates Date: Thu, 14 Dec 2017 08:52:42 +0000 Subject: [PATCH] Enable pylint (#202) --- pyls/__main__.py | 2 ++ pyls/_utils.py | 6 ++--- pyls/_version.py | 2 +- pyls/config/flake8_conf.py | 5 ++-- pyls/config/pycodestyle_conf.py | 2 +- pyls/language_server.py | 5 ++-- pyls/plugins/format.py | 44 ++++++++++++++++---------------- pyls/plugins/hover.py | 2 +- pyls/plugins/pycodestyle_lint.py | 11 ++++---- pyls/plugins/pyflakes_lint.py | 10 ++++---- pyls/plugins/signature.py | 10 ++++---- pyls/plugins/symbols.py | 2 +- pyls/python_ls.py | 5 ++-- pyls/server.py | 2 +- pyls/workspace.py | 8 +++--- tox.ini | 2 ++ 16 files changed, 62 insertions(+), 56 deletions(-) diff --git a/pyls/__main__.py b/pyls/__main__.py index 508d918c..1671b67e 100644 --- a/pyls/__main__.py +++ b/pyls/__main__.py @@ -80,12 +80,14 @@ def _binary_stdio(): PY3K = sys.version_info >= (3, 0) if PY3K: + # pylint: disable=no-member stdin, stdout = sys.stdin.buffer, sys.stdout.buffer else: # Python 2 on Windows opens sys.stdin in text mode, and # binary data that read from it becomes corrupted on \r\n if sys.platform == "win32": # set sys.stdin to binary mode + # pylint: disable=no-member,import-error import os import msvcrt msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY) diff --git a/pyls/_utils.py b/pyls/_utils.py index 6c5a296d..3251fdb4 100644 --- a/pyls/_utils.py +++ b/pyls/_utils.py @@ -65,7 +65,7 @@ def find_parents(root, path, names): def list_to_string(value): - return ",".join(value) if type(value) == list else value + return ",".join(value) if isinstance(value, list) else value def merge_dicts(dict_a, dict_b): @@ -122,7 +122,7 @@ def format_docstring(contents): Until we can find a fast enough way of discovering and parsing each format, we can do a little better by at least preserving indentation. """ - contents = contents.replace('\t', '\u00A0' * 4) - contents = contents.replace(' ', '\u00A0' * 2) + contents = contents.replace('\t', u'\u00A0' * 4) + contents = contents.replace(' ', u'\u00A0' * 2) contents = contents.replace('*', '\\*') return contents diff --git a/pyls/_version.py b/pyls/_version.py index 89b42fc2..aab9f795 100644 --- a/pyls/_version.py +++ b/pyls/_version.py @@ -1,4 +1,4 @@ - +# pylint: skip-file # This file helps to compute a version number in source trees obtained from # git-archive tarball (such as those provided by githubs download-from-tag # feature). Distribution tarballs (built by setup.py sdist) and build diff --git a/pyls/config/flake8_conf.py b/pyls/config/flake8_conf.py index a7f4fa21..419a7f4a 100644 --- a/pyls/config/flake8_conf.py +++ b/pyls/config/flake8_conf.py @@ -1,8 +1,8 @@ # Copyright 2017 Palantir Technologies, Inc. import logging import os -from .source import ConfigSource from pyls._utils import find_parents +from .source import ConfigSource log = logging.getLogger(__name__) @@ -33,8 +33,7 @@ def user_config(self): def _user_config_file(self): if self.is_windows: return os.path.expanduser('~\\.flake8') - else: - return os.path.join(self.xdg_home, 'flake8') + return os.path.join(self.xdg_home, 'flake8') def project_config(self, document_path): files = find_parents(self.root_path, document_path, PROJECT_CONFIGS) diff --git a/pyls/config/pycodestyle_conf.py b/pyls/config/pycodestyle_conf.py index 5863ad14..c09375b0 100644 --- a/pyls/config/pycodestyle_conf.py +++ b/pyls/config/pycodestyle_conf.py @@ -1,7 +1,7 @@ # Copyright 2017 Palantir Technologies, Inc. import pycodestyle -from .source import ConfigSource from pyls._utils import find_parents +from .source import ConfigSource CONFIG_KEY = 'pycodestyle' diff --git a/pyls/language_server.py b/pyls/language_server.py index 487b5135..8c2a647a 100644 --- a/pyls/language_server.py +++ b/pyls/language_server.py @@ -15,6 +15,7 @@ class _StreamHandlerWrapper(socketserver.StreamRequestHandler, object): def setup(self): super(_StreamHandlerWrapper, self).setup() + # pylint: disable=no-member self.delegate = self.DELEGATE_CLASS(self.rfile, self.wfile) def handle(self): @@ -62,7 +63,7 @@ def __getitem__(self, item): def wrapped(*args, **kwargs): try: return func(*args, **kwargs) - except: # pylint: disable=bare-except + except: log.exception("CAUGHT") raise return wrapped @@ -77,7 +78,7 @@ class LanguageServer(MethodJSONRPCServer): root_uri = None init_opts = None - def capabilities(self): + def capabilities(self): # pylint: disable=no-self-use return {} def initialize(self, root_uri, init_opts, process_id): diff --git a/pyls/plugins/format.py b/pyls/plugins/format.py index e0a47c39..4d770a4b 100644 --- a/pyls/plugins/format.py +++ b/pyls/plugins/format.py @@ -14,7 +14,7 @@ def pyls_format_document(document): @hookimpl -def pyls_format_range(document, range): +def pyls_format_range(document, range): # pylint: disable=redefined-builtin # First we 'round' the range up/down to full lines only range['start']['character'] = 0 range['end']['line'] += 1 @@ -32,25 +32,25 @@ def pyls_format_range(document, range): def _format(document, lines=None): - new_source, changed = FormatCode( - document.source, - lines=lines, - filename=document.filename, - style_config=file_resources.GetDefaultStyleForDir( - os.path.dirname(document.filename) - ) + new_source, changed = FormatCode( + document.source, + lines=lines, + filename=document.filename, + style_config=file_resources.GetDefaultStyleForDir( + os.path.dirname(document.filename) ) - - if not changed: - return [] - - # I'm too lazy at the moment to parse diffs into TextEdit items - # So let's just return the entire file... - return [{ - 'range': { - 'start': {'line': 0, 'character': 0}, - # End char 0 of the line after our document - 'end': {'line': len(document.lines), 'character': 0} - }, - 'newText': new_source - }] + ) + + if not changed: + return [] + + # I'm too lazy at the moment to parse diffs into TextEdit items + # So let's just return the entire file... + return [{ + 'range': { + 'start': {'line': 0, 'character': 0}, + # End char 0 of the line after our document + 'end': {'line': len(document.lines), 'character': 0} + }, + 'newText': new_source + }] diff --git a/pyls/plugins/hover.py b/pyls/plugins/hover.py index 4fe01937..fe1eca82 100644 --- a/pyls/plugins/hover.py +++ b/pyls/plugins/hover.py @@ -13,7 +13,7 @@ def pyls_hover(document, position): # Find an exact match for a completion definitions = [d for d in definitions if d.name == word] - if len(definitions) == 0: + if not definitions: # :( return {'contents': ''} diff --git a/pyls/plugins/pycodestyle_lint.py b/pyls/plugins/pycodestyle_lint.py index 4dde8f74..d157a38e 100644 --- a/pyls/plugins/pycodestyle_lint.py +++ b/pyls/plugins/pycodestyle_lint.py @@ -36,23 +36,24 @@ def __init__(self, options=None): self.diagnostics = [] super(PyCodeStyleDiagnosticReport, self).__init__(options=options) - def error(self, lineno, offset, text, check): + def error(self, line_number, offset, text, check): # PyCodeStyle will sometimes give you an error the line after the end of the file # e.g. no newline at end of file # In that case, the end offset should just be some number ~100 # (because why not? There's nothing to underline anyways) - range = { - 'start': {'line': lineno - 1, 'character': offset}, + err_range = { + 'start': {'line': line_number - 1, 'character': offset}, 'end': { # FIXME: It's a little naiive to mark until the end of the line, can we not easily do better? - 'line': lineno - 1, 'character': 100 if lineno > len(self.lines) else len(self.lines[lineno - 1]) + 'line': line_number - 1, + 'character': 100 if line_number > len(self.lines) else len(self.lines[line_number - 1]) }, } code, _message = text.split(" ", 1) self.diagnostics.append({ 'source': 'pycodestyle', - 'range': range, + 'range': err_range, 'message': text, 'code': code, # Are style errors really ever errors? diff --git a/pyls/plugins/pyflakes_lint.py b/pyls/plugins/pyflakes_lint.py index 600a4937..22757ddd 100644 --- a/pyls/plugins/pyflakes_lint.py +++ b/pyls/plugins/pyflakes_lint.py @@ -19,27 +19,27 @@ def __init__(self, lines): def unexpectedError(self, filename, msg): # pragma: no cover pass - def syntaxError(self, filename, msg, lineno, offset, text): - range = { + def syntaxError(self, _filename, msg, lineno, offset, text): + err_range = { 'start': {'line': lineno - 1, 'character': offset}, 'end': {'line': lineno - 1, 'character': offset + len(text)}, } self.diagnostics.append({ 'source': 'pyflakes', - 'range': range, + 'range': err_range, 'message': msg, 'severity': lsp.DiagnosticSeverity.Error, }) def flake(self, message): """ Get message like :: """ - range = { + err_range = { 'start': {'line': message.lineno - 1, 'character': message.col}, 'end': {'line': message.lineno - 1, 'character': len(self.lines[message.lineno - 1])}, } self.diagnostics.append({ 'source': 'pyflakes', - 'range': range, + 'range': err_range, 'message': message.message % message.message_args, 'severity': lsp.DiagnosticSeverity.Warning }) diff --git a/pyls/plugins/signature.py b/pyls/plugins/signature.py index e737865f..1c1c1c0c 100644 --- a/pyls/plugins/signature.py +++ b/pyls/plugins/signature.py @@ -5,9 +5,9 @@ log = logging.getLogger(__name__) -SPHINX = re.compile("\s*:param\s+(?P\w+):\s*(?P[^\\n]+)") -EPYDOC = re.compile("\s*@param\s+(?P\w+):\s*(?P[^\\n]+)") -GOOGLE = re.compile("\s*(?P\w+).*:\s*(?P[^\\n]+)") +SPHINX = re.compile(r"\s*:param\s+(?P\w+):\s*(?P[^\n]+)") +EPYDOC = re.compile(r"\s*@param\s+(?P\w+):\s*(?P[^\n]+)") +GOOGLE = re.compile(r"\s*(?P\w+).*:\s*(?P[^\n]+)") DOC_REGEX = [SPHINX, EPYDOC, GOOGLE] @@ -16,7 +16,7 @@ def pyls_signature_help(document, position): signatures = document.jedi_script(position).call_signatures() - if len(signatures) == 0: + if not signatures: return {'signatures': []} s = signatures[0] @@ -26,7 +26,7 @@ def pyls_signature_help(document, position): } # If there are params, add those - if len(s.params) > 0: + if s.params: sig['parameters'] = [{ 'label': p.name, 'documentation': _param_docs(s.docstring(), p.name) diff --git a/pyls/plugins/symbols.py b/pyls/plugins/symbols.py index 8dd83a07..c0f9747a 100644 --- a/pyls/plugins/symbols.py +++ b/pyls/plugins/symbols.py @@ -39,7 +39,7 @@ def _container(definition): # as children of the module. if parent.parent(): return parent.name - except: + except: # pylint: disable=bare-except pass diff --git a/pyls/python_ls.py b/pyls/python_ls.py index d0b78147..8bceb6e5 100644 --- a/pyls/python_ls.py +++ b/pyls/python_ls.py @@ -13,6 +13,7 @@ class PythonLanguageServer(LanguageServer): + # pylint: disable=too-many-public-methods,redefined-builtin workspace = None config = None @@ -148,14 +149,14 @@ def m_text_document__hover(self, textDocument=None, position=None, **_kwargs): def m_text_document__document_symbol(self, textDocument=None, **_kwargs): return self.document_symbols(textDocument['uri']) - def m_text_document__formatting(self, textDocument=None, options=None, **_kwargs): + def m_text_document__formatting(self, textDocument=None, _options=None, **_kwargs): # For now we're ignoring formatting options. return self.format_document(textDocument['uri']) def m_text_document__rename(self, textDocument=None, position=None, newName=None, **_kwargs): return self.rename(textDocument['uri'], position, newName) - def m_text_document__range_formatting(self, textDocument=None, range=None, options=None, **_kwargs): + def m_text_document__range_formatting(self, textDocument=None, range=None, _options=None, **_kwargs): # Again, we'll ignore formatting options for now. return self.format_range(textDocument['uri'], range) diff --git a/pyls/server.py b/pyls/server.py index 5e4acee1..5b1ca6ac 100644 --- a/pyls/server.py +++ b/pyls/server.py @@ -56,7 +56,7 @@ def handle(self): on_result(msg['result']) elif 'error' in msg and on_error: on_error(msg['error']) - except Exception: + except: # pylint: disable=bare-except log.exception("Language server exiting due to uncaught exception") break diff --git a/pyls/workspace.py b/pyls/workspace.py index 03461aad..a4ee406f 100644 --- a/pyls/workspace.py +++ b/pyls/workspace.py @@ -22,19 +22,19 @@ def get_submodules(mod): """Get all submodules of a given module""" - def catch_exceptions(module): + def catch_exceptions(_module): pass + try: m = __import__(mod) submodules = [mod] - submods = pkgutil.walk_packages(m.__path__, m.__name__ + '.', - catch_exceptions) + submods = pkgutil.walk_packages(m.__path__, m.__name__ + '.', catch_exceptions) for sm in submods: sm_name = sm[1] submodules.append(sm_name) except ImportError: return [] - except Exception: + except: # pylint: disable=bare-except return [mod] return submodules diff --git a/tox.ini b/tox.ini index a4cd3b39..28a64c69 100644 --- a/tox.ini +++ b/tox.ini @@ -23,8 +23,10 @@ deps = pytest coverage pytest-cov + pylint [testenv:lint] commands = + pylint pyls pycodestyle pyls pyflakes pyls