Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .claude/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"permissions": {
"allow": [
"Bash(uv run pytest:*)",
"Bash(git add:*)",
"Bash(uv run mypy:*)",
"Bash(uv run pre-commit:*)"

],
"deny": []
}
}
21 changes: 8 additions & 13 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@ jobs:
fail-fast: false
matrix:
name: [
"windows-py39",
"windows-py310",
"windows-py313",
"windows-pypy3",

"ubuntu-py310-pytestmain",
"ubuntu-py39",
"ubuntu-py310",
"ubuntu-py311",
"ubuntu-py312",
Expand All @@ -45,32 +44,28 @@ jobs:
]

include:
- name: "windows-py39"
python: "3.9"
- name: "windows-py310"
python: "3.10"
os: windows-latest
tox_env: "py39"
tox_env: "py310"
- name: "windows-py313"
python: "3.13"
os: windows-latest
tox_env: "py313"
- name: "windows-pypy3"
python: "pypy3.9"
python: "pypy3.10"
os: windows-latest
tox_env: "pypy3"
- name: "ubuntu-py310-pytestmain"
python: "3.10"
os: ubuntu-latest
tox_env: "py310-pytestmain"
use_coverage: true
- name: "ubuntu-py39"
python: "3.9"
os: ubuntu-latest
tox_env: "py39"
use_coverage: true
- name: "ubuntu-py310"
python: "3.10"
os: ubuntu-latest
tox_env: "py310"
use_coverage: true
- name: "ubuntu-py311"
python: "3.11"
os: ubuntu-latest
Expand All @@ -87,12 +82,12 @@ jobs:
tox_env: "py313"
use_coverage: true
- name: "ubuntu-pypy3"
python: "pypy3.9"
python: "pypy3.10"
os: ubuntu-latest
tox_env: "pypy3"
use_coverage: true
- name: "ubuntu-benchmark"
python: "3.9"
python: "3.10"
os: ubuntu-latest
tox_env: "benchmark"

Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,6 @@ pip-wheel-metadata/

# pytest-benchmark
.benchmarks/

# Claude Code local settings
.claude/settings.local.json
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ repos:
rev: v3.21.0
hooks:
- id: pyupgrade
args: [--py39-plus]
args: [--py310-plus]
- repo: https://github.com/asottile/blacken-docs
rev: 1.20.0
hooks:
Expand Down
51 changes: 51 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# CLAUDE.md

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.

## Project Overview

Pluggy is a minimalist production-ready plugin system that serves as the core framework for pytest, datasette and devpi.
It provides hook specification and implementation mechanisms through a plugin manager system.

## Development Commands

All commands use `uv run` for consistent environments.

### Testing
- `uv run pytest` - Run all tests (prefer running all tests for quick feedback)
- `uv run pytest testing/benchmark.py` - Run benchmark tests

### Code Quality
- `uv run pre-commit run -a` - Run all pre-commit hooks (linting, formatting, type checking)
- Always reread files modified by pre-commit

## Development Process

- Always read `src/pluggy/*.py` to get a full picture
- Consider backward compatibility
- Always run all tests: `uv run pytest`
- Always run pre-commit before committing: `uv run pre-commit run -a`
- Prefer running full pre-commit over individual tools (ruff/mypy)



## Core Architecture

### Main Components

- **PluginManager** (`src/pluggy/_manager.py`): Central registry that manages plugins and coordinates hook calls
- **HookCaller** (`src/pluggy/_hooks.py`): Executes hook implementations with proper argument binding
- **HookImpl/HookSpec** (`src/pluggy/_hooks.py`): Represent hook implementations and specifications
- **Result** (`src/pluggy/_result.py`): Handles hook call results and exception propagation
- **Multicall** (`src/pluggy/_callers.py`): Core execution engine for calling multiple hook implementations

### Package Structure
- `src/pluggy/` - Main package source
- `testing/` - Test suite using pytest
- `docs/` - Sphinx documentation and examples
- `changelog/` - Towncrier fragments for changelog generation

## Configuration Files
- `pyproject.toml` - Project metadata, build system, tool configuration (ruff, mypy, setuptools-scm)
- `tox.ini` - Multi-environment testing configuration
- `.pre-commit-config.yaml` - Code quality automation (ruff, mypy, flake8, etc.)
9 changes: 6 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ classifiers = [
"Programming Language :: Python :: Implementation :: PyPy",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
]
description = "plugin and hook calling mechanisms for python"
readme = {file = "README.rst", content-type = "text/x-rst"}
requires-python = ">=3.9"
requires-python = ">=3.10"

dynamic = ["version"]
[project.optional-dependencies]
[dependency-groups]
dev = ["pre-commit", "tox"]
testing = ["pytest", "pytest-benchmark", "coverage"]


[tool.setuptools]
packages = ["pluggy"]
package-dir = {""="src"}
Expand Down Expand Up @@ -67,6 +67,9 @@ lines-after-imports = 2

[tool.setuptools_scm]

[tool.uv]
default-groups = ["dev", "testing"]

[tool.towncrier]
package = "pluggy"
package_dir = "src/pluggy"
Expand Down
13 changes: 8 additions & 5 deletions src/pluggy/_callers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from collections.abc import Sequence
from typing import cast
from typing import NoReturn
from typing import TypeAlias
import warnings

from ._hooks import HookImpl
Expand All @@ -19,7 +20,7 @@

# Need to distinguish between old- and new-style hook wrappers.
# Wrapping with a tuple is the fastest type-safe way I found to do it.
Teardown = Generator[None, object, object]
Teardown: TypeAlias = Generator[None, object, object]


def run_old_style_hookwrapper(
Expand Down Expand Up @@ -66,10 +67,12 @@ def _raise_wrapfail(
def _warn_teardown_exception(
hook_name: str, hook_impl: HookImpl, e: BaseException
) -> None:
msg = "A plugin raised an exception during an old-style hookwrapper teardown.\n"
msg += f"Plugin: {hook_impl.plugin_name}, Hook: {hook_name}\n"
msg += f"{type(e).__name__}: {e}\n"
msg += "For more information see https://pluggy.readthedocs.io/en/stable/api_reference.html#pluggy.PluggyTeardownRaisedWarning" # noqa: E501
msg = (
f"A plugin raised an exception during an old-style hookwrapper teardown.\n"
f"Plugin: {hook_impl.plugin_name}, Hook: {hook_name}\n"
f"{type(e).__name__}: {e}\n"
f"For more information see https://pluggy.readthedocs.io/en/stable/api_reference.html#pluggy.PluggyTeardownRaisedWarning" # noqa: E501
)
warnings.warn(PluggyTeardownRaisedWarning(msg), stacklevel=6)


Expand Down
20 changes: 11 additions & 9 deletions src/pluggy/_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from __future__ import annotations

from collections.abc import Callable
from collections.abc import Generator
from collections.abc import Mapping
from collections.abc import Sequence
Expand All @@ -12,29 +13,28 @@
import sys
from types import ModuleType
from typing import Any
from typing import Callable
from typing import Final
from typing import final
from typing import Optional
from typing import overload
from typing import TYPE_CHECKING
from typing import TypeAlias
from typing import TypedDict
from typing import TypeVar
from typing import Union
import warnings

from ._result import Result


_T = TypeVar("_T")
_F = TypeVar("_F", bound=Callable[..., object])
_Namespace = Union[ModuleType, type]
_Plugin = object
_HookExec = Callable[

_Namespace: TypeAlias = ModuleType | type
_Plugin: TypeAlias = object
_HookExec: TypeAlias = Callable[
[str, Sequence["HookImpl"], Mapping[str, object], bool],
Union[object, list[object]],
object | list[object],
]
_HookImplFunction = Callable[..., Union[_T, Generator[None, Result[_T], None]]]
_HookImplFunction: TypeAlias = Callable[..., _T | Generator[None, Result[_T], None]]


class HookspecOpts(TypedDict):
Expand Down Expand Up @@ -374,7 +374,9 @@ def __getattr__(self, name: str) -> HookCaller: ...
_HookRelay = HookRelay


_CallHistory = list[tuple[Mapping[str, object], Optional[Callable[[Any], None]]]]
_CallHistory: TypeAlias = list[
tuple[Mapping[str, object], Callable[[Any], None] | None]
]


class HookCaller:
Expand Down
9 changes: 6 additions & 3 deletions src/pluggy/_manager.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
from __future__ import annotations

from collections.abc import Callable
from collections.abc import Iterable
from collections.abc import Mapping
from collections.abc import Sequence
import inspect
import types
from typing import Any
from typing import Callable
from typing import cast
from typing import Final
from typing import TYPE_CHECKING
from typing import TypeAlias
import warnings

from . import _tracing
Expand All @@ -32,8 +33,10 @@
import importlib.metadata


_BeforeTrace = Callable[[str, Sequence[HookImpl], Mapping[str, Any]], None]
_AfterTrace = Callable[[Result[Any], str, Sequence[HookImpl], Mapping[str, Any]], None]
_BeforeTrace: TypeAlias = Callable[[str, Sequence[HookImpl], Mapping[str, Any]], None]
_AfterTrace: TypeAlias = Callable[
[Result[Any], str, Sequence[HookImpl], Mapping[str, Any]], None
]


def _warn_for_function(warning: Warning, function: Callable[..., object]) -> None:
Expand Down
6 changes: 3 additions & 3 deletions src/pluggy/_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@

from __future__ import annotations

from collections.abc import Callable
from types import TracebackType
from typing import Callable
from typing import cast
from typing import final
from typing import Generic
from typing import Optional
from typing import TypeAlias
from typing import TypeVar


_ExcInfo = tuple[type[BaseException], BaseException, Optional[TracebackType]]
_ExcInfo: TypeAlias = tuple[type[BaseException], BaseException, TracebackType | None]
ResultType = TypeVar("ResultType")


Expand Down
10 changes: 3 additions & 7 deletions src/pluggy/_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

from __future__ import annotations

from collections.abc import Callable
from collections.abc import Sequence
from typing import Any
from typing import Callable


_Writer = Callable[[str], object]
Expand All @@ -32,7 +32,7 @@ def _format_message(self, tags: Sequence[str], args: Sequence[object]) -> str:
content = " ".join(map(str, args))
indent = " " * self.indent

lines = ["{}{} [{}]\n".format(indent, content, ":".join(tags))]
lines = [f"{indent}{content} [{':'.join(tags)}]\n"]

for name, value in extra.items():
lines.append(f"{indent} {name}: {value}\n")
Expand All @@ -42,11 +42,7 @@ def _format_message(self, tags: Sequence[str], args: Sequence[object]) -> str:
def _processmessage(self, tags: tuple[str, ...], args: tuple[object, ...]) -> None:
if self._writer is not None and args:
self._writer(self._format_message(tags, args))
try:
processor = self._tags2proc[tags]
except KeyError:
pass
else:
if processor := self._tags2proc.get(tags):
processor(tags, args)

def setwriter(self, writer: _Writer | None) -> None:
Expand Down
2 changes: 1 addition & 1 deletion testing/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from collections.abc import Callable
from functools import wraps
from typing import Any
from typing import Callable
from typing import cast
from typing import TypeVar

Expand Down
2 changes: 1 addition & 1 deletion testing/test_hookcaller.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from collections.abc import Callable
from collections.abc import Generator
from collections.abc import Sequence
from typing import Callable
from typing import TypeVar

import pytest
Expand Down
5 changes: 2 additions & 3 deletions testing/test_multicall.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from collections.abc import Callable
from collections.abc import Mapping
from collections.abc import Sequence
from typing import Callable
from typing import Union

import pytest

Expand All @@ -20,7 +19,7 @@ def MC(
methods: Sequence[Callable[..., object]],
kwargs: Mapping[str, object],
firstresult: bool = False,
) -> Union[object, list[object]]:
) -> object | list[object]:
caller = _multicall
hookfuncs = []
for method in methods:
Expand Down
Loading