Skip to content

Commit 3b32469

Browse files
committed
[refactor] Condition by dict
1 parent 3e1664a commit 3b32469

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

src/kotoha/core.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,32 @@
2929

3030

3131
class ArgumentConcreteTypeHintChecker(ast.NodeVisitor):
32+
_concrete_type_hint_error_codes: dict[str, ErrorMessage] = {
33+
"list": KTH101,
34+
"tuple": KTH102,
35+
"set": KTH103,
36+
"dict": KTH104,
37+
}
38+
3239
def __init__(self) -> None:
3340
self.errors: list[tuple[LineNumber, ColumnOffset, ErrorMessage]] = []
3441

3542
def visit_arg(self, node: ast.arg) -> None:
3643
if node.annotation is not None:
3744
annotation: ast.expr = node.annotation
38-
if hasattr(annotation, "value"):
39-
if annotation.value.id == "list":
40-
self.errors.append((node.lineno, node.col_offset, KTH101))
41-
elif annotation.value.id == "tuple":
42-
self.errors.append((node.lineno, node.col_offset, KTH102))
43-
elif annotation.value.id == "set":
44-
self.errors.append((node.lineno, node.col_offset, KTH103))
45-
elif annotation.value.id == "dict":
46-
self.errors.append((node.lineno, node.col_offset, KTH104))
45+
if (
46+
hasattr(annotation, "value")
47+
and annotation.value.id in self._concrete_type_hint_error_codes
48+
):
49+
self.errors.append(
50+
(
51+
node.lineno,
52+
node.col_offset,
53+
self._concrete_type_hint_error_codes[
54+
annotation.value.id
55+
],
56+
)
57+
)
4758
self.generic_visit(node)
4859

4960

0 commit comments

Comments
 (0)