fix: MethodDispatcher.__get__ returns independent bound dispatcher per instance#138
Open
gaoflow wants to merge 1 commit into
Open
fix: MethodDispatcher.__get__ returns independent bound dispatcher per instance#138gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
…ispatcher MethodDispatcher.__get__ mutated self.obj and self.cls on the descriptor itself and returned self, so every attribute lookup shared the same object. Accessing other_obj.method after storing bound = obj.method would silently overwrite the stored instance, causing bound(...) to dispatch to the wrong object. Fix: __get__ now returns a new BoundMethodDispatcher instance that captures the specific (instance, owner) pair for that lookup, matching the semantics of Python's ordinary bound-method protocol. Add test_method_dispatcher_bound_independence to cover the regression.
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.
Bug
MethodDispatcher.__get__storedinstanceandownerdirectly onself(the shared descriptor object) and then returned
self:Because every attribute lookup returns the same
MethodDispatcherobject,accessing
other_obj.methodafter storingbound = obj.methodsilentlyoverwrites
self.obj, makingbound(...)dispatch to the wrong instance.Minimal reproducer:
Fix
__get__now returns a newBoundMethodDispatcherinstance that capturesits own
(obj, cls)pair for each lookup, exactly as Python's ordinarybound-method protocol works.
MethodDispatcheritself is no longerresponsible for storing per-call state.
A regression test (
test_method_dispatcher_bound_independence) is included.All 63 tests pass (
python -m pytest multipledispatch/tests/ -p no:randomly).This pull request was prepared with the assistance of AI, under my direction and review.