Skip to content

Commit

Permalink
Fix pylint build
Browse files Browse the repository at this point in the history
  • Loading branch information
Stribik András committed Feb 12, 2025
1 parent 3f707c0 commit 811fb57
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 54 deletions.
25 changes: 11 additions & 14 deletions rplugin/python3/semshi/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def __init__(self, buf, vim, options):
self._selected_nodes = []

def __repr__(self):
return '<BufferHandler(%d)>' % self._buf_num
return f'<BufferHandler({self._buf_num:d})>'

def viewport(self, start, stop):
"""Set viewport to line range from `start` to `stop` and add highlights
Expand Down Expand Up @@ -194,7 +194,7 @@ def _visible_and_hidden(self, nodes):
return visible, hidden

# pylint: disable=protected-access
@debug_time(None, lambda s, n: '%d / %d' % (len(n), len(s._pending_nodes)))
@debug_time(None, lambda s, n: f'{len(n):d} / {len(s._pending_nodes):d}')
def _remove_from_pending(self, nodes):
"""Return nodes which couldn't be removed from the pending list (which
means they need to be cleared from the buffer).
Expand Down Expand Up @@ -243,22 +243,20 @@ def _update_error_indicator(self):

def _place_sign(self, id, line, name):
self._wrap_async(self._vim.command)(
'sign place %d line=%d name=%s buffer=%d' % (
id, line, name, self._buf_num),
f'sign place {id:d} line={line:d} name={name:s} buffer={self._buf_num:d}',
async_=True)

def _unplace_sign(self, id):
self._wrap_async(self._vim.command)(
'sign unplace %d buffer=%d' % (
id, self._buf_num),
f'sign unplace {id:d} buffer={self._buf_num:d}',
async_=True)

@debug_time(None, lambda _, a, c: '+%d, -%d' % (len(a), len(c)))
@debug_time(None, lambda _, a, c: f'+{len(a):d}, -{len(c):d}')
def _update_hls(self, add, clear):
self._add_hls(nodes_to_hl(add))
self._clear_hls(nodes_to_hl(clear, clear=True))

@debug_time(None, lambda _, nodes: '%d nodes' % len(nodes))
@debug_time(None, lambda _, nodes: f'{len(nodes):d} nodes')
def _add_hls(self, node_or_nodes):
buf = self._buf
if not node_or_nodes:
Expand All @@ -269,7 +267,7 @@ def _add_hls(self, node_or_nodes):
self._call_atomic_async(
[('nvim_buf_add_highlight', (buf, *n)) for n in node_or_nodes])

@debug_time(None, lambda _, nodes: '%d nodes' % len(nodes))
@debug_time(None, lambda _, nodes: f'{len(nodes):d} nodes')
def _clear_hls(self, node_or_nodes):
buf = self._buf
if not node_or_nodes:
Expand Down Expand Up @@ -304,7 +302,7 @@ def rename(self, cursor, new_name=None):
))
num = len(nodes)
if new_name is None:
new_name = self._vim.eval('input("Rename %d nodes to: ")' % num)
new_name = self._vim.eval(f'input("Rename {num:d} nodes to: ")')
# Can't output a carriage return via out_write()
self._vim.command('echo "\r"')
if not new_name or new_name == cur_node.name:
Expand All @@ -322,7 +320,7 @@ def rename(self, cursor, new_name=None):
line[node.col + len(node.name) + offset:])
offset += len(new_name) - len(node.name)
self._buf[lineno - 1] = line
self._vim.out_write('%d nodes renamed.\n' % num)
self._vim.out_write(f'{num:d} nodes renamed.\n')

def goto(self, what, direction=None):
"""Go to next location of type `what` in direction `direction`."""
Expand All @@ -346,7 +344,7 @@ def goto(self, what, direction=None):
elif what in hl_groups:
locs = self._parser.locations_by_hl_group(hl_groups[what])
else:
raise ValueError('"%s" is not a recognized element type.' % what)
raise ValueError(f'"{what:s}" is not a recognized element type.')
if not locs:
return
if direction == 'first':
Expand Down Expand Up @@ -381,8 +379,7 @@ def show_error(self):
if error is None:
self._vim.out_write('No syntax error to show.\n')
return
self._vim.out_write('Syntax error: %s (%d, %d)\n' %
(error.msg, error.lineno, error.offset))
self._vim.out_write(f'Syntax error: {error.msg:s} ({error.lineno:d}, {error.offset:d})\n')

def shutdown(self):
# Cancel the error timer so vim quits immediately
Expand Down
16 changes: 5 additions & 11 deletions rplugin/python3/semshi/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Node:
__slots__ = ['id', 'name', 'lineno', 'col', 'end', 'env',
'symname', 'symbol', 'hl_group', 'target', '_tup']

# pylint: disable=too-many-positional-arguments
def __init__(self, name, lineno, col, env, target=None, hl_group=None):
self.id = next(Node.id_counter)
self.name = name
Expand All @@ -56,9 +57,7 @@ def __init__(self, name, lineno, col, env, target=None, hl_group=None):
except KeyError as exc:
# Set dummy hl group, so all fields in __repr__ are defined.
self.hl_group = '?'
raise Exception(
'%s can\'t lookup "%s"' % (self, self.symname)
) from exc
raise KeyError(f'{self:s} can\'t lookup "{self.symname:s}"') from exc
if hl_group is not None:
self.hl_group = hl_group
else:
Expand All @@ -80,14 +79,9 @@ def __hash__(self):
return hash(self._tup)

def __repr__(self):
return '<%s %s %s (%s, %s) %d>' % (
self.name,
self.hl_group[6:],
'.'.join([x.get_name() for x in self.env]),
self.lineno,
self.col,
self.id,
)
env_names = '.'.join([x.get_name() for x in self.env])
hl_group = self.hl_group[6:]
return f'<{self.name} {hl_group} {env_names} ({self.lineno}, {self.col}) {self.id}>'

def _make_hl_group(self):
"""Return highlight group the node belongs to."""
Expand Down
18 changes: 4 additions & 14 deletions rplugin/python3/semshi/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def cmd_semshi(self, args):
try:
func = _subcommands[args[0]]
except KeyError:
self.echo_error('Subcommand not found: %s' % args[0])
self.echo_error(f'Subcommand not found: {args[0]}')
return
func(self, *args[1:])

Expand Down Expand Up @@ -188,14 +188,7 @@ def error(self):

@subcommand
def status(self):
self.echo(
'current handler: {handler}\n'
'handlers: {handlers}'
.format(
handler=self._cur_handler,
handlers=self._handlers
)
)
self.echo(f'current handler: {self._cur_handler,}\nhandlers: {self._handlers}')

def _select_handler(self, buf_or_buf_num):
"""Select handler for `buf_or_buf_num`."""
Expand Down Expand Up @@ -224,8 +217,7 @@ def _remove_handler(self, buf_or_buf_num):
handler = self._handlers.pop(buf_num)
except KeyError:
return
else:
handler.shutdown()
handler.shutdown()

def _update_viewport(self, start, stop):
self._cur_handler.viewport(start, stop)
Expand Down Expand Up @@ -285,6 +277,4 @@ def _convert_excluded_hl_groups(items):
return [hl_groups[g] for g in items]
except KeyError as e:
# TODO Use err_write instead?
raise Exception(
'"%s" is an unknown highlight group.' % e.args[0]
) from e
raise KeyError(f'"{e.args[0]}" is an unknown highlight group.') from e
2 changes: 1 addition & 1 deletion rplugin/python3/semshi/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def wrapper(*args, **kwargs):
label = func.__name__
except AttributeError:
label = func.__class__.__name__
text = 'TIME %s: %f ' % (label, time.time() - t)
text = f'TIME {label}: {time.time() - t:f} '
if detail is not None:
if callable(detail):
text += detail(*args, **kwargs)
Expand Down
23 changes: 9 additions & 14 deletions rplugin/python3/semshi/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
from ast import (AsyncFunctionDef, Attribute, ClassDef, DictComp, Eq,
ExceptHandler, FunctionDef, GeneratorExp, Global, Gt, GtE,
Import, ImportFrom, Lambda, ListComp, Load, Lt, LtE, Module,
Name, NameConstant, Nonlocal, NotEq, Num, SetComp, Store, Str,
Try, arg)
Name, Constant, Nonlocal, NotEq, SetComp, Store, Try, arg)
from itertools import count
import sys
from token import NAME, OP
Expand All @@ -17,12 +16,7 @@
SetComp, GeneratorExp, Lambda)
FUNCTION_BLOCKS = (FunctionDef, Lambda, AsyncFunctionDef)
# Node types which don't require any action
if sys.version_info < (3, 8):
SKIP = (NameConstant, Str, Num)
else:
from ast import Constant # pylint: disable=ungrouped-imports
SKIP = (Constant,)
SKIP += (Store, Load, Eq, Lt, Gt, NotEq, LtE, GtE)
SKIP = (Constant, Store, Load, Eq, Lt, Gt, NotEq, LtE, GtE)


def tokenize_lines(lines):
Expand All @@ -36,13 +30,14 @@ def advance(tokens, s=None, type=NAME):
matching one of the strings in `s` if `s` is an iterable. Without any
arguments, just advances to next NAME token.
"""
return next(t for t in tokens if t.type == type and _cond(s)(t))

def _cond(s):
if s is None:
cond = lambda token: True
elif isinstance(s, str):
cond = lambda token: token.string == s
else:
cond = lambda token: token.string in s
return next(t for t in tokens if t.type == type and cond(t))
return lambda token: True
if isinstance(s, str):
return lambda token: token.string == s
return lambda token: token.string in s


@debug_time
Expand Down

0 comments on commit 811fb57

Please sign in to comment.