Repository navigation
Fix #33: Raise NotImplementedError in InterfaceProtocolCheckMixin for Missing Methods - #178
SarthakB11 wants to merge 3 commits into
Conversation
…ProtocolCheckMixin if either classes lack the declared method
Reviewer's Guide by SourceryThis pull request enhances the No diagrams generated as the changes look simple and do not need a visual representation. File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey @SarthakB11 - I've reviewed your changes - here's some feedback:
Overall Comments:
- Consider using
inspect.signatureto 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
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 |
There was a problem hiding this comment.
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):
passMake 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.
| cls_method = getattr(parent_class, defined_method) | ||
|
|
||
| # Get all callable attributes from parent class that aren't dunder methods | ||
| parent_methods = set() |
There was a problem hiding this comment.
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.
|
@codecakes made further improvements based on sourcery's suggestions |
|
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. |
Overview
This PR enhances the InterfaceProtocolCheckMixin to properly verify that both the interface and implementation have all required methods.
Changes Made I have made:
Implementation Details
Testing
All tests pass successfully, including:
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:
Bug Fixes:
Enhancements:
Tests: