Skip to content

Commit 5fe048a

Browse files
authored
Limit code snippet length of generic renderer (#1267)
1 parent 529c5b0 commit 5fe048a

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
2020
- Added markdown rendering to display code in error messages
2121
- Improved RI checking to raise a warning when a `NameError` is raised and the missing name matches an instance attribute, and is due to an omitted `self.` in the RI.
2222
- Extended `AccumulationTable` class to support multiple loops in sequence within the same context manager
23+
- Added a solution to prevent possible large snippets created by the `render_generic` function
2324

2425
### 💫 New checkers
2526

python_ta/reporters/node_printers.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,25 @@ def render_generic(msg, node=None, source_lines=None, config=None):
3636
source_lines[start_line - 1],
3737
)
3838
else:
39-
yield (start_line, slice(start_col, None), LineType.ERROR, source_lines[start_line - 1])
40-
yield from (
41-
(line, slice(None, None), LineType.ERROR, source_lines[line - 1])
42-
for line in range(start_line + 1, end_line)
39+
num_lines = end_line - start_line
40+
half_threshold = MAX_SNIPPET_LINES // 2
41+
yield (
42+
start_line,
43+
slice(start_col, None),
44+
LineType.ERROR,
45+
source_lines[start_line - 1],
4346
)
47+
for line in range(start_line + 1, start_line + min(half_threshold, num_lines)):
48+
yield (line, slice(None, None), LineType.ERROR, source_lines[line - 1])
49+
50+
if end_line - start_line > MAX_SNIPPET_LINES:
51+
yield ("", slice(None, None), LineType.OTHER, "...")
52+
53+
for line in range(
54+
end_line - min(half_threshold, num_lines - half_threshold) + 1, end_line
55+
):
56+
yield (line, slice(None, None), LineType.ERROR, source_lines[line - 1])
57+
4458
yield (end_line, slice(None, end_col), LineType.ERROR, source_lines[end_line - 1])
4559

4660
# Display up to 2 lines after node for context:
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""Examples for e9920_unnecessary_f_string_checker to demonstrate reduction of snippet length"""
2+
3+
4+
def demo_function() -> str:
5+
"""
6+
Demonstrates e9920_unnecessary_f_string_checker
7+
"""
8+
x = "hello"
9+
a = f"{x}" # error on this line
10+
11+
b = f"{x =}" # no error on this line
12+
13+
c = f"{x + ' world'
14+
15+
16+
17+
18+
19+
+ 'hi'
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
}" # error on this line
30+
31+
return x + a + b + c

0 commit comments

Comments
 (0)