diff options
Diffstat (limited to 'sphinx/util/logging.py')
-rw-r--r-- | sphinx/util/logging.py | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/sphinx/util/logging.py b/sphinx/util/logging.py index 09780723a..64b7d8fb4 100644 --- a/sphinx/util/logging.py +++ b/sphinx/util/logging.py @@ -12,7 +12,7 @@ import logging import logging.handlers from collections import defaultdict from contextlib import contextmanager -from typing import IO, Any, Dict, Generator, List, Tuple, Union +from typing import IO, TYPE_CHECKING, Any, Dict, Generator, List, Optional, Tuple, Type, Union from docutils import nodes from docutils.nodes import Node @@ -21,17 +21,14 @@ from docutils.utils import get_source_line from sphinx.errors import SphinxWarning from sphinx.util.console import colorize -if False: - # For type annotation - from typing import Type # for python3.5.1 - +if TYPE_CHECKING: from sphinx.application import Sphinx NAMESPACE = 'sphinx' VERBOSE = 15 -LEVEL_NAMES = defaultdict(lambda: logging.WARNING) # type: Dict[str, int] +LEVEL_NAMES: Dict[str, int] = defaultdict(lambda: logging.WARNING) LEVEL_NAMES.update({ 'CRITICAL': logging.CRITICAL, 'SEVERE': logging.CRITICAL, @@ -42,7 +39,7 @@ LEVEL_NAMES.update({ 'DEBUG': logging.DEBUG, }) -VERBOSITY_MAP = defaultdict(lambda: 0) # type: Dict[int, int] +VERBOSITY_MAP: Dict[int, int] = defaultdict(lambda: 0) VERBOSITY_MAP.update({ 0: logging.INFO, 1: VERBOSE, @@ -94,7 +91,7 @@ def convert_serializable(records: List[logging.LogRecord]) -> None: class SphinxLogRecord(logging.LogRecord): """Log record class supporting location""" prefix = '' - location = None # type: Any + location: Any = None def getMessage(self) -> str: message = super().getMessage() @@ -166,6 +163,8 @@ class NewLineStreamHandler(logging.StreamHandler): class MemoryHandler(logging.handlers.BufferingHandler): """Handler buffering all logs.""" + buffer: List[logging.LogRecord] + def __init__(self) -> None: super().__init__(-1) @@ -177,7 +176,7 @@ class MemoryHandler(logging.handlers.BufferingHandler): try: for record in self.buffer: logger.handle(record) - self.buffer = [] # type: List[logging.LogRecord] + self.buffer = [] finally: self.release() @@ -331,7 +330,7 @@ def prefixed_warnings(prefix: str) -> Generator[None, None, None]: class LogCollector: def __init__(self) -> None: - self.logs = [] # type: List[logging.LogRecord] + self.logs: List[logging.LogRecord] = [] @contextmanager def collect(self) -> Generator[None, None, None]: @@ -356,6 +355,8 @@ def is_suppressed_warning(type: str, subtype: str, suppress_warnings: List[str]) if type is None: return False + subtarget: Optional[str] + for warning_type in suppress_warnings: if '.' in warning_type: target, subtarget = warning_type.split('.', 1) @@ -450,7 +451,7 @@ class OnceFilter(logging.Filter): def __init__(self, name: str = '') -> None: super().__init__(name) - self.messages = {} # type: Dict[str, List] + self.messages: Dict[str, List] = {} def filter(self, record: logging.LogRecord) -> bool: once = getattr(record, 'once', '') @@ -471,7 +472,7 @@ class SphinxLogRecordTranslator(logging.Filter): * Make a instance of SphinxLogRecord * docname to path if location given """ - LogRecordClass = None # type: Type[logging.LogRecord] + LogRecordClass: Type[logging.LogRecord] = None def __init__(self, app: "Sphinx") -> None: self.app = app @@ -509,7 +510,7 @@ class WarningLogRecordTranslator(SphinxLogRecordTranslator): LogRecordClass = SphinxWarningLogRecord -def get_node_location(node: Node) -> str: +def get_node_location(node: Node) -> Optional[str]: (source, line) = get_source_line(node) if source and line: return "%s:%s" % (source, line) |