2
2
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
3
3
4
4
import collections
5
- from typing import Any , NamedTuple
5
+ from typing import Any , NamedTuple , Optional , Sequence , Tuple , Union
6
+
7
+ from astroid import nodes
6
8
7
- from pylint import interfaces
8
9
from pylint .constants import PY38_PLUS
10
+ from pylint .interfaces import HIGH , UNDEFINED , Confidence
11
+ from pylint .message .message import Message
9
12
from pylint .testutils .constants import UPDATE_OPTION
10
13
11
14
@@ -16,19 +19,30 @@ class MessageTest(
16
19
):
17
20
"""Used to test messages produced by pylint. Class name cannot start with Test as pytest doesn't allow constructors in test classes."""
18
21
19
- def __new__ (cls , msg_id , line = None , node = None , args = None , confidence = None ):
22
+ def __new__ (
23
+ cls ,
24
+ msg_id : str ,
25
+ line : Optional [int ] = None ,
26
+ node : Optional [nodes .NodeNG ] = None ,
27
+ args : Any = None ,
28
+ confidence : Optional [Confidence ] = None ,
29
+ ) -> "MessageTest" :
20
30
return tuple .__new__ (cls , (msg_id , line , node , args , confidence ))
21
31
22
- def __eq__ (self , other ) :
32
+ def __eq__ (self , other : object ) -> bool :
23
33
if isinstance (other , MessageTest ):
24
34
if self .confidence and other .confidence :
25
35
return super ().__eq__ (other )
26
- return self [:- 1 ] == other [:- 1 ]
36
+ return tuple ( self [:- 1 ]) == tuple ( other [:- 1 ])
27
37
return NotImplemented # pragma: no cover
28
38
29
39
30
40
class MalformedOutputLineException (Exception ):
31
- def __init__ (self , row , exception ):
41
+ def __init__ (
42
+ self ,
43
+ row : Union [Sequence [str ], str ],
44
+ exception : Exception ,
45
+ ) -> None :
32
46
example = "msg-symbolic-name:42:27:MyClass.my_function:The message"
33
47
other_example = "msg-symbolic-name:7:42::The message"
34
48
expected = [
@@ -62,39 +76,42 @@ def __init__(self, row, exception):
62
76
class OutputLine (NamedTuple ):
63
77
symbol : str
64
78
lineno : int
65
- column : str
66
- object : Any
79
+ column : int
80
+ object : str
67
81
msg : str
68
- confidence : interfaces . Confidence
82
+ confidence : str
69
83
70
84
@classmethod
71
- def from_msg (cls , msg ) :
72
- column = cls .get_column (msg .column )
85
+ def from_msg (cls , msg : Message ) -> "OutputLine" :
86
+ column = cls ._get_column (msg .column )
73
87
return cls (
74
88
msg .symbol ,
75
89
msg .line ,
76
90
column ,
77
91
msg .obj or "" ,
78
92
msg .msg .replace ("\r \n " , "\n " ),
79
- msg .confidence .name
80
- if msg .confidence != interfaces .UNDEFINED
81
- else interfaces .HIGH .name ,
93
+ msg .confidence .name if msg .confidence != UNDEFINED else HIGH .name ,
82
94
)
83
95
84
- @classmethod
85
- def get_column ( cls , column ) :
96
+ @staticmethod
97
+ def _get_column ( column : str ) -> int :
86
98
if not PY38_PLUS :
87
- return "" # pragma: no cover
88
- return str (column )
99
+ # We check the column only for the new better ast parser introduced in python 3.8
100
+ return 0 # pragma: no cover
101
+ return int (column )
89
102
90
103
@classmethod
91
- def from_csv (cls , row ) :
104
+ def from_csv (cls , row : Union [ Sequence [ str ], str ]) -> "OutputLine" :
92
105
try :
93
- confidence = row [5 ] if len (row ) == 6 else interfaces .HIGH .name
94
- column = cls .get_column (row [2 ])
95
- return cls (row [0 ], int (row [1 ]), column , row [3 ], row [4 ], confidence )
106
+ if isinstance (row , Sequence ):
107
+ column = cls ._get_column (row [2 ])
108
+ if len (row ) == 5 :
109
+ return cls (row [0 ], int (row [1 ]), column , row [3 ], row [4 ], HIGH .name )
110
+ if len (row ) == 6 :
111
+ return cls (row [0 ], int (row [1 ]), column , row [3 ], row [4 ], row [5 ])
112
+ raise IndexError
96
113
except Exception as e :
97
114
raise MalformedOutputLineException (row , e ) from e
98
115
99
- def to_csv (self ):
100
- return tuple (self )
116
+ def to_csv (self ) -> Tuple [ str , str , str , str , str , str ] :
117
+ return tuple (str ( i ) for i in self ) # type: ignore # pylint: disable=not-an-iterable
0 commit comments