Skip to content

Commit c84360d

Browse files
committed
Miscellaneous linting
1 parent 50067e0 commit c84360d

File tree

5 files changed

+46
-37
lines changed

5 files changed

+46
-37
lines changed

trepan/lib/breakpoint.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from collections import defaultdict
1111
from types import CodeType, ModuleType
1212
from typing import Optional
13+
from types import FrameType
1314
from xdis import load_module
1415

1516

@@ -25,13 +26,13 @@ class Breakpoint:
2526

2627
def __init__(
2728
self,
28-
number,
29-
filename,
30-
line,
29+
number: int,
30+
filename: str,
31+
line: int,
3132
temporary=False,
3233
condition=None,
3334
code=None,
34-
offset=None,
35+
offset: Optional[int] =None,
3536
):
3637
self.offset = offset
3738
self.condition = condition
@@ -370,7 +371,7 @@ def reset(self):
370371
pass # BreakpointManager
371372

372373

373-
def checkfuncname(brkpt: Breakpoint, frame):
374+
def checkfuncname(brkpt: Breakpoint, frame: FrameType):
374375
"""
375376
Check whether we should break at `frame` because the frame's
376377
code object matches `brkpt.code`.

trepan/lib/deparse.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
from xdis import PYTHON_VERSION_TRIPLE
1212

1313
if PYTHON_VERSION_TRIPLE >= (3, 9):
14-
pass
14+
def deparse_and_cache(_co, _errmsg_fn, _tempdir=None):
15+
print("Derparsing is not available for Python >= 3.9")
16+
1517
else:
1618
if (3, 7) <= PYTHON_VERSION_TRIPLE:
1719
try:
@@ -93,6 +95,7 @@ def deparse_offset(co, name: str, last_i: int, errmsg_fn: Callable) -> tuple:
9395

9496
# Demo it
9597
if __name__ == "__main__":
98+
9699
import inspect
97100

98101
def msg(msg_str):
@@ -104,8 +107,9 @@ def errmsg(msg_str):
104107
return
105108

106109
curframe = inspect.currentframe()
107-
mapped_name, name_for_code = deparse_and_cache(curframe.f_code, errmsg)
108-
print(pyficache.getline(mapped_name, 7))
110+
if curframe is not None:
111+
mapped_name, name_for_code = deparse_and_cache(curframe.f_code, errmsg)
112+
print(pyficache.getline(mapped_name, 7))
109113
# mapped_name, name_for_code = deparse_offset(curframe.f_code,
110114
# curframe.f_code.co_name,
111115
# curframe.f_lasti, errmsg)

trepan/lib/format.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
# Copyright (C) 2013, 2015, 2017, 2019, 2020, 2023-2024
3+
# Copyright (C) 2013, 2015, 2017, 2019, 2020, 2023-2025
44
# Rocky Bernstein <[email protected]>
55
#
66
# This program is free software: you can redistribute it and/or modify
@@ -18,15 +18,18 @@
1818
"""Pygments-related terminal formatting"""
1919

2020

21-
import pyficache
21+
import pyficache # type: ignore
2222
import re
23+
import sys
2324

2425
from pygments import format, highlight, lex
2526
from pygments.console import ansiformat
2627
from pygments.filter import Filter
2728
from pygments.formatter import Formatter
2829
from pygments.formatters import Terminal256Formatter, TerminalFormatter
29-
from pygments.formatters.terminal import TERMINAL_COLORS
30+
31+
32+
from pygments.formatters.terminal import TERMINAL_COLORS # type: ignore
3033
from pygments.lexers import PythonLexer, RstLexer
3134
from pygments.token import (
3235
Comment,
@@ -409,6 +412,7 @@ def format_python(python_str: str, style) -> str:
409412
terminal_formatter = Terminal256Formatter(style=style)
410413
return highlight(python_str, python_lexer, terminal_formatter)
411414

415+
412416
def rst_text(text, mono, width=80):
413417
if mono:
414418
tf = mono_tf

trepan/lib/printing.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
#
3-
# Copyright (C) 2007-2010, 2015, 2020, 2023-2024
3+
# Copyright (C) 2007-2010, 2015, 2020, 2023-2025
44
# Rocky Bernstein
55
#
66
# This program is free software: you can redistribute it and/or modify
@@ -17,22 +17,14 @@
1717
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1818

1919
import inspect
20+
import pprint
2021
import types
2122
from typing import Optional
2223

2324

2425
def print_dict(s, obj, title) -> str:
25-
if hasattr(obj, "__dict__"):
26-
obj = obj.__dict__
27-
pass
28-
if isinstance(obj, dict):
29-
s += f"\n{title}:\n"
30-
keys = list(obj.keys())
31-
keys.sort()
32-
for key in keys:
33-
s += f" {repr(key)}:\t{obj[key]}\n"
34-
pass
35-
pass
26+
s += f"\n{title}:\n"
27+
s += pprint.pformat(obj)
3628
return s
3729

3830

trepan/lib/stack.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22

33
# Copyright (C) 2008-2010, 2013, 2015, 2017-2018, 2020-2021,
4-
# 2023-2024 Rocky Bernstein <[email protected]>
4+
# 2023-2025 Rocky Bernstein <[email protected]>
55
#
66
# This program is free software: you can redistribute it and/or modify
77
# it under the terms of the GNU General Public License as published by
@@ -25,6 +25,7 @@
2525
import re
2626
from opcode import opname
2727
from reprlib import repr
28+
from types import FrameType
2829
from typing import Optional, Tuple
2930

3031
import xdis
@@ -55,20 +56,25 @@ def deparse_fn(code):
5556
have_deparser = True
5657
except ImportError:
5758

58-
def deparse_offset(code, name: str, list_i: int, _) -> tuple:
59+
def deparse_offset(_code, _name: str, _list_i: int, _) -> tuple:
5960
return None, None
6061

6162
have_deparser = False
6263

6364
_with_local_varname = re.compile(r"_\[[0-9+]]")
6465

6566

66-
def count_frames(frame, count_start=0):
67+
def count_frames(frame: FrameType, count_start=0):
6768
"""Return a count of the number of frames"""
6869
count = -count_start
69-
while frame:
70-
count += 1
71-
frame = frame.f_back
70+
for _ in range(1000):
71+
if frame is None:
72+
break
73+
else:
74+
count += 1
75+
frame = frame.f_back
76+
else:
77+
return 1000
7278
return count
7379

7480

@@ -119,7 +125,7 @@ def deparse_source_from_code(code):
119125
return source_text
120126

121127

122-
def format_function_name(frame, style: str) -> Tuple[Optional[str], Optional[str]]:
128+
def format_function_name(frame: FrameType, style: str) -> Tuple[Optional[str], Optional[str]]:
123129
"""
124130
Pick out the function name from ``frame`` and return both the name
125131
and the name styled according to ``style``
@@ -139,7 +145,7 @@ def format_function_name(frame, style: str) -> Tuple[Optional[str], Optional[str
139145
return funcname, format_token(Function, funcname, style=style)
140146

141147

142-
def format_function_and_parameters(frame, debugger, style: str) -> Tuple[bool, str]:
148+
def format_function_and_parameters(frame: FrameType, debugger, style: str) -> Tuple[bool, str]:
143149
""" """
144150

145151
funcname, s = format_function_name(frame, style)
@@ -279,16 +285,18 @@ def frame2filesize(frame):
279285
bc_path = None
280286
path = frame.f_globals["__file__"]
281287
source_path = getsourcefile(path)
288+
if source_path is None:
289+
return None, None
282290
fs_size = os.stat(source_path).st_size
283291
if bc_path:
284292
(
285-
version,
286-
timestamp,
287-
magic_int,
288-
co,
289-
is_pypy,
293+
_version,
294+
_timestamp,
295+
_magic_int,
296+
_co,
297+
_is_pypy,
290298
bc_source_size,
291-
sip_hash,
299+
_sip_hash,
292300
) = xdis.load_module(bc_path, fast_load=True, get_code=False)
293301
return fs_size, bc_source_size
294302
elif osp.exists(path):

0 commit comments

Comments
 (0)