Repository navigation
fix(lcom): don't count inherited calls as variables - #865
Merged
DaisukeYoda merged 1 commit intoOct 26, 2026
Merged
Conversation
A self.<name>() call to a method the class inherits was recorded as an instance variable, while the same call written as super().<name>() or Base.<name>(self) recorded nothing. Subclasses that only call base hooks through self therefore kept every such method as its own component, and the super() spelling of the same class scored 1. In a class with bases, a name that is only ever reached as self.<name>(...) and is never bound or declared by the class is now treated as an inherited method and left out of the variable set, both for participating methods and for the constructor-only count. Any other use of self.<name>, such as an assignment or a bare read, keeps it as state, as do __slots__ entries and declared ctypes fields. A class without bases keeps its current behavior. Closes ludo-technologies#862
DaisukeYoda
self-requested a review
October 25, 2026 12:30
Contributor
Author
|
Thanks for the review! GitHub still shows this as blocked: the branch ruleset on |
Member
|
@dodoshell Okay. Merging. |
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.
Summary
A
self.m()call to a method the class inherits was recorded as an instance variable, so the method stayed in the graph as its own component. The same call written assuper().m()orBase.m(self)recorded nothing and the method was excluded as stateless (#810). With this PR both spellings give the same result.Type of Change
Related Issues
Closes #862
Changes
internal/analyzer/lcom.go: newcollectInheritedCalls. In a class with at least one base, a name counts as an inherited method when:self.<name>in the class is the callee of a call. An assignment (self.handler = h) or a bare read anywhere keeps it as state, since the class may be storing a callable on the instance.factory: Callable) or__slots__entry with that name, and no ctypes field declared in_fields_(which can be assigned outside the class body).The class-body part reuses
collectProtocolDefinitionsand adds nested classes, bare annotations and__slots__, which declare an attribute without binding a protocol method.These names are dropped from each participating method's variables and from the constructor-only variables, so
instance_variablesno longer counts them. The call edge is still recorded and core ignores it, as it does for any callee outside the participating set. Nested classes are skipped, since theirselfis a different object. A class without bases keeps the current behaviour, soself._callback()inTestLCOMAnalyzer_MethodReferencesAreNotVariablesstill counts.internal/analyzer/lcom_test.go: newTestLCOMAnalyzer_InheritedMethodCallsAreNotVariables:self.and withsuper(): both give LCOM4 1, 3/3 excluded, 0 instance variablesBookmarksView, based on the hike example): onlyself.bookmarksis counted__init__and then called stays a variable__slots__stay variablesself.open_cb()stay variables (with_fields_assigned after the class)@staticmethodof the class stay connectedself.closedoesn't affect the outer classwebsite/docs/rules/low-class-cohesion.md: one paragraph after the stateless-methods one. English page only, and I didn't touch CHANGELOG.md.Testing
make testpasses locally (all ofinternal/analyzerpasses; see below for the rest)make lintpasses locally (go vet,golangci-lint run; gofmt is clean on the changed files)Other checks:
lcom.gochange, the two repro subtests fail. I also removed each guard one at a time (the other-use check, the class-body bindings, the method bindings,__slots__, the ctypes fields, the bases check, the nested-class skip, the constructor filter), and each removal makes at least one subtest fail.go test ./...on Windows: everything ininternal/analyzerpasses. Ten tests indomainandservicefail on this machine (path and permission tests), but they fail the same way onmainwithout this change.pyscn analyze --json --no-open --select lcom:Scopelcom4 3,instance_variables: 3,ScopeViaSuperlcom4 1excluded_methods: 3,instance_variables: 0email,logging,httpandasyncio(313 classes): 63 classes changed, none went up. 11 have a lower LCOM4, and the average goes from 1.16 to 1.12. Examples:email.message.MIMEPart: 4 → 3, instance variables 13 → 4 (callsMessagemethods throughself)asyncio.unix_events._UnixSelectorEventLoop: 5 → 4, instance variables 11 → 3logging.config.ConvertingList: 2 → 1 (self.convert/self.convert_with_keyfromConvertingMixin)Documentation
Additional Context
As the issue says, state written only in a base class is still out of scope. A callable the base stores on the instance and the subclass only calls is now treated like an inherited method, which matches what
super()already gave.