refactor: use Protocol and singledispatch - #3607
Merged
Merged
Conversation
Prefer functional style over class inheritance: - Replace the `BaseFilter` base class with a structural `ResourceFilter` protocol. `OneOf` stores resources as a tuple so one-shot iterators survive repeated `filter()` calls in the cluster getter retry loop. - Split the isinstance chains in `check_action_view` and `check_vote_view` into `functools.singledispatch` handlers, removing the C901 suppressions. Behavior is unchanged, including the `NotImplementedError` fallback for unregistered action/vote types.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors cluster resource selection and governance view-check helpers to prefer structural typing and functional dispatch, reducing inheritance and large isinstance chains while keeping behavior the same.
Changes:
- Replaced the
BaseFilterclass with a structuralResourceFilterProtocol, and updatedOneOfto store resources as a tuple to avoid one-shot iterator exhaustion. - Refactored governance action/vote “view” checks to use
functools.singledispatchhandlers instead of longisinstancechains, removing prior complexity suppressions. - Updated cluster getter type hints to align with the new filter protocol.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
cardano_node_tests/utils/governance_utils.py |
Uses singledispatch to map action/vote types to expected CLI “view” output structures, replacing isinstance chains. |
cardano_node_tests/cluster_management/resources_management.py |
Introduces ResourceFilter protocol and updates OneOf to eagerly materialize resource iterables. |
cardano_node_tests/cluster_management/cluster_getter.py |
Adjusts return type annotations to reflect the new ResourceFilter protocol. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
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.
Prefer functional style over class inheritance:
BaseFilterbase class with a structuralResourceFilterprotocol.OneOfstores resources as a tuple so one-shot iterators survive repeatedfilter()calls in the cluster getter retry loop.check_action_viewandcheck_vote_viewintofunctools.singledispatchhandlers, removing the C901 suppressions. Behavior is unchanged, including theNotImplementedErrorfallback for unregistered action/vote types.