Skip to content

Commit

Permalink
Remove default argument in hookimpl overshadowing the runtime value,
Browse files Browse the repository at this point in the history
  • Loading branch information
krassowski committed Sep 10, 2023
1 parent 6c168d0 commit 5bc1de1
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 18 deletions.
82 changes: 65 additions & 17 deletions pylsp/plugins/pyflakes_lint.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Copyright 2017-2020 Palantir Technologies, Inc.
# Copyright 2021- Python Language Server Contributors.

from pyflakes import api as pyflakes_api, messages
import ast
from contextlib import contextmanager

from pyflakes import api as pyflakes_api, messages, checker
from pylsp import hookimpl, lsp

# Pyflakes messages that should be reported as Errors instead of Warns
Expand All @@ -20,12 +23,14 @@


@hookimpl
def pylsp_lint(workspace, document):
def pylsp_lint(document):
with workspace.report_progress("lint: pyflakes"):
reporter = PyflakesDiagnosticReport(document.lines)
pyflakes_api.check(
document.source.encode("utf-8"), document.path, reporter=reporter
)
if end_coordinates_patch.is_needed:
with end_coordinates_patch.activate_patch():
reporter = PyflakesDiagnosticReport(document.lines)
else:
reporter = PyflakesDiagnosticReport(document.lines)
pyflakes_api.check(document.source.encode('utf-8'), document.path, reporter=reporter)
return reporter.diagnostics


Expand Down Expand Up @@ -72,10 +77,10 @@ def syntaxError(self, _filename, msg, lineno, offset, text):
def flake(self, message):
"""Get message like <filename>:<lineno>: <msg>"""
err_range = {
"start": {"line": message.lineno - 1, "character": message.col},
"end": {
"line": message.lineno - 1,
"character": len(self.lines[message.lineno - 1]),
'start': {'line': message.lineno - 1, 'character': message.col},
'end': {
'line': getattr(message, 'end_lineno', message.lineno) - 1,
'character': getattr(message, 'end_col_offset', len(self.lines[message.lineno - 1]))
},
}

Expand All @@ -85,11 +90,54 @@ def flake(self, message):
severity = lsp.DiagnosticSeverity.Error
break

self.diagnostics.append(
{
"source": "pyflakes",
"range": err_range,
"message": message.message % message.message_args,
"severity": severity,
}
self.diagnostics.append({
'source': 'pyflakes',
'range': err_range,
'message': message.message % message.message_args,
'severity': severity
})


class PyflakesEndPositionsPatch:
def __init__(self, checker_class, message_class):
self.is_active = False
self.is_needed = self._check_if_patch_needed(message_class)
checker_class.handleNode = self._patch_handle_node(checker_class.handleNode)
message_class.__init__ = self._patch_message_init(message_class.__init__)

@contextmanager
def activate_patch(self):
self.is_active = True
try:
yield
finally:
self.is_active = False

def _check_if_patch_needed(self, message_class) -> bool:
node = ast.parse('1').body[0]
message = message_class('fielname', node)
python_version_supports_end_coords = hasattr(node, 'end_lineno')
is_patch_applied_upstream = hasattr(message, 'end_lineno')
return (
python_version_supports_end_coords
and not is_patch_applied_upstream
)

def _patch_handle_node(self, original):
def patched(this, node, parent):
if self.is_active and this.offset and getattr(node, 'end_lineno', None) is not None:
node.end_lineno += this.offset[0]
node.end_col_offset += this.offset[1]
return original(this, node, parent)
return patched

def _patch_message_init(self, original):
def patched(this, filename, loc):
original(this, filename, loc)
if self.is_active:
this.end_col = getattr(loc, 'end_col_offset', None)
this.end_lineno = getattr(loc, 'end_lineno', None)
return patched


end_coordinates_patch = PyflakesEndPositionsPatch(checker.Checker, messages.Message)
2 changes: 1 addition & 1 deletion pylsp/plugins/references.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


@hookimpl
def pylsp_references(document, position, exclude_declaration=False):
def pylsp_references(document, position, exclude_declaration):
code_position = _utils.position_to_jedi_linecolumn(document, position)
usages = document.jedi_script().get_references(**code_position)

Expand Down

0 comments on commit 5bc1de1

Please sign in to comment.