|
| 1 | +# 02 — Configuration objects (TypedDicts removed) |
| 2 | + |
| 3 | +**Status:** Replace live options encoding |
| 4 | +**Depends on:** [01-module-reorganization.md](01-module-reorganization.md) |
| 5 | +**Next:** [03-markers-attach-config.md](03-markers-attach-config.md) |
| 6 | + |
| 7 | +## Problem |
| 8 | + |
| 9 | +On `main`, options are `HookspecOpts` / `HookimplOpts` TypedDicts — unvalidated |
| 10 | +dicts copied around as the live representation. try-claude introduced proper |
| 11 | +configuration classes; those are the API. |
| 12 | + |
| 13 | +## Goals |
| 14 | + |
| 15 | +- Add `HookspecConfiguration` and `HookimplConfiguration` (`__slots__`, |
| 16 | + `Final`, validation). |
| 17 | +- **Remove TypedDicts from the live and public API surface.** |
| 18 | +- Provide a **narrow pytest support shim** that accepts mapping/dict-shaped |
| 19 | + options and converts them to configuration objects (pytest migration only). |
| 20 | +- Export configuration classes from `pluggy`. |
| 21 | + |
| 22 | +## Non-goals |
| 23 | + |
| 24 | +- Keeping TypedDicts “for compatibility” as a dual API ([DECISIONS.md](DECISIONS.md) D4). |
| 25 | +- Marker attachment (doc 03) — can land in the same PR if cleaner, but |
| 26 | + conceptually next. |
| 27 | +- `create_hookimpl` body (needs NormalImpl/WrapperImpl — doc 04); may stub |
| 28 | + or add method signature with a TODO. |
| 29 | + |
| 30 | +## Target design |
| 31 | + |
| 32 | +```python |
| 33 | +# src/pluggy/_config.py |
| 34 | + |
| 35 | +class HookspecConfiguration: |
| 36 | + __slots__ = ("firstresult", "historic", "warn_on_impl", "warn_on_impl_args") |
| 37 | + firstresult: Final[bool] |
| 38 | + historic: Final[bool] |
| 39 | + warn_on_impl: Final[Warning | None] |
| 40 | + warn_on_impl_args: Final[Mapping[str, Warning] | None] |
| 41 | + |
| 42 | + def __init__(self, firstresult=False, historic=False, ...): |
| 43 | + if historic and firstresult: |
| 44 | + raise ValueError("cannot have a historic firstresult hook") |
| 45 | + ... |
| 46 | + |
| 47 | +class HookimplConfiguration: |
| 48 | + __slots__ = ("wrapper", "hookwrapper", "optionalhook", |
| 49 | + "tryfirst", "trylast", "specname") |
| 50 | + ... |
| 51 | + # create_hookimpl added in doc 04 |
| 52 | +``` |
| 53 | + |
| 54 | +Pytest support shim (name flexible — e.g. `_pytest_compat.py` or helpers in |
| 55 | +`_config.py` marked clearly): |
| 56 | + |
| 57 | +```python |
| 58 | +def hookspec_config_from_mapping(opts: Mapping[str, Any]) -> HookspecConfiguration: |
| 59 | + """Pytest/support only: accept legacy dict-shaped options.""" |
| 60 | + return HookspecConfiguration( |
| 61 | + firstresult=opts.get("firstresult", False), |
| 62 | + historic=opts.get("historic", False), |
| 63 | + warn_on_impl=opts.get("warn_on_impl"), |
| 64 | + warn_on_impl_args=opts.get("warn_on_impl_args"), |
| 65 | + ) |
| 66 | + |
| 67 | +def hookimpl_config_from_mapping(opts: Mapping[str, Any]) -> HookimplConfiguration: |
| 68 | + ... |
| 69 | +``` |
| 70 | + |
| 71 | +Do **not** keep `TypedDict` classes in `__all__`. Do not document dicts as |
| 72 | +the options API. Markers take kwargs that construct configuration objects |
| 73 | +directly (doc 03). |
| 74 | + |
| 75 | +## Reference branch / files |
| 76 | + |
| 77 | +```bash |
| 78 | +git show try-claude:src/pluggy/_hook_config.py |
| 79 | +``` |
| 80 | + |
| 81 | +Port classes from try-claude; then **delete** the TypedDict definitions from |
| 82 | +the live module (try still kept them — we go further per D4). Isolate mapping |
| 83 | +helpers as the only dict-facing surface. |
| 84 | + |
| 85 | +## Implementation steps |
| 86 | + |
| 87 | +### Step 2.1 — Add configuration classes |
| 88 | + |
| 89 | +Port from try-claude `_hook_config.py` into `_config.py`. |
| 90 | + |
| 91 | +### Step 2.2 — Remove TypedDicts from live path |
| 92 | + |
| 93 | +1. Delete `HookspecOpts` / `HookimplOpts` TypedDict definitions (or move to a |
| 94 | + private pytest-shim module if pytest tests in-tree still need the names |
| 95 | + during transition — prefer deletion + mapping helpers). |
| 96 | +2. Update all internal imports/usages to configuration classes. |
| 97 | +3. Update `__init__.py` exports: add configuration classes; drop TypedDict |
| 98 | + exports. |
| 99 | + |
| 100 | +### Step 2.3 — Pytest support shim |
| 101 | + |
| 102 | +1. Add `hookspec_config_from_mapping` / `hookimpl_config_from_mapping`. |
| 103 | +2. Document in docstring: for pytest/support migration only, not the public |
| 104 | + options API. |
| 105 | +3. If pytest (downstream) needs a temporary import path, keep it private |
| 106 | + (`pluggy._config` or `pluggy._pytest_compat`), not in `__all__`. |
| 107 | + |
| 108 | +### Step 2.4 — Tests |
| 109 | + |
| 110 | +- Validation (`historic` + `firstresult`). |
| 111 | +- Mapping shim round-trip. |
| 112 | +- No remaining tests that treat TypedDicts as the primary API. |
| 113 | + |
| 114 | +```bash |
| 115 | +uv run pytest |
| 116 | +uv run pre-commit run -a |
| 117 | +``` |
| 118 | + |
| 119 | +Commit message: |
| 120 | + |
| 121 | +```text |
| 122 | +feat(config): replace TypedDict options with Hook*Configuration classes |
| 123 | +``` |
| 124 | + |
| 125 | +## Public API / back-compat |
| 126 | + |
| 127 | +| Before | After | |
| 128 | +|--------|-------| |
| 129 | +| `HookspecOpts` / `HookimplOpts` TypedDicts | **Removed** from public API | |
| 130 | +| dict attrs on marked functions | configuration objects (doc 03) | |
| 131 | +| — | `HookspecConfiguration` / `HookimplConfiguration` exported | |
| 132 | +| — | private mapping shim for pytest support | |
| 133 | + |
| 134 | +Marker **kwargs** (`firstresult=`, `wrapper=`, …) stay — they construct |
| 135 | +objects, they are not TypedDict literals. |
| 136 | + |
| 137 | +## Tests to add/update |
| 138 | + |
| 139 | +| File | Coverage | |
| 140 | +|------|----------| |
| 141 | +| `testing/test_configuration.py` | Class validation; mapping shim | |
| 142 | +| Anything importing TypedDicts | Migrate or delete | |
| 143 | + |
| 144 | +## Done when |
| 145 | + |
| 146 | +- [ ] Configuration classes are the only live options encoding. |
| 147 | +- [ ] TypedDicts gone from public `__all__` / docs path. |
| 148 | +- [ ] Pytest mapping shim exists and is clearly non-API. |
| 149 | +- [ ] pytest + pre-commit green. |
0 commit comments