8
8
from typing import Generator
9
9
from typing import List
10
10
from typing import Mapping
11
+ from typing import Optional
11
12
12
13
import pytest
13
14
from _pytest import nodes
14
15
from _pytest .compat import nullcontext
15
16
from _pytest .config import _strtobool
17
+ from _pytest .config import Config
16
18
from _pytest .config import create_terminal_writer
17
19
from _pytest .pathlib import Path
18
20
@@ -448,17 +450,15 @@ def caplog(request):
448
450
result ._finalize ()
449
451
450
452
451
- def get_actual_log_level (config , * setting_names ):
452
- """Return the actual logging level."""
453
-
453
+ def get_log_level_for_setting (config : Config , * setting_names : str ) -> Optional [int ]:
454
454
for setting_name in setting_names :
455
455
log_level = config .getoption (setting_name )
456
456
if log_level is None :
457
457
log_level = config .getini (setting_name )
458
458
if log_level :
459
459
break
460
460
else :
461
- return
461
+ return None
462
462
463
463
if isinstance (log_level , str ):
464
464
log_level = log_level .upper ()
@@ -483,7 +483,7 @@ class LoggingPlugin:
483
483
"""Attaches to the logging module and captures log messages for each test.
484
484
"""
485
485
486
- def __init__ (self , config ) :
486
+ def __init__ (self , config : Config ) -> None :
487
487
"""Creates a new plugin to capture log messages.
488
488
489
489
The formatter can be safely shared across all handlers so
@@ -503,9 +503,9 @@ def __init__(self, config):
503
503
get_option_ini (config , "log_date_format" ),
504
504
get_option_ini (config , "log_auto_indent" ),
505
505
)
506
- self .log_level = get_actual_log_level (config , "log_level" )
506
+ self .log_level = get_log_level_for_setting (config , "log_level" )
507
507
508
- self .log_file_level = get_actual_log_level (config , "log_file_level" )
508
+ self .log_file_level = get_log_level_for_setting (config , "log_file_level" )
509
509
self .log_file_format = get_option_ini (config , "log_file_format" , "log_format" )
510
510
self .log_file_date_format = get_option_ini (
511
511
config , "log_file_date_format" , "log_date_format"
@@ -518,7 +518,7 @@ def __init__(self, config):
518
518
if log_file :
519
519
self .log_file_handler = logging .FileHandler (
520
520
log_file , mode = "w" , encoding = "UTF-8"
521
- )
521
+ ) # type: Optional[logging.FileHandler]
522
522
self .log_file_handler .setFormatter (self .log_file_formatter )
523
523
else :
524
524
self .log_file_handler = None
@@ -568,7 +568,7 @@ def _setup_cli_logging(self):
568
568
get_option_ini (config , "log_auto_indent" ),
569
569
)
570
570
571
- log_cli_level = get_actual_log_level (config , "log_cli_level" , "log_level" )
571
+ log_cli_level = get_log_level_for_setting (config , "log_cli_level" , "log_level" )
572
572
self .log_cli_handler = log_cli_handler
573
573
self .live_logs_context = lambda : catching_logs (
574
574
log_cli_handler , formatter = log_cli_formatter , level = log_cli_level
0 commit comments