Fix @inject on classmethod returning wrong cls in subclass hierarchy - #978
Merged
ZipFile merged 5 commits intoAug 4, 2026
Merged
Conversation
When _patch_method processed inherited classmethods on subclasses, it would wrap the already-bound method (which has cls baked in) and set it as a plain function on the subclass. This broke the classmethod descriptor protocol, causing deeper subclasses to receive the wrong cls. The fix skips re-patching inherited methods whose underlying __func__ is already patched by the parent class, preserving correct cls binding through normal Python inheritance. Fixes ets-labs#947 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
ZipFile
force-pushed
the
fix/issue947-classmethod-inject-cls
branch
from
August 4, 2026 18:33
57aa252 to
3d143f8
Compare
ZipFile
force-pushed
the
fix/issue947-classmethod-inject-cls
branch
from
August 4, 2026 18:35
3d143f8 to
186d71a
Compare
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.
Fixes #947
Problem
When a @classmethod decorated with @Inject is inherited through multiple levels (Base -> Sub1 -> Sub2), calling Sub2.injected_factory() incorrectly returns an instance of Sub1 instead of Sub2.
Root Cause
In _patch_method, when processing inherited classmethods on subclasses, the method is not in the subclass's dict, so the code gets a bound method from getmembers. This bound method has cls already baked in. Since the bound method itself isn't registered as patched, the code wraps it again and sets the result as a plain function on the subclass via setattr. This breaks the classmethod descriptor protocol — deeper subclasses inherit the patched plain function (with the wrong cls) instead of going through the classmethod descriptor.
Fix
In the else branch of _patch_method (inherited methods), check if the underlying func is already patched. If so, skip — the parent class's classmethod descriptor will correctly provide cls to all subclasses through normal Python inheritance.
Validation