changeset 52905:483b0bb23085

typing: use a protocol to annotate `hooks` in repository.py That is one external import for the repository interface module. Two more to go. This introduces a new "misc" module in the `interfaces` "package" to host small things that does warrant their own module yet.
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Sat, 08 Feb 2025 18:12:29 +0100
parents acaf6bad6b89
children bde94bd8e8a2
files mercurial/interfaces/misc.py mercurial/interfaces/repository.py mercurial/util.py
diffstat 3 files changed, 30 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mercurial/interfaces/misc.py	Sat Feb 08 18:12:29 2025 +0100
@@ -0,0 +1,26 @@
+# misc.py - Various Interface that did not deserve a dedicated module (yet)
+#
+# Copyright 2025 Octobus, contact@octobus.net
+from __future__ import annotations
+
+import abc
+
+from typing import (
+    Callable,
+    List,
+    Protocol,
+)
+
+
+class IHooks(Protocol):
+    """A collection of hook functions that can be used to extend a
+    function's behavior. Hooks are called in lexicographic order,
+    based on the names of their sources."""
+
+    @abc.abstractmethod
+    def add(self, source: bytes, hook: Callable):
+        ...
+
+    @abc.abstractmethod
+    def __call__(self, *args) -> List:
+        ...
--- a/mercurial/interfaces/repository.py	Sun Feb 09 22:45:16 2025 +0100
+++ b/mercurial/interfaces/repository.py	Sat Feb 08 18:12:29 2025 +0100
@@ -33,7 +33,6 @@
     # to avoid circular imports
     from .. import (
         pathutil,
-        util,
     )
     from ..utils import (
         urlutil,
@@ -42,6 +41,7 @@
     from . import (
         dirstate as intdirstate,
         matcher,
+        misc,
     )
 
     # TODO: make a protocol class for this
@@ -2229,7 +2229,7 @@
     def checkpush(self, pushop):
         pass
 
-    prepushoutgoinghooks: util.hooks
+    prepushoutgoinghooks: misc.IHooks
     """util.hooks instance."""
 
     @abc.abstractmethod
--- a/mercurial/util.py	Sun Feb 09 22:45:16 2025 +0100
+++ b/mercurial/util.py	Sat Feb 08 18:12:29 2025 +0100
@@ -70,6 +70,7 @@
     urllibcompat,
 )
 from .interfaces import (
+    misc as int_misc,
     modules as intmod,
 )
 from .utils import (
@@ -3184,7 +3185,7 @@
         raise error.ParseError(_(b"couldn't parse size: %s") % s)
 
 
-class hooks:
+class hooks(int_misc.IHooks):
     """A collection of hook functions that can be used to extend a
     function's behavior. Hooks are called in lexicographic order,
     based on the names of their sources."""