|
2 | 2 | import logging |
3 | 3 | import re |
4 | 4 | from contextlib import contextmanager |
| 5 | +from typing import AbstractSet |
| 6 | +from typing import Dict |
| 7 | +from typing import List |
| 8 | +from typing import Mapping |
5 | 9 |
|
6 | 10 | import py |
7 | 11 |
|
@@ -32,14 +36,15 @@ class ColoredLevelFormatter(logging.Formatter): |
32 | 36 | logging.INFO: {"green"}, |
33 | 37 | logging.DEBUG: {"purple"}, |
34 | 38 | logging.NOTSET: set(), |
35 | | - } |
| 39 | + } # type: Mapping[int, AbstractSet[str]] |
36 | 40 | LEVELNAME_FMT_REGEX = re.compile(r"%\(levelname\)([+-.]?\d*s)") |
37 | 41 |
|
38 | | - def __init__(self, terminalwriter, *args, **kwargs): |
| 42 | + def __init__(self, terminalwriter, *args, **kwargs) -> None: |
39 | 43 | super().__init__(*args, **kwargs) |
40 | 44 | self._original_fmt = self._style._fmt |
41 | | - self._level_to_fmt_mapping = {} |
| 45 | + self._level_to_fmt_mapping = {} # type: Dict[int, str] |
42 | 46 |
|
| 47 | + assert self._fmt is not None |
43 | 48 | levelname_fmt_match = self.LEVELNAME_FMT_REGEX.search(self._fmt) |
44 | 49 | if not levelname_fmt_match: |
45 | 50 | return |
@@ -216,31 +221,31 @@ def catching_logs(handler, formatter=None, level=None): |
216 | 221 | class LogCaptureHandler(logging.StreamHandler): |
217 | 222 | """A logging handler that stores log records and the log text.""" |
218 | 223 |
|
219 | | - def __init__(self): |
| 224 | + def __init__(self) -> None: |
220 | 225 | """Creates a new log handler.""" |
221 | 226 | logging.StreamHandler.__init__(self, py.io.TextIO()) |
222 | | - self.records = [] |
| 227 | + self.records = [] # type: List[logging.LogRecord] |
223 | 228 |
|
224 | | - def emit(self, record): |
| 229 | + def emit(self, record: logging.LogRecord) -> None: |
225 | 230 | """Keep the log records in a list in addition to the log text.""" |
226 | 231 | self.records.append(record) |
227 | 232 | logging.StreamHandler.emit(self, record) |
228 | 233 |
|
229 | | - def reset(self): |
| 234 | + def reset(self) -> None: |
230 | 235 | self.records = [] |
231 | 236 | self.stream = py.io.TextIO() |
232 | 237 |
|
233 | 238 |
|
234 | 239 | class LogCaptureFixture: |
235 | 240 | """Provides access and control of log capturing.""" |
236 | 241 |
|
237 | | - def __init__(self, item): |
| 242 | + def __init__(self, item) -> None: |
238 | 243 | """Creates a new funcarg.""" |
239 | 244 | self._item = item |
240 | 245 | # dict of log name -> log level |
241 | | - self._initial_log_levels = {} # Dict[str, int] |
| 246 | + self._initial_log_levels = {} # type: Dict[str, int] |
242 | 247 |
|
243 | | - def _finalize(self): |
| 248 | + def _finalize(self) -> None: |
244 | 249 | """Finalizes the fixture. |
245 | 250 |
|
246 | 251 | This restores the log levels changed by :meth:`set_level`. |
@@ -453,7 +458,7 @@ def _create_formatter(self, log_format, log_date_format): |
453 | 458 | ): |
454 | 459 | formatter = ColoredLevelFormatter( |
455 | 460 | create_terminal_writer(self._config), log_format, log_date_format |
456 | | - ) |
| 461 | + ) # type: logging.Formatter |
457 | 462 | else: |
458 | 463 | formatter = logging.Formatter(log_format, log_date_format) |
459 | 464 |
|
|
0 commit comments