7
7
import sys
8
8
from collections import Counter
9
9
from io import StringIO , TextIOWrapper
10
- from typing import TYPE_CHECKING , Dict , List , Optional , Tuple
10
+ from typing import TYPE_CHECKING , Dict , List , Optional , TextIO , Tuple , Union
11
11
12
12
import pytest
13
13
from _pytest .config import Config
@@ -53,7 +53,7 @@ def __init__(self, test_file: FunctionalTestFile, config: Optional[Config] = Non
53
53
self ._test_file = test_file
54
54
self ._config = config
55
55
56
- def setUp (self ):
56
+ def setUp (self ) -> None :
57
57
if self ._should_be_skipped_due_to_version ():
58
58
pytest .skip (
59
59
f"Test cannot run with Python { sys .version .split (' ' , maxsplit = 1 )[0 ]} ."
@@ -78,10 +78,10 @@ def setUp(self):
78
78
if sys .platform .lower () in platforms :
79
79
pytest .skip (f"Test cannot run on platform { sys .platform !r} " )
80
80
81
- def runTest (self ):
81
+ def runTest (self ) -> None :
82
82
self ._runTest ()
83
83
84
- def _should_be_skipped_due_to_version (self ):
84
+ def _should_be_skipped_due_to_version (self ) -> bool :
85
85
return (
86
86
sys .version_info < self ._test_file .options ["min_pyver" ]
87
87
or sys .version_info > self ._test_file .options ["max_pyver" ]
@@ -141,21 +141,21 @@ def multiset_difference(
141
141
return missing , unexpected
142
142
143
143
# pylint: disable=consider-using-with
144
- def _open_expected_file (self ):
144
+ def _open_expected_file (self ) -> Union [ StringIO , TextIOWrapper , TextIO ] :
145
145
try :
146
146
return open (self ._test_file .expected_output , encoding = "utf-8" )
147
147
except FileNotFoundError :
148
148
return StringIO ("" )
149
149
150
150
# pylint: disable=consider-using-with
151
- def _open_source_file (self ):
151
+ def _open_source_file (self ) -> TextIO :
152
152
if self ._test_file .base == "invalid_encoded_data" :
153
153
return open (self ._test_file .source , encoding = "utf-8" )
154
154
if "latin1" in self ._test_file .base :
155
155
return open (self ._test_file .source , encoding = "latin1" )
156
156
return open (self ._test_file .source , encoding = "utf8" )
157
157
158
- def _get_expected (self ):
158
+ def _get_expected (self ) -> Tuple [ Counter , List [ OutputLine ]] :
159
159
with self ._open_source_file () as f :
160
160
expected_msgs = self .get_expected_messages (f )
161
161
if not expected_msgs :
@@ -166,10 +166,10 @@ def _get_expected(self):
166
166
]
167
167
return expected_msgs , expected_output_lines
168
168
169
- def _get_actual (self ):
169
+ def _get_actual (self ) -> Tuple [ Counter , List [ OutputLine ]] :
170
170
messages = self ._linter .reporter .messages
171
171
messages .sort (key = lambda m : (m .line , m .symbol , m .msg ))
172
- received_msgs = Counter ()
172
+ received_msgs : Counter = Counter ()
173
173
received_output_lines = []
174
174
for msg in messages :
175
175
assert (
@@ -179,7 +179,7 @@ def _get_actual(self):
179
179
received_output_lines .append (OutputLine .from_msg (msg ))
180
180
return received_msgs , received_output_lines
181
181
182
- def _runTest (self ):
182
+ def _runTest (self ) -> None :
183
183
__tracebackhide__ = True # pylint: disable=unused-variable
184
184
modules_to_check = [self ._test_file .source ]
185
185
self ._linter .check (modules_to_check )
@@ -234,7 +234,12 @@ def error_msg_for_unequal_output(self, expected_lines, received_lines) -> str:
234
234
error_msg += f"{ line } \n "
235
235
return error_msg
236
236
237
- def _check_output_text (self , _ , expected_output , actual_output ):
237
+ def _check_output_text (
238
+ self ,
239
+ _ : Counter ,
240
+ expected_output : List [OutputLine ],
241
+ actual_output : List [OutputLine ],
242
+ ) -> None :
238
243
"""This is a function because we want to be able to update the text in LintModuleOutputUpdate"""
239
244
assert expected_output == actual_output , self .error_msg_for_unequal_output (
240
245
expected_output , actual_output
0 commit comments