Skip to content

Commit 511429f

Browse files
pdbethkeclaude
andcommitted
refactor(campaign): rename the context manager, correct the reader count
`campaign_rules` -> `use_campaign_rules` (controller's ruling): it collided with `EngineContext.campaign_rules()`, the getter, and tests/test_campaign_active.py imports both into one module. Six call sites, no behavioural change. I4 -- `active_template` has EIGHT readers, not nine. Counted and named in context.py so the next reader does not have to re-count: continuous, nonpersistent, norangemodifier, persistent, disadvantage, combat_sense, simulate_death, universal_translator. base.py:1814 mentions the slot in prose and does not read it. Corrected in both context.py and test_campaign_active.py. M1 -- dropped the redundant local `import os` / `import pytest` from the `provider` fixture; both are already module-level. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Xof4ZUS5n6A5ibX3PXNYbs
1 parent b25a27f commit 511429f

5 files changed

Lines changed: 25 additions & 18 deletions

File tree

kirby_cost/campaign/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""A campaign's own rules: what this GM changed about the HERO template."""
2-
from kirby_cost.campaign.active import campaign_rules
2+
from kirby_cost.campaign.active import use_campaign_rules
33
from kirby_cost.campaign.rules import CampaignRules, OVERRIDABLE_FIELDS
44

5-
__all__ = ["CampaignRules", "OVERRIDABLE_FIELDS", "campaign_rules"]
5+
__all__ = ["CampaignRules", "OVERRIDABLE_FIELDS", "use_campaign_rules"]

kirby_cost/campaign/active.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,14 @@
2020

2121

2222
@contextmanager
23-
def campaign_rules(rules: Optional["CampaignRules"]):
24-
"""Run a block with *rules* active, restoring the previous value after."""
23+
def use_campaign_rules(rules: Optional["CampaignRules"]):
24+
"""Run a block with *rules* active, restoring the previous value after.
25+
26+
Named `use_` rather than plainly `campaign_rules` because the latter
27+
collides with `EngineContext.campaign_rules()`, the GETTER -- and a module
28+
that needs both (tests/test_campaign_active.py does) ends up importing two
29+
different things under one name.
30+
"""
2531
previous = EngineContext.campaign_rules()
2632
EngineContext.set_campaign_rules(rules)
2733
try:

kirby_cost/core/context.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,20 @@ def set_active_template(cls, template: Optional['Template']) -> None:
6767
def campaign_rules(cls) -> Optional['CampaignRules']:
6868
"""The active campaign's rule overrides, or None.
6969
70-
DELIBERATELY separate from `active_template`. That slot has nine
70+
DELIBERATELY separate from `active_template`. That slot has EIGHT
7171
readers whose branches were never finished -- they only ever ran their
72-
None side -- so populating it would switch nine stubs on at once. See
73-
the campaign-rule-overrides spec, section 7.
72+
None side -- so populating it would switch eight stubs on at once
73+
(counted 2026-08-25: continuous, nonpersistent, norangemodifier,
74+
persistent, disadvantage, combat_sense, simulate_death,
75+
universal_translator; base.py:1814 mentions the slot in prose and does
76+
not read it). See the campaign-rule-overrides spec, section 7.
7477
"""
7578
return cls.get_instance()._campaign_rules
7679

7780
@classmethod
7881
def set_campaign_rules(cls, rules: Optional['CampaignRules']) -> None:
7982
"""Set the active campaign's rule overrides. Prefer the
80-
`kirby_cost.campaign.campaign_rules` context manager, which restores
83+
`kirby_cost.campaign.use_campaign_rules` context manager, which restores
8184
the previous value."""
8285
cls.get_instance()._campaign_rules = rules
8386

tests/conftest.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,6 @@ def make_adder(
317317
def provider():
318318
"""The configured HDT provider, or skip. Rules validate against a real
319319
template, so authoring them needs one resolvable."""
320-
import os
321-
import pytest
322320
from kirby_cost.template.hdt_provider import HDTTemplateProvider
323321
if not os.environ.get("KIRBY_COST_HDT"):
324322
pytest.skip("KIRBY_COST_HDT is not set")

tests/test_campaign_active.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import pytest
1010

11-
from kirby_cost.campaign import CampaignRules, campaign_rules
11+
from kirby_cost.campaign import CampaignRules, use_campaign_rules
1212
from kirby_cost.core.context import EngineContext
1313

1414

@@ -19,32 +19,32 @@ def test_the_slot_starts_empty():
1919
def test_the_block_sets_and_restores(provider):
2020
rules = CampaignRules(provider=provider)
2121
assert EngineContext.campaign_rules() is None
22-
with campaign_rules(rules):
22+
with use_campaign_rules(rules):
2323
assert EngineContext.campaign_rules() is rules
2424
assert EngineContext.campaign_rules() is None
2525

2626

2727
def test_the_block_restores_even_when_the_body_raises(provider):
2828
rules = CampaignRules(provider=provider)
2929
with pytest.raises(RuntimeError):
30-
with campaign_rules(rules):
30+
with use_campaign_rules(rules):
3131
raise RuntimeError("boom")
3232
assert EngineContext.campaign_rules() is None
3333

3434

3535
def test_blocks_nest_and_restore_the_outer_value(provider):
3636
outer, inner = CampaignRules(provider=provider), CampaignRules(provider=provider)
37-
with campaign_rules(outer):
38-
with campaign_rules(inner):
37+
with use_campaign_rules(outer):
38+
with use_campaign_rules(inner):
3939
assert EngineContext.campaign_rules() is inner
4040
assert EngineContext.campaign_rules() is outer
4141
assert EngineContext.campaign_rules() is None
4242

4343

4444
def test_the_active_template_slot_is_left_alone(provider):
45-
"""active_template has nine readers whose branches are unfinished stubs
45+
"""active_template has EIGHT readers whose branches are unfinished stubs
4646
(`if template: # Would check if template is 6E; return True`). Setting it
47-
would flip all nine on at once. This feature must not touch it."""
47+
would flip all eight on at once. This feature must not touch it."""
4848
rules = CampaignRules(provider=provider)
49-
with campaign_rules(rules):
49+
with use_campaign_rules(rules):
5050
assert EngineContext.active_template() is None

0 commit comments

Comments
 (0)