Skip to content

Commit

Permalink
Fix #34 (ignore marked lines)
Browse files Browse the repository at this point in the history
Update tests
  • Loading branch information
mwermelinger committed Jan 10, 2024
1 parent 7983a53 commit 2b6c6c6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
13 changes: 11 additions & 2 deletions allowed.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,13 @@ def location(line: int, line_cell_map: list) -> tuple[int, int]:
return line_cell_map[line] if line_cell_map else (0, line)


def ignore(node: ast.AST, source: list[str]) -> bool:
"""Return True if node is on a line to be ignored."""
return hasattr(node, "lineno") and source[node.lineno - 1].rstrip().endswith(
"# allowed"
)


# ----- main functions -----


Expand All @@ -389,9 +396,11 @@ def check_tree(
"""Check if tree only uses allowed constructs. Add violations to errors."""
language, options, imports, functions, methods = constructs
for node in ast.walk(tree):
# if a node has no line number, handle it via its parent
# If a node has no line number, handle it via its parent.
if isinstance(node, NO_LINE):
pass
elif ignore(node, source):
pass
elif isinstance(node, (ast.BinOp, ast.UnaryOp, ast.BoolOp)):
if not isinstance(node.op, language):
cell, line = location(node.lineno, line_cell_map)
Expand All @@ -408,7 +417,7 @@ def check_tree(
cell, line = location(node.lineno, line_cell_map)
message = CONCRETE.get(type(node), "unknown construct")
else:
# if a node has no line number, report it for inclusion in NO_LINE
# If a node has no line number, report it for inclusion in NO_LINE.
cell, line = 0, 0
message = f"unknown construct {str(node)} at unknown line"
errors.append((cell, line, message))
Expand Down
12 changes: 6 additions & 6 deletions sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
f = 9e5 + 8.3e-2 - 7_000 * 6 / -5 // 4 % 3**2
1 << 3
abs(min(1, max(2, 3)))
f.is_integer()
f.is_integer() # allowed

# Booleans
True and False or not True
Expand All @@ -30,7 +30,7 @@
s.upper()

# Lists
l = [] + list("abc")
l = [] + list("abc") + [letter for letter in "abc"]
l.sort(reverse=True)
l.append(1)
l.pop()
Expand All @@ -41,11 +41,11 @@

# Operations common to all sequences
t[2] not in l[:3:-1] * 2
t.index("a")
l.count(None)
# The following allows all constructs on the same line.
l.count(t.index(1)) # allowed

# Sets
items = set() | {1, 2, 3} & {2, 3} ^ {3, 4} - {4, 5}
items = set() | {1, 2, 3} & {2, 3} ^ {3, 4} - {i for i in range(1, 10, 2)}
items.add(items.pop())
items.discard(9)
items.union(l)
Expand All @@ -58,7 +58,7 @@
print(key, value)

# Control flow statements
try:
try: # allowed
for i in range(1, 5):
while i < 6:
if i == 0:
Expand Down
6 changes: 2 additions & 4 deletions tests/sample-py-m.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
sample.py:8: types
sample.py:9: choice
sample.py:15: <<
sample.py:17: float.is_integer()
sample.py:22: if expression
sample.py:29: f-string
sample.py:30: str.upper()
sample.py:44: tuple.index()
sample.py:45: list.count()
sample.py:33: list comprehension
sample.py:48: ^
sample.py:61: try
sample.py:48: set comprehension
sample.py:67: break
sample.py:70: while-else
sample.py:71: continue
Expand Down
3 changes: 2 additions & 1 deletion tests/sample-py.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ sample.py:9: choice
sample.py:15: <<
sample.py:22: if expression
sample.py:29: f-string
sample.py:33: list comprehension
sample.py:48: ^
sample.py:61: try
sample.py:48: set comprehension
sample.py:67: break
sample.py:70: while-else
sample.py:71: continue
Expand Down

0 comments on commit 2b6c6c6

Please sign in to comment.