Skip to content

Fix missing raise keyword in _get_selector_method#465

Open
MichaelMVS wants to merge 1 commit intoelementsinteractive:mainfrom
MichaelMVS:fix-missing-raise-invalid-selectormethod
Open

Fix missing raise keyword in _get_selector_method#465
MichaelMVS wants to merge 1 commit intoelementsinteractive:mainfrom
MichaelMVS:fix-missing-raise-invalid-selectormethod

Conversation

@MichaelMVS
Copy link
Copy Markdown

Summary

The function _get_selector_method in src/twyn/main.py was creating an InvalidSelectorMethodError exception but never raising it:

def _get_selector_method(selector_method: str) -> SelectorMethod:
    if selector_method not in SELECTOR_METHOD_MAPPING:
        InvalidSelectorMethodError("Invalid selector method")  # Missing raise!
    return SELECTOR_METHOD_MAPPING[selector_method]()

This bug caused the function to return None instead of raising an exception when an invalid selector method was provided. The validation error was silently swallowed.

Impact

When an invalid selector method is passed (either via CLI or config), instead of raising an error and informing the user, the function would return None and the program would continue with a None selector, leading to confusing failures later in the code path.

Fix

Added the missing raise keyword so the exception is properly raised when an invalid selector method is detected.

Testing

The existing test suite passes (189 tests). This fix ensures that invalid selector methods are properly rejected with a clear error message instead of silently returning None.

The function was creating an InvalidSelectorMethodError exception but never
raising it, causing the function to return None instead. This meant that
invalid selector methods were silently accepted rather than raising an
error as intended.
Copilot AI review requested due to automatic review settings April 9, 2026 04:42
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes _get_selector_method to correctly raise InvalidSelectorMethodError when an unknown selector method is provided, preventing a silent None return and downstream confusing failures.

Changes:

  • Add missing raise for InvalidSelectorMethodError in _get_selector_method.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 279 to 283
def _get_selector_method(selector_method: str) -> SelectorMethod:
"""Return the selector_method from set of available ones."""
if selector_method not in SELECTOR_METHOD_MAPPING:
InvalidSelectorMethodError("Invalid selector method")
raise InvalidSelectorMethodError("Invalid selector method")

Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return type annotation appears incorrect: SelectorMethod (from twyn.base.constants) is a Literal[...] of method names, but this function returns an instantiated selector (SELECTOR_METHOD_MAPPING[selector_method]()), not the string key. Consider updating the return type (and related parameter annotations where this value is passed around) to the appropriate selector base type/protocol to avoid misleading typing.

Copilot uses AI. Check for mistakes.
Comment on lines 281 to +282
if selector_method not in SELECTOR_METHOD_MAPPING:
InvalidSelectorMethodError("Invalid selector method")
raise InvalidSelectorMethodError("Invalid selector method")
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The raised error message is quite generic. For parity with the config validation (e.g., listing allowed selector methods), consider including the invalid value and the valid options (from SELECTOR_METHOD_MAPPING.keys()) to make CLI/config failures easier to diagnose.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants