Skip to content

Commit 0b45952

Browse files
committed
Add __slots__ to most classes
A bit more efficient, and prevents typo mistakes.
1 parent 8a5c80b commit 0b45952

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

src/pluggy/_hooks.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ class HookspecMarker:
5858
if the :py:class:`.PluginManager` uses the same project_name.
5959
"""
6060

61+
__slots__ = ("project_name",)
62+
6163
def __init__(self, project_name: str) -> None:
6264
self.project_name = project_name
6365

@@ -127,6 +129,8 @@ class HookimplMarker:
127129
if the :py:class:`.PluginManager` uses the same project_name.
128130
"""
129131

132+
__slots__ = ("project_name",)
133+
130134
def __init__(self, project_name: str) -> None:
131135
self.project_name = project_name
132136

@@ -263,9 +267,9 @@ def varnames(func: object) -> Tuple[Tuple[str, ...], Tuple[str, ...]]:
263267

264268
class _HookRelay:
265269
"""hook holder object for performing 1:N hook calls where N is the number
266-
of registered plugins.
270+
of registered plugins."""
267271

268-
"""
272+
__slots__ = ("__dict__",)
269273

270274
if TYPE_CHECKING:
271275

@@ -274,6 +278,15 @@ def __getattr__(self, name: str) -> "_HookCaller":
274278

275279

276280
class _HookCaller:
281+
__slots__ = (
282+
"name",
283+
"spec",
284+
"_hookexec",
285+
"_wrappers",
286+
"_nonwrappers",
287+
"_call_history",
288+
)
289+
277290
def __init__(
278291
self,
279292
name: str,
@@ -282,9 +295,9 @@ def __init__(
282295
spec_opts: Optional["_HookSpecOpts"] = None,
283296
) -> None:
284297
self.name = name
298+
self._hookexec = hook_execute
285299
self._wrappers: List[HookImpl] = []
286300
self._nonwrappers: List[HookImpl] = []
287-
self._hookexec = hook_execute
288301
self._call_history: Optional[
289302
List[Tuple[Mapping[str, object], Optional[Callable[[Any], None]]]]
290303
] = None
@@ -439,6 +452,19 @@ def _maybe_apply_history(self, method: "HookImpl") -> None:
439452

440453

441454
class HookImpl:
455+
__slots__ = (
456+
"function",
457+
"argnames",
458+
"kwargnames",
459+
"plugin",
460+
"opts",
461+
"plugin_name",
462+
"hookwrapper",
463+
"optionalhook",
464+
"tryfirst",
465+
"trylast",
466+
)
467+
442468
def __init__(
443469
self,
444470
plugin: _Plugin,
@@ -461,6 +487,16 @@ def __repr__(self) -> str:
461487

462488

463489
class HookSpec:
490+
__slots__ = (
491+
"namespace",
492+
"function",
493+
"name",
494+
"argnames",
495+
"kwargnames",
496+
"opts",
497+
"warn_on_impl",
498+
)
499+
464500
def __init__(self, namespace: _Namespace, name: str, opts: "_HookSpecOpts") -> None:
465501
self.namespace = namespace
466502
self.function: Callable[..., object] = getattr(namespace, name)

src/pluggy/_manager.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ class PluginManager:
9999
which will subsequently send debug information to the trace helper.
100100
"""
101101

102+
__slots__ = (
103+
"project_name",
104+
"_name2plugin",
105+
"_plugin2hookcallers",
106+
"_plugin_distinfo",
107+
"trace",
108+
"hook",
109+
"_inner_hookexec",
110+
)
111+
102112
def __init__(self, project_name: str) -> None:
103113
self.project_name = project_name
104114
self._name2plugin: Dict[str, _Plugin] = {}

src/pluggy/_result.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class HookCallError(Exception):
3838

3939

4040
class _Result(Generic[_T]):
41+
__slots__ = ("_result", "_excinfo")
42+
4143
def __init__(self, result: Optional[_T], excinfo: Optional[_ExcInfo]) -> None:
4244
self._result = result
4345
self._excinfo = excinfo

0 commit comments

Comments
 (0)