Skip to content

Fix #33: Raise NotImplementedError in InterfaceProtocolCheckMixin for Missing Methods - #178

Closed
SarthakB11 wants to merge 3 commits into
myhealthconnectsociety:mainfrom
SarthakB11:fix-interface-protocol-check-mixin
Closed

SarthakB11 wants to merge 3 commits into
myhealthconnectsociety:mainfrom
SarthakB11:fix-interface-protocol-check-mixin

Conversation

@SarthakB11

@SarthakB11 SarthakB11 commented Apr 21, 2025 •

Copy link
Copy Markdown

Overview

This PR enhances the InterfaceProtocolCheckMixin to properly verify that both the interface and implementation have all required methods.

Changes Made I have made:

  • Enhanced method checking in InterfaceProtocolCheckMixin:
    • Added verification that methods declared in the interface are implemented
    • Added verification that implementation doesn't contain undeclared methods
    • Improved error messages to clearly indicate which methods are missing or extra
  • Added comprehensive test suite:
    • Created tests for correct implementations (all methods present with proper signatures)
    • Created tests for implementations missing required methods
    • Created tests for implementations with extra methods not in the interface
    • Used proper pytest assertions and structured test classes

Implementation Details

  • Used direct access to class dict to identify methods defined in each class
  • Improved variable naming for better code readability
  • Streamlined implementation without redundant checks
  • Removed the TODO comment since it was implemented

Testing

All tests pass successfully, including:

  • New tests for the enhanced InterfaceProtocolCheckMixin
  • Existing service tests that use the mixin

Closes
This PR addresses issue #33

Summary by Sourcery

Enhance InterfaceProtocolCheckMixin to provide robust method verification for interface implementations, ensuring all required methods are present and correctly defined

New Features:

  • Implement comprehensive method verification for interface implementations, checking for both missing and extra methods

Bug Fixes:

  • Fix incomplete method verification in InterfaceProtocolCheckMixin

Enhancements:

  • Improve method signature checking to validate method parameters between interface and implementation
  • Enhance error reporting to provide clear details about method mismatches

Tests:

  • Add test suite to verify correct interface implementation
  • Create tests for scenarios with missing methods and extra methods
  • Implement comprehensive pytest tests for InterfaceProtocolCheckMixin

SarthakB11 added 2 commits April 22, 2025 02:00
@sourcery-ai

sourcery-ai Bot commented Apr 21, 2025 •

Copy link
Copy Markdown
Contributor

Reviewer's Guide by Sourcery

This pull request enhances the InterfaceProtocolCheckMixin to ensure that implementations adhere to the interface by verifying the presence and signatures of required methods. It also prevents implementations from having extra methods not defined in the interface. Comprehensive tests have been added to cover correct implementations, missing methods, and extra methods.

No diagrams generated as the changes look simple and do not need a visual representation.

File-Level Changes

Change Details Files
Enhanced method checking in InterfaceProtocolCheckMixin to verify that implementations have all methods declared in the interface and do not contain undeclared methods.
  • Added verification for missing methods in the implementation.
  • Added verification for extra methods in the implementation.
  • Improved error messages to clearly indicate missing or extra methods.
  • Used class __dict__ to identify methods defined in each class.
  • Streamlined implementation by removing redundant checks.
  • Removed the TODO comment since it was implemented.
xcov19/utils/mixins.py
Added a comprehensive test suite for the enhanced InterfaceProtocolCheckMixin.
  • Created tests for correct implementations (all methods present with proper signatures).
  • Created tests for implementations missing required methods.
  • Created tests for implementations with extra methods not in the interface.
  • Used pytest assertions and structured test classes.
xcov19/tests/test_mixins.py

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey @SarthakB11 - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider using inspect.signature to compare method signatures more robustly.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Review instructions: all looks good
  • 🟡 Testing: 1 issue found
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Interface method with parameters that should be implemented by subclasses.
This method is intentionally empty as it serves as an interface definition.
"""
pass

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

suggestion (testing): Add a test case for incorrect method signature.

While the current tests cover missing and extra methods, it's crucial to also test scenarios where the method signature in the implementation doesn't match the interface. For instance, different parameter types, different number of parameters, or a different return type. This would ensure comprehensive coverage of the signature matching logic.

Suggested implementation:

import pytest

def test_incorrect_signature_raises_error():
    with pytest.raises(TypeError):
        # This class intentionally has an incorrect signature for method_with_params:
        # It is missing the second parameter compared to the definition in BaseClass.
        class IncorrectImplementation(BaseClass, InterfaceProtocolCheckMixin):
            def method_with_params(self, param1: int):
                pass

Make sure that the InterfaceProtocolCheckMixin is designed to perform runtime signature checks (e.g., via init_subclass) so that the error is raised at class definition time, or adjust the test accordingly if the check is performed at instantiation time.

Comment thread xcov19/utils/mixins.py Outdated
cls_method = getattr(parent_class, defined_method)

# Get all callable attributes from parent class that aren't dunder methods
parent_methods = set()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

issue (complexity): Consider using set comprehensions instead of explicit loops to build parent_methods and cls_methods for improved conciseness and readability, which reduces complexity and maintenance overhead

Using explicit loops for building parent_methods and cls_methods increases the code size and maintenance overhead. Consider using set comprehensions, which are more concise and equally clear. For example, you can replace:

# Get all callable attributes from parent class that aren't dunder methods
parent_methods = set()
for name, attr in parent_class.__dict__.items():
    if not name.startswith("__") and callable(attr):
        parent_methods.add(name)

# Get all callable attributes from implementation class that aren't dunder methods
cls_methods = set()
for name, attr in cls.__dict__.items():
    if not name.startswith("__") and callable(attr):
        cls_methods.add(name)

with:

parent_methods = {name for name, attr in parent_class.__dict__.items() if not name.startswith("__") and callable(attr)}
cls_methods = {name for name, attr in cls.__dict__.items() if not name.startswith("__") and callable(attr)}

This maintains the functionality while reducing the complexity and improving readability.

@SarthakB11

Copy link
Copy Markdown
Author

@codecakes made further improvements based on sourcery's suggestions

@SarthakB11

Copy link
Copy Markdown
Author

Closing since there's been no maintainer engagement in 15 months (only automated review activity). Happy to reopen if there's renewed interest in this fix.

@SarthakB11 SarthakB11 closed this Jul 18, 2026
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.

1 participant