Skip to content

Commit 80a73dc

Browse files
committed
Mark some fields and classes Final and @final
Refs #428.
1 parent b4c2146 commit 80a73dc

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

src/pluggy/_hooks.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from typing import Any
1212
from typing import Callable
1313
from typing import Final
14+
from typing import final
1415
from typing import Generator
1516
from typing import List
1617
from typing import Mapping
@@ -69,6 +70,7 @@ class HookimplOpts(TypedDict):
6970
specname: str | None
7071

7172

73+
@final
7274
class HookspecMarker:
7375
"""Decorator for marking functions as hook specifications.
7476
@@ -146,6 +148,7 @@ def setattr_hookspec_opts(func: _F) -> _F:
146148
return setattr_hookspec_opts
147149

148150

151+
@final
149152
class HookimplMarker:
150153
"""Decorator for marking functions as hook implementations.
151154
@@ -341,6 +344,7 @@ def varnames(func: object) -> tuple[tuple[str, ...], tuple[str, ...]]:
341344
return args, kwargs
342345

343346

347+
@final
344348
class HookRelay:
345349
"""Hook holder object for performing 1:N hook calls where N is the number
346350
of registered plugins."""
@@ -382,10 +386,12 @@ def __init__(
382386
spec_opts: HookspecOpts | None = None,
383387
) -> None:
384388
""":meta private:"""
389+
#: Name of the hook getting called.
385390
self.name: Final = name
386391
self._hookexec: Final = hook_execute
387392
self._hookimpls: Final[list[HookImpl]] = []
388393
self._call_history: _CallHistory | None = None
394+
# TODO: Document, or make private.
389395
self.spec: HookSpec | None = None
390396
if specmodule_or_class is not None:
391397
assert spec_opts is not None
@@ -606,6 +612,7 @@ def __repr__(self) -> str:
606612
return f"<_SubsetHookCaller {self.name!r}>"
607613

608614

615+
@final
609616
class HookImpl:
610617
"""A hook implementation in a :class:`HookCaller`."""
611618

@@ -635,34 +642,35 @@ def __init__(
635642
self.function: Final = function
636643
argnames, kwargnames = varnames(self.function)
637644
#: The positional parameter names of ``function```.
638-
self.argnames = argnames
645+
self.argnames: Final = argnames
639646
#: The keyword parameter names of ``function```.
640-
self.kwargnames = kwargnames
647+
self.kwargnames: Final = kwargnames
641648
#: The plugin which defined this hook implementation.
642-
self.plugin = plugin
649+
self.plugin: Final = plugin
643650
#: The :class:`HookimplOpts` used to configure this hook implementation.
644-
self.opts = hook_impl_opts
651+
self.opts: Final = hook_impl_opts
645652
#: The name of the plugin which defined this hook implementation.
646-
self.plugin_name = plugin_name
653+
self.plugin_name: Final = plugin_name
647654
#: Whether the hook implementation is a :ref:`wrapper <hookwrapper>`.
648-
self.wrapper = hook_impl_opts["wrapper"]
655+
self.wrapper: Final = hook_impl_opts["wrapper"]
649656
#: Whether the hook implementation is an :ref:`old-style wrapper
650657
#: <old_style_hookwrappers>`.
651-
self.hookwrapper = hook_impl_opts["hookwrapper"]
658+
self.hookwrapper: Final = hook_impl_opts["hookwrapper"]
652659
#: Whether validation against a hook specification is :ref:`optional
653660
#: <optionalhook>`.
654-
self.optionalhook = hook_impl_opts["optionalhook"]
661+
self.optionalhook: Final = hook_impl_opts["optionalhook"]
655662
#: Whether to try to order this hook implementation :ref:`first
656663
#: <callorder>`.
657-
self.tryfirst = hook_impl_opts["tryfirst"]
664+
self.tryfirst: Final = hook_impl_opts["tryfirst"]
658665
#: Whether to try to order this hook implementation :ref:`last
659666
#: <callorder>`.
660-
self.trylast = hook_impl_opts["trylast"]
667+
self.trylast: Final = hook_impl_opts["trylast"]
661668

662669
def __repr__(self) -> str:
663670
return f"<HookImpl plugin_name={self.plugin_name!r}, plugin={self.plugin!r}>"
664671

665672

673+
@final
666674
class HookSpec:
667675
__slots__ = (
668676
"namespace",

src/pluggy/_manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@ class PluginManager:
9191

9292
def __init__(self, project_name: str) -> None:
9393
#: The project name.
94-
self.project_name: Final[str] = project_name
94+
self.project_name: Final = project_name
9595
self._name2plugin: Final[dict[str, _Plugin]] = {}
9696
self._plugin_distinfo: Final[list[tuple[_Plugin, DistFacade]]] = []
9797
#: The "hook relay", used to call a hook on all registered plugins.
9898
#: See :ref:`calling`.
99-
self.hook: Final[HookRelay] = HookRelay()
99+
self.hook: Final = HookRelay()
100100
#: The tracing entry point. See :ref:`tracing`.
101101
self.trace: Final[_tracing.TagTracerSub] = _tracing.TagTracer().get(
102102
"pluginmanage"

src/pluggy/_result.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from types import TracebackType
77
from typing import Callable
88
from typing import cast
9+
from typing import final
910
from typing import Generator
1011
from typing import Generic
1112
from typing import NoReturn
@@ -36,6 +37,7 @@ class HookCallError(Exception):
3637
"""Hook was called incorrectly."""
3738

3839

40+
@final
3941
class Result(Generic[ResultType]):
4042
"""An object used to inspect and set the result in a :ref:`hook wrapper
4143
<hookwrappers>`."""

0 commit comments

Comments
 (0)