Skip to content

Commit 7e69ce7

Browse files
committed
Add full type annotations to logging/test_fixture.py
1 parent 395bbae commit 7e69ce7

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

testing/logging/test_fixture.py

+30-17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# mypy: disable-error-code="attr-defined"
2+
# mypy: disallow-untyped-defs
23
import logging
34
from typing import Iterator
45

@@ -28,7 +29,7 @@ def test_fixture_help(pytester: Pytester) -> None:
2829
result.stdout.fnmatch_lines(["*caplog*"])
2930

3031

31-
def test_change_level(caplog):
32+
def test_change_level(caplog: pytest.LogCaptureFixture) -> None:
3233
caplog.set_level(logging.INFO)
3334
logger.debug("handler DEBUG level")
3435
logger.info("handler INFO level")
@@ -43,7 +44,7 @@ def test_change_level(caplog):
4344
assert "CRITICAL" in caplog.text
4445

4546

46-
def test_change_level_logging_disabled(caplog):
47+
def test_change_level_logging_disabled(caplog: pytest.LogCaptureFixture) -> None:
4748
logging.disable(logging.CRITICAL)
4849
assert logging.root.manager.disable == logging.CRITICAL
4950
caplog.set_level(logging.WARNING)
@@ -143,7 +144,7 @@ def test3(caplog):
143144
result.assert_outcomes(passed=3)
144145

145146

146-
def test_with_statement(caplog):
147+
def test_with_statement(caplog: pytest.LogCaptureFixture) -> None:
147148
with caplog.at_level(logging.INFO):
148149
logger.debug("handler DEBUG level")
149150
logger.info("handler INFO level")
@@ -158,7 +159,7 @@ def test_with_statement(caplog):
158159
assert "CRITICAL" in caplog.text
159160

160161

161-
def test_with_statement_logging_disabled(caplog):
162+
def test_with_statement_logging_disabled(caplog: pytest.LogCaptureFixture) -> None:
162163
logging.disable(logging.CRITICAL)
163164
assert logging.root.manager.disable == logging.CRITICAL
164165
with caplog.at_level(logging.WARNING):
@@ -196,7 +197,9 @@ def test_with_statement_logging_disabled(caplog):
196197
("NOTVALIDLEVEL", logging.NOTSET),
197198
],
198199
)
199-
def test_force_enable_logging_level_string(caplog, level_str, expected_disable_level):
200+
def test_force_enable_logging_level_string(
201+
caplog: pytest.LogCaptureFixture, level_str: str, expected_disable_level: int
202+
) -> None:
200203
"""Test _force_enable_logging using a level string.
201204
202205
``expected_disable_level`` is one level below ``level_str`` because the disabled log level
@@ -215,15 +218,15 @@ def test_force_enable_logging_level_string(caplog, level_str, expected_disable_l
215218
assert test_logger.manager.disable == expected_disable_level
216219

217220

218-
def test_log_access(caplog):
221+
def test_log_access(caplog: pytest.LogCaptureFixture) -> None:
219222
caplog.set_level(logging.INFO)
220223
logger.info("boo %s", "arg")
221224
assert caplog.records[0].levelname == "INFO"
222225
assert caplog.records[0].msg == "boo %s"
223226
assert "boo arg" in caplog.text
224227

225228

226-
def test_messages(caplog):
229+
def test_messages(caplog: pytest.LogCaptureFixture) -> None:
227230
caplog.set_level(logging.INFO)
228231
logger.info("boo %s", "arg")
229232
logger.info("bar %s\nbaz %s", "arg1", "arg2")
@@ -244,22 +247,22 @@ def test_messages(caplog):
244247
assert "Exception" not in caplog.messages[-1]
245248

246249

247-
def test_record_tuples(caplog):
250+
def test_record_tuples(caplog: pytest.LogCaptureFixture) -> None:
248251
caplog.set_level(logging.INFO)
249252
logger.info("boo %s", "arg")
250253

251254
assert caplog.record_tuples == [(__name__, logging.INFO, "boo arg")]
252255

253256

254-
def test_unicode(caplog):
257+
def test_unicode(caplog: pytest.LogCaptureFixture) -> None:
255258
caplog.set_level(logging.INFO)
256259
logger.info("bū")
257260
assert caplog.records[0].levelname == "INFO"
258261
assert caplog.records[0].msg == "bū"
259262
assert "bū" in caplog.text
260263

261264

262-
def test_clear(caplog):
265+
def test_clear(caplog: pytest.LogCaptureFixture) -> None:
263266
caplog.set_level(logging.INFO)
264267
logger.info("bū")
265268
assert len(caplog.records)
@@ -270,15 +273,19 @@ def test_clear(caplog):
270273

271274

272275
@pytest.fixture
273-
def logging_during_setup_and_teardown(caplog):
276+
def logging_during_setup_and_teardown(
277+
caplog: pytest.LogCaptureFixture,
278+
) -> Iterator[None]:
274279
caplog.set_level("INFO")
275280
logger.info("a_setup_log")
276281
yield
277282
logger.info("a_teardown_log")
278283
assert [x.message for x in caplog.get_records("teardown")] == ["a_teardown_log"]
279284

280285

281-
def test_caplog_captures_for_all_stages(caplog, logging_during_setup_and_teardown):
286+
def test_caplog_captures_for_all_stages(
287+
caplog: pytest.LogCaptureFixture, logging_during_setup_and_teardown: None
288+
) -> None:
282289
assert not caplog.records
283290
assert not caplog.get_records("call")
284291
logger.info("a_call_log")
@@ -287,25 +294,31 @@ def test_caplog_captures_for_all_stages(caplog, logging_during_setup_and_teardow
287294
assert [x.message for x in caplog.get_records("setup")] == ["a_setup_log"]
288295

289296
# This reaches into private API, don't use this type of thing in real tests!
290-
assert set(caplog._item.stash[caplog_records_key]) == {"setup", "call"}
297+
caplog_records = caplog._item.stash[caplog_records_key]
298+
assert set(caplog_records) == {"setup", "call"}
291299

292300

293-
def test_clear_for_call_stage(caplog, logging_during_setup_and_teardown):
301+
def test_clear_for_call_stage(
302+
caplog: pytest.LogCaptureFixture, logging_during_setup_and_teardown: None
303+
) -> None:
294304
logger.info("a_call_log")
295305
assert [x.message for x in caplog.get_records("call")] == ["a_call_log"]
296306
assert [x.message for x in caplog.get_records("setup")] == ["a_setup_log"]
297-
assert set(caplog._item.stash[caplog_records_key]) == {"setup", "call"}
307+
caplog_records = caplog._item.stash[caplog_records_key]
308+
assert set(caplog_records) == {"setup", "call"}
298309

299310
caplog.clear()
300311

301312
assert caplog.get_records("call") == []
302313
assert [x.message for x in caplog.get_records("setup")] == ["a_setup_log"]
303-
assert set(caplog._item.stash[caplog_records_key]) == {"setup", "call"}
314+
caplog_records = caplog._item.stash[caplog_records_key]
315+
assert set(caplog_records) == {"setup", "call"}
304316

305317
logging.info("a_call_log_after_clear")
306318
assert [x.message for x in caplog.get_records("call")] == ["a_call_log_after_clear"]
307319
assert [x.message for x in caplog.get_records("setup")] == ["a_setup_log"]
308-
assert set(caplog._item.stash[caplog_records_key]) == {"setup", "call"}
320+
caplog_records = caplog._item.stash[caplog_records_key]
321+
assert set(caplog_records) == {"setup", "call"}
309322

310323

311324
def test_ini_controls_global_log_level(pytester: Pytester) -> None:

0 commit comments

Comments
 (0)