Skip to content

Commit 81832c2

Browse files
authored
Refactor is_suppressed_warning to use set lookups (#12539)
1 parent 602bfe4 commit 81832c2

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

sphinx/util/logging.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from sphinx.util.osutil import abspath
1717

1818
if TYPE_CHECKING:
19-
from collections.abc import Iterator
19+
from collections.abc import Iterator, Sequence, Set
2020

2121
from docutils.nodes import Node
2222

@@ -407,23 +407,18 @@ def filter(self, record: logging.LogRecord) -> bool:
407407
return record.levelno < logging.WARNING
408408

409409

410-
def is_suppressed_warning(type: str, subtype: str, suppress_warnings: list[str]) -> bool:
410+
def is_suppressed_warning(
411+
warning_type: str, sub_type: str, suppress_warnings: Set[str] | Sequence[str],
412+
) -> bool:
411413
"""Check whether the warning is suppressed or not."""
412-
if type is None:
414+
if warning_type is None or len(suppress_warnings) == 0:
413415
return False
414-
415-
subtarget: str | None
416-
417-
for warning_type in suppress_warnings:
418-
if '.' in warning_type:
419-
target, subtarget = warning_type.split('.', 1)
420-
else:
421-
target, subtarget = warning_type, None
422-
423-
if target == type and subtarget in (None, subtype, "*"):
424-
return True
425-
426-
return False
416+
suppressed_warnings = frozenset(suppress_warnings)
417+
if warning_type in suppressed_warnings:
418+
return True
419+
if f'{warning_type}.*' in suppressed_warnings:
420+
return True
421+
return f'{warning_type}.{sub_type}' in suppressed_warnings
427422

428423

429424
class WarningSuppressor(logging.Filter):
@@ -441,7 +436,7 @@ def filter(self, record: logging.LogRecord) -> bool:
441436
suppress_warnings = self.app.config.suppress_warnings
442437
except AttributeError:
443438
# config is not initialized yet (ex. in conf.py)
444-
suppress_warnings = []
439+
suppress_warnings = ()
445440

446441
if is_suppressed_warning(type, subtype, suppress_warnings):
447442
return False

0 commit comments

Comments
 (0)