Skip to content

Commit 8097080

Browse files
committed
[bugfix] Fix unannotated parameter case
1 parent 2a9797d commit 8097080

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/kotoha/core.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import ast
2-
from typing import cast
32

43
KTH000 = (
54
"KTH000 "
@@ -14,9 +13,15 @@ def __init__(self) -> None:
1413
self.errors: list[tuple[int, int, str]] = []
1514

1615
def visit_arg(self, node: ast.arg) -> None:
17-
annotation = cast(ast.Constant, node.annotation)
18-
if annotation.value.id in {"list", "dict", "set", "tuple"}:
19-
self.errors.append((node.lineno, node.col_offset, KTH000))
16+
if node.annotation is not None:
17+
annotation: ast.expr = node.annotation
18+
if hasattr(annotation, "value") and annotation.value.id in {
19+
"list",
20+
"dict",
21+
"set",
22+
"tuple",
23+
}:
24+
self.errors.append((node.lineno, node.col_offset, KTH000))
2025
self.generic_visit(node)
2126

2227

tests/test_core.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,16 @@ def plus_one_ok(numbers: Iterable[int]) -> list[int]:
2929
assert len(checker.errors) == 1
3030
assert checker.errors[0] == (6, 16, ANY)
3131
assert checker.errors[0][2].startswith("KTH000")
32+
33+
def test_KTH000_none_annotation(self):
34+
code = dedent(
35+
"""\
36+
def plus_one_ng(numbers: list[int], dummy) -> list[int]:
37+
return [n + 1 for n in numbers]
38+
"""
39+
)
40+
41+
checker = ArgumentConcreteTypeHintChecker()
42+
checker.visit(ast.parse(code))
43+
44+
assert len(checker.errors) == 1

0 commit comments

Comments
 (0)