refactor: modernize batch — @override, single stop signal, struct cleanups#758
Merged
Merged
Conversation
…anups Five modernization items from the audit, bundled. koalabear/field: @OverRide decorator added to is_fixed_size, get_byte_length, serialize, deserialize so the SSZType contract is visible at the method site. networking lifecycle: live.py and gossipsub/behavior.py carried both _running (bool) and _stop_event (asyncio.Event) after PR leanEthereum#743. The event already exposes is_set / wait / set / clear, which covers every read and write the bool was doing. Dropped the bool in both files; checks are now `not self._stop_event.is_set()` for "running" and `self._stop_event.is_set()` for "stopped". The event default stays a vanilla `asyncio.Event`; __post_init__ in each file forces it to set (= stopped) so a fresh instance starts stopped until dial/listen/start clears it. Updated __main__.py and node_runner.py plus 6 affected unit tests. storage/namespaces: five frozen dataclass classes whose only contents were string defaults collapsed to plain `Final[str]` module constants (BLOCKS_TABLE_NAME, BLOCKS_CREATE_TABLE, BLOCKS_CREATE_INDEX, ...). 30+ attribute accesses in sqlite.py and its import block rewritten. xmss/tweak_hash: TreeTweak and ChainTweak were Pydantic StrictBaseModel instances used only as packing structs feeding the `match tweak` block in _encode_tweak. Converted both to typing.NamedTuple. The match patterns keep working since NamedTuple auto-populates __match_args__. The future-annotations import that the audit flagged as R1 (banned in Pydantic-defining files) goes away with the conversion since TreeTweak/ChainTweak are no longer Pydantic; TweakHasher stays a StrictBaseModel. xmss/constants: TWEAK_PREFIX_CHAIN, TWEAK_PREFIX_TREE, TWEAK_PREFIX_MESSAGE were Fp(value=0x00..0x02) wrappers whose every consumer immediately did `.value` to recover the int. Inlined as plain Final[int]; dropped the unused Fp import and rewrote three call sites plus one test. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Five modernization items from the audit, bundled. Net +193 / −239 across 13 files.
subspecs/koalabear/field.py@overrideadded tois_fixed_size,get_byte_length,serialize,deserializeso theSSZTypecontract is visible at the method site.subspecs/networking/client/event_source/live.py+subspecs/networking/gossipsub/behavior.py_running(bool) and_stop_event(asyncio.Event). The event already exposesis_set/wait/set/clear, which covers every read and write the bool was doing. Dropped the bool;not self._stop_event.is_set()is the "running" predicate. The default factory stays a vanillaasyncio.Event; each file's__post_init__forces it to set (= stopped) so fresh instances start stopped untildial/listen/startclears it. Updated__main__.py,node_runner.py, and 6 affected unit tests.subspecs/storage/namespaces.py+sqlite.py@dataclass(frozen=True, slots=True)namespace classes whose only contents were string defaults collapsed to plainFinal[str]module constants (BLOCKS_TABLE_NAME,BLOCKS_CREATE_TABLE,BLOCKS_CREATE_INDEX, ...). 30+ attribute accesses insqlite.pyand its import block rewritten.subspecs/xmss/tweak_hash.pyTreeTweakandChainTweakwere PydanticStrictBaseModelinstances used only as packing structs feeding thematch tweakblock. Converted both totyping.NamedTuple— the match patterns keep working sinceNamedTupleauto-populates__match_args__. Thefrom __future__ import annotationsline that the audit flagged as R1 (banned in Pydantic-defining files) goes away with the conversion;TweakHasheritself stays aStrictBaseModel.subspecs/xmss/constants.pyTWEAK_PREFIX_CHAIN/TREE/MESSAGEwereFp(value=0x00..0x02)wrappers whose every consumer immediately did.value. Inlined as plainFinal[int]; dropped the unusedFpimport and rewrote three call sites plus one test.Test plan
ruff check— clean.ruff format --check— clean.ty check— clean (it caught 12 stale_runningreferences in__main__.pyand tests during iteration; all migrated).pytestover koalabear, event_source, gossipsub publish, storage, xmss — 205 tests pass.fill tests/consensus/lstar/fc/test_tick_system.py::test_tick_interval_progression_through_full_slot— 1 passed in 9.16s (consensus fixture pipeline still threads through after the lifecycle signal change).🤖 Generated with Claude Code