summaryrefslogtreecommitdiff
path: root/sphinx/events.py
diff options
context:
space:
mode:
Diffstat (limited to 'sphinx/events.py')
-rw-r--r--sphinx/events.py34
1 files changed, 12 insertions, 22 deletions
diff --git a/sphinx/events.py b/sphinx/events.py
index e3a3b964f..634fdc6ca 100644
--- a/sphinx/events.py
+++ b/sphinx/events.py
@@ -10,29 +10,26 @@
:license: BSD, see LICENSE for details.
"""
-import warnings
from collections import defaultdict
from operator import attrgetter
-from typing import Any, Callable, Dict, List, NamedTuple, Tuple
+from typing import TYPE_CHECKING, Any, Callable, Dict, List, NamedTuple, Tuple, Type
-from sphinx.deprecation import RemovedInSphinx40Warning
from sphinx.errors import ExtensionError, SphinxError
from sphinx.locale import __
from sphinx.util import logging
from sphinx.util.inspect import safe_getattr
-if False:
- # For type annotation
- from typing import Type # for python3.5.1
-
+if TYPE_CHECKING:
from sphinx.application import Sphinx
logger = logging.getLogger(__name__)
-EventListener = NamedTuple('EventListener', [('id', int),
- ('handler', Callable),
- ('priority', int)])
+
+class EventListener(NamedTuple):
+ id: int
+ handler: Callable
+ priority: int
# List of all known core events. Maps name to arguments description.
@@ -58,13 +55,10 @@ core_events = {
class EventManager:
"""Event manager for Sphinx."""
- def __init__(self, app: "Sphinx" = None) -> None:
- if app is None:
- warnings.warn('app argument is required for EventManager.',
- RemovedInSphinx40Warning)
+ def __init__(self, app: "Sphinx") -> None:
self.app = app
self.events = core_events.copy()
- self.listeners = defaultdict(list) # type: Dict[str, List[EventListener]]
+ self.listeners: Dict[str, List[EventListener]] = defaultdict(list)
self.next_listener_id = 0
def add(self, name: str) -> None:
@@ -91,7 +85,7 @@ class EventManager:
listeners.remove(listener)
def emit(self, name: str, *args: Any,
- allowed_exceptions: Tuple["Type[Exception]", ...] = ()) -> List:
+ allowed_exceptions: Tuple[Type[Exception], ...] = ()) -> List:
"""Emit a Sphinx event."""
try:
logger.debug('[app] emitting event: %r%s', name, repr(args)[:100])
@@ -104,11 +98,7 @@ class EventManager:
listeners = sorted(self.listeners[name], key=attrgetter("priority"))
for listener in listeners:
try:
- if self.app is None:
- # for compatibility; RemovedInSphinx40Warning
- results.append(listener.handler(*args))
- else:
- results.append(listener.handler(self.app, *args))
+ results.append(listener.handler(self.app, *args))
except allowed_exceptions:
# pass through the errors specified as *allowed_exceptions*
raise
@@ -121,7 +111,7 @@ class EventManager:
return results
def emit_firstresult(self, name: str, *args: Any,
- allowed_exceptions: Tuple["Type[Exception]", ...] = ()) -> Any:
+ allowed_exceptions: Tuple[Type[Exception], ...] = ()) -> Any:
"""Emit a Sphinx event and returns first result.
This returns the result of the first handler that doesn't return ``None``.