Skip to content

Commit

Permalink
Fix a bunch of linting issues and upgrade a lot of string formatting …
Browse files Browse the repository at this point in the history
…to f-strings.
  • Loading branch information
ionelmc committed May 1, 2024
1 parent b70190e commit 5c463b0
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 116 deletions.
2 changes: 1 addition & 1 deletion build_backend/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
from setuptools.build_meta import * # noqa

if hasattr(build_meta, 'build_editable'):
del build_editable
del build_editable # noqa: F821
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ max-line-length = 140
ignore = [
"RUF001", # ruff-specific rules ambiguous-unicode-character-string
"S101", # flake8-bandit assert
"S307",
"S308", # flake8-bandit suspicious-mark-safe-usage
"E501", # pycodestyle line-too-long
]
Expand Down
1 change: 1 addition & 0 deletions src/hunter/_event.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ cdef extern from *:
void PyFrame_FastToLocals(FrameType)
int PyFrame_GetLineNumber(FrameType)


@cython.final
cdef class Event:
cdef:
Expand Down
4 changes: 2 additions & 2 deletions src/hunter/_event.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,9 @@ cdef class Event:
elif filename.endswith(('.pyc', '.pyo')):
filename = filename[:-1]
elif filename.endswith(('.so', '.pyd')):
basename = CYTHON_SUFFIX_RE.sub('', filename)
cybasename = CYTHON_SUFFIX_RE.sub('', filename)
for ext in ('.pyx', '.py'):
cyfilename = basename + ext
cyfilename = cybasename + ext
if exists(cyfilename):
filename = cyfilename
break
Expand Down
6 changes: 6 additions & 0 deletions src/hunter/_predicates.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,32 @@ cdef class Query:
readonly tuple query_regex
readonly tuple query_startswith


@cython.final
cdef class And:
cdef:
readonly tuple predicates


@cython.final
cdef class Or:
cdef:
readonly tuple predicates


@cython.final
cdef class Not:
cdef:
readonly object predicate


@cython.final
cdef class When:
cdef:
readonly object condition
readonly tuple actions


@cython.final
cdef class From:
cdef:
Expand All @@ -48,6 +53,7 @@ cdef class From:
readonly int origin_depth
readonly int origin_calls


@cython.final
cdef class Backlog:
cdef:
Expand Down
5 changes: 2 additions & 3 deletions src/hunter/_predicates.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ cdef Event_getter_typedef[17] Event_getters = [
Event_get_builtin,
]


@cython.final
cdef class QueryEntry:
cdef Event_getter_typedef getter
Expand All @@ -94,6 +95,7 @@ cdef class QueryEntry:
and self.getter_index == (<QueryEntry> other).getter_index
)


@cython.final
cdef class Query:
"""
Expand Down Expand Up @@ -125,8 +127,6 @@ cdef class Query:
``threadid``,
``threadname``.
"""
cdef void* prefix_getter_pointer

query_eq = {}
query_startswith = {}
query_endswith = {}
Expand Down Expand Up @@ -196,7 +196,6 @@ cdef class Query:
self.query_gt = tuple(sorted(query_gt.items()))
self.query_gte = tuple(sorted(query_gte.items()))


def __str__(self):
return 'Query(%s)' % (
', '.join([
Expand Down
1 change: 1 addition & 0 deletions src/hunter/_tracer.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ cdef extern from "pystate.h":
Py_tracefunc c_tracefunc
Py_tracefunc c_profilefunc


@cython.final
cdef class Tracer:
cdef:
Expand Down
48 changes: 24 additions & 24 deletions src/hunter/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from collections import defaultdict
from itertools import islice
from os import getpid
from typing import ClassVar

from . import config
from .util import BUILTIN_SYMBOLS
Expand Down Expand Up @@ -67,10 +68,10 @@ def __eq__(self, other):
return type(self) is type(other) and self.klass == other.klass and self.kwargs == other.kwargs

def __str__(self):
return '{0.__class__.__name__}(klass={0.klass}, kwargs={0.kwargs})'.format(self)
return f'{self.__class__.__name__}(klass={self.klass}, kwargs={self.kwargs})'

def __repr__(self):
return '{0.__class__.__name__}(klass={0.klass!r}, kwargs={0.kwargs!r})'.format(self)
return f'{self.__class__.__name__}(klass={self.klass!r}, kwargs={self.kwargs!r})'

def __call__(self, event):
"""
Expand All @@ -87,10 +88,10 @@ def __eq__(self, other):
return type(self) is type(other) and self.options == other.options

def __str__(self):
return '{0.__class__.__name__}(options={0.options})'.format(self)
return f'{self.__class__.__name__}(options={self.options})'

def __repr__(self):
return '{0.__class__.__name__}(options={0.options!r})'.format(self)
return f'{self.__class__.__name__}(options={self.options!r})'

def __call__(self, event):
import manhole
Expand All @@ -104,7 +105,7 @@ class ColorStreamAction(Action):
Baseclass for your custom action. Just implement your own ``__call__``.
"""

_stream_cache = {}
_stream_cache: ClassVar = {}
_stream = None
_tty = None
_repr_func = None
Expand Down Expand Up @@ -151,18 +152,18 @@ def __eq__(self, other):

def __str__(self):
return (
'{0.__class__.__name__}(stream={0.stream}, force_colors={0.force_colors}, '
'filename_alignment={0.filename_alignment}, thread_alignment={0.thread_alignment}, '
'pid_alignment={0.pid_alignment} repr_limit={0.repr_limit}, '
'repr_func={0.repr_func})'.format(self)
f'{self.__class__.__name__}(stream={self.stream}, force_colors={self.force_colors}, '
f'filename_alignment={self.filename_alignment}, thread_alignment={self.thread_alignment}, '
f'pid_alignment={self.pid_alignment} repr_limit={self.repr_limit}, '
f'repr_func={self.repr_func})'
)

def __repr__(self):
return (
'{0.__class__.__name__}(stream={0.stream!r}, force_colors={0.force_colors!r}, '
'filename_alignment={0.filename_alignment!r}, thread_alignment={0.thread_alignment!r}, '
'pid_alignment={0.pid_alignment!r} repr_limit={0.repr_limit!r}, '
'repr_func={0.repr_func!r})'.format(self)
f'{self.__class__.__name__}(stream={self.stream!r}, force_colors={self.force_colors!r}, '
f'filename_alignment={self.filename_alignment!r}, thread_alignment={self.thread_alignment!r}, '
f'pid_alignment={self.pid_alignment!r} repr_limit={self.repr_limit!r}, '
f'repr_func={self.repr_func!r})'
)

@property
Expand Down Expand Up @@ -275,7 +276,7 @@ def filename_prefix(self, event=None):
filename = f'[...]{filename[5 - self.filename_alignment :]}'
return '{:>{}}{} '.format(filename, self.filename_alignment, lineno, **self.other_colors)
else:
return '{:>{}} '.format('', self.filename_alignment)
return f'{"":>{self.filename_alignment}} '

def pid_prefix(self):
"""
Expand All @@ -287,7 +288,7 @@ def pid_prefix(self):
pid_align = self.pid_alignment
else:
pid = pid_align = ''
return '{:{}}'.format(pid, pid_align)
return f'{pid:{pid_align}}'

def thread_prefix(self, event):
"""
Expand All @@ -302,7 +303,7 @@ def thread_prefix(self, event):
threading_support = len(self.seen_threads) > 1
thread_name = threading.current_thread().name if threading_support else ''
thread_align = self.thread_alignment if threading_support else ''
return '{:{}}'.format(thread_name, thread_align)
return f'{thread_name:{thread_align}}'

def output(self, format_str, *args, **kwargs):
"""
Expand Down Expand Up @@ -431,7 +432,7 @@ class CallPrinter(CodePrinter):

EVENT_COLORS = CALL_COLORS

locals = defaultdict(list)
locals: ClassVar = defaultdict(list)

@classmethod
def cleanup(cls):
Expand Down Expand Up @@ -483,9 +484,11 @@ def __call__(self, event):
'{VARS}{0}{VARS-NAME}{1}{VARS}={RESET}{2}'.format(
prefix,
var_display,
self.try_str(event.locals.get(var_lookup, MISSING))
if event.detached
else self.try_repr(event.locals.get(var_lookup, MISSING)),
(
self.try_str(event.locals.get(var_lookup, MISSING))
if event.detached
else self.try_repr(event.locals.get(var_lookup, MISSING))
),
**self.other_colors,
)
for prefix, var_lookup, var_display in get_arguments(code)
Expand Down Expand Up @@ -860,10 +863,7 @@ def __call__(self, event):
),
)
else:
template = '{{}}{{}}{{}}{{CONT}}:{{BRIGHT}}{{fore(BLUE)}}{} {{KIND}}<= {{BRIGHT}}{{fore(YELLOW)}}no frames available {{NORMAL}}(detached={})'.format(
event.function,
event.detached,
)
template = f'{{}}{{}}{{}}{{CONT}}:{{BRIGHT}}{{fore(BLUE)}}{event.function} {{KIND}}<= {{BRIGHT}}{{fore(YELLOW)}}no frames available {{NORMAL}}(detached={event.detached})'
template += '{RESET}\n'
self.output(
template,
Expand Down
8 changes: 2 additions & 6 deletions src/hunter/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,8 @@ def __init__(
self.detached = False

def __repr__(self):
return '<Event kind={!r} function={!r} module={!r} filename={!r} lineno={}>'.format(
self.kind,
self.function,
self.module,
self.filename,
self.lineno,
return (
f'<Event kind={self.kind!r} function={self.function!r} module={self.module!r} filename={self.filename!r} lineno={self.lineno}>'
)

def __eq__(self, other):
Expand Down
43 changes: 11 additions & 32 deletions src/hunter/predicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def __init__(self, **query):
self.query_gte = tuple(sorted(query_gte.items()))

def __str__(self):
return 'Query(%s)' % (
return 'Query(%s)' % ( # noqa: UP031
', '.join(
', '.join(f'{key}{kind}={value!r}' for key, value in mapping)
for kind, mapping in [
Expand All @@ -159,7 +159,7 @@ def __str__(self):
)

def __repr__(self):
return '<hunter.predicates.Query: %s>' % ' '.join(
return '<hunter.predicates.Query: %s>' % ' '.join( # noqa: UP031
fmt % (mapping,)
for fmt, mapping in [
('query_eq=%r', self.query_eq),
Expand Down Expand Up @@ -289,10 +289,7 @@ def __str__(self):
)

def __repr__(self):
return '<hunter.predicates.When: condition={!r}, actions={!r}>'.format(
self.condition,
self.actions,
)
return f'<hunter.predicates.When: condition={self.condition!r}, actions={self.actions!r}>'

def __eq__(self, other):
return isinstance(other, When) and self.condition == other.condition and self.actions == other.actions
Expand Down Expand Up @@ -367,11 +364,7 @@ def __init__(self, condition, predicate=None, watermark=0):
self._origin_calls = None

def __str__(self):
return 'From({}, {}, watermark={})'.format(
self.condition,
self.predicate,
self.watermark,
)
return f'From({self.condition}, {self.predicate}, watermark={self.watermark})'

def __repr__(self):
return f'<hunter.predicates.From: condition={self.condition!r}, predicate={self.predicate!r}, watermark={self.watermark!r}>'
Expand Down Expand Up @@ -444,7 +437,7 @@ def __init__(self, *predicates):
self.predicates = predicates

def __str__(self):
return 'And(%s)' % ', '.join(str(p) for p in self.predicates)
return f'And({", ".join(str(p) for p in self.predicates)})'

def __repr__(self):
return f'<hunter.predicates.And: predicates={self.predicates!r}>'
Expand Down Expand Up @@ -507,7 +500,7 @@ def __init__(self, *predicates):
self.predicates = predicates

def __str__(self):
return 'Or(%s)' % ', '.join(str(p) for p in self.predicates)
return f'Or({", ".join(str(p) for p in self.predicates)})'

def __repr__(self):
return f'<hunter.predicates.Or: predicates={self.predicates!r}>'
Expand Down Expand Up @@ -570,10 +563,10 @@ def __init__(self, predicate):
self.predicate = predicate

def __str__(self):
return 'Not(%s)' % self.predicate
return f'Not({self.predicate})'

def __repr__(self):
return '<hunter.predicates.Not: predicate=%r>' % self.predicate
return f'<hunter.predicates.Not: predicate={self.predicate!r}>'

def __eq__(self, other):
return isinstance(other, Not) and self.predicate == other.predicate
Expand Down Expand Up @@ -669,7 +662,7 @@ def __init__(
):
self.action = action() if inspect.isclass(action) and issubclass(action, Action) else action
if not isinstance(self.action, ColorStreamAction):
raise TypeError('Action %r must be a ColorStreamAction.' % self.action)
raise TypeError(f'Action {self.action!r} must be a ColorStreamAction.')
self.condition = condition
self.queue = collections.deque(maxlen=size)
self.size = size
Expand Down Expand Up @@ -744,24 +737,10 @@ def __call__(self, event):
return result

def __str__(self):
return 'Backlog({}, size={}, stack={}, vars={}, action={}, filter={})'.format(
self.condition,
self.size,
self.stack,
self.vars,
self.action,
self._filter,
)
return f'Backlog({self.condition}, size={self.size}, stack={self.stack}, vars={self.vars}, action={self.action}, filter={self._filter})'

def __repr__(self):
return '<hunter.predicates.Backlog: condition={!r}, size={!r}, stack={!r}, vars={!r}, action={!r}, filter={!r}>'.format(
self.condition,
self.size,
self.stack,
self.vars,
self.action,
self._filter,
)
return f'<hunter.predicates.Backlog: condition={self.condition!r}, size={self.size!r}, stack={self.stack!r}, vars={self.vars!r}, action={self.action!r}, filter={self._filter!r}>'

def __eq__(self, other):
return (
Expand Down
Loading

0 comments on commit 5c463b0

Please sign in to comment.