File tree Expand file tree Collapse file tree 2 files changed +22
-4
lines changed
Expand file tree Collapse file tree 2 files changed +22
-4
lines changed Original file line number Diff line number Diff line change 11import ast
2- from typing import cast
32
43KTH000 = (
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
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments