Skip to content

Commit

Permalink
Mark some fields and classes Final and @final
Browse files Browse the repository at this point in the history
  • Loading branch information
bluetech committed Aug 13, 2023
1 parent b4c2146 commit 80a73dc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
28 changes: 18 additions & 10 deletions src/pluggy/_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from typing import Any
from typing import Callable
from typing import Final
from typing import final
from typing import Generator
from typing import List
from typing import Mapping
Expand Down Expand Up @@ -69,6 +70,7 @@ class HookimplOpts(TypedDict):
specname: str | None


@final
class HookspecMarker:
"""Decorator for marking functions as hook specifications.
Expand Down Expand Up @@ -146,6 +148,7 @@ def setattr_hookspec_opts(func: _F) -> _F:
return setattr_hookspec_opts


@final
class HookimplMarker:
"""Decorator for marking functions as hook implementations.
Expand Down Expand Up @@ -341,6 +344,7 @@ def varnames(func: object) -> tuple[tuple[str, ...], tuple[str, ...]]:
return args, kwargs


@final
class HookRelay:
"""Hook holder object for performing 1:N hook calls where N is the number
of registered plugins."""
Expand Down Expand Up @@ -382,10 +386,12 @@ def __init__(
spec_opts: HookspecOpts | None = None,
) -> None:
""":meta private:"""
#: Name of the hook getting called.
self.name: Final = name
self._hookexec: Final = hook_execute
self._hookimpls: Final[list[HookImpl]] = []
self._call_history: _CallHistory | None = None
# TODO: Document, or make private.
self.spec: HookSpec | None = None
if specmodule_or_class is not None:
assert spec_opts is not None
Expand Down Expand Up @@ -606,6 +612,7 @@ def __repr__(self) -> str:
return f"<_SubsetHookCaller {self.name!r}>"


@final
class HookImpl:
"""A hook implementation in a :class:`HookCaller`."""

Expand Down Expand Up @@ -635,34 +642,35 @@ def __init__(
self.function: Final = function
argnames, kwargnames = varnames(self.function)
#: The positional parameter names of ``function```.
self.argnames = argnames
self.argnames: Final = argnames
#: The keyword parameter names of ``function```.
self.kwargnames = kwargnames
self.kwargnames: Final = kwargnames
#: The plugin which defined this hook implementation.
self.plugin = plugin
self.plugin: Final = plugin
#: The :class:`HookimplOpts` used to configure this hook implementation.
self.opts = hook_impl_opts
self.opts: Final = hook_impl_opts
#: The name of the plugin which defined this hook implementation.
self.plugin_name = plugin_name
self.plugin_name: Final = plugin_name
#: Whether the hook implementation is a :ref:`wrapper <hookwrapper>`.
self.wrapper = hook_impl_opts["wrapper"]
self.wrapper: Final = hook_impl_opts["wrapper"]
#: Whether the hook implementation is an :ref:`old-style wrapper
#: <old_style_hookwrappers>`.
self.hookwrapper = hook_impl_opts["hookwrapper"]
self.hookwrapper: Final = hook_impl_opts["hookwrapper"]
#: Whether validation against a hook specification is :ref:`optional
#: <optionalhook>`.
self.optionalhook = hook_impl_opts["optionalhook"]
self.optionalhook: Final = hook_impl_opts["optionalhook"]
#: Whether to try to order this hook implementation :ref:`first
#: <callorder>`.
self.tryfirst = hook_impl_opts["tryfirst"]
self.tryfirst: Final = hook_impl_opts["tryfirst"]
#: Whether to try to order this hook implementation :ref:`last
#: <callorder>`.
self.trylast = hook_impl_opts["trylast"]
self.trylast: Final = hook_impl_opts["trylast"]

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


@final
class HookSpec:
__slots__ = (
"namespace",
Expand Down
4 changes: 2 additions & 2 deletions src/pluggy/_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ class PluginManager:

def __init__(self, project_name: str) -> None:
#: The project name.
self.project_name: Final[str] = project_name
self.project_name: Final = project_name
self._name2plugin: Final[dict[str, _Plugin]] = {}
self._plugin_distinfo: Final[list[tuple[_Plugin, DistFacade]]] = []
#: The "hook relay", used to call a hook on all registered plugins.
#: See :ref:`calling`.
self.hook: Final[HookRelay] = HookRelay()
self.hook: Final = HookRelay()
#: The tracing entry point. See :ref:`tracing`.
self.trace: Final[_tracing.TagTracerSub] = _tracing.TagTracer().get(
"pluginmanage"
Expand Down
2 changes: 2 additions & 0 deletions src/pluggy/_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from types import TracebackType
from typing import Callable
from typing import cast
from typing import final
from typing import Generator
from typing import Generic
from typing import NoReturn
Expand Down Expand Up @@ -36,6 +37,7 @@ class HookCallError(Exception):
"""Hook was called incorrectly."""


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

0 comments on commit 80a73dc

Please sign in to comment.