Repository navigation
fix(lcom): treat a computed getattr over a class-level name table as stateless - #890
Merged
DaisukeYoda merged 1 commit intoNov 14, 2026
Conversation
…stateless A method that reaches its fields through a computed name, such as getattr(self, self._names[i]) or delattr(self, name) in a loop over self.__fields__, recorded only the class-level name table as its state, so it became its own LCOM4 component next to the methods that use the same fields directly. collectMethods now drops that method's accesses when every one of them is a name the class body assigns and no method assigns through self. The stateless pass then leaves it out of the graph, like a method whose only access is a literal getattr(self, "x"). A method that also reads an instance field stays in the graph, and a literal getattr name is unchanged. Closes ludo-technologies#887
DaisukeYoda
self-requested a review
November 14, 2026 03:56
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 method that reaches its fields through a computed name, like
getattr(self, self._names[i])ordelattr(self, name)in a loop overself.__fields__, became its own LCOM4 component. The onlyself.<name>it reads is the class-level name table, so it shared nothing with the methods that use the same fields directly. With this PR such a method is left out of the graph as stateless, the same as a method whose only access is a literalgetattr(self, "x").Type of Change
Related Issues
Closes #887
Changes
internal/analyzer/lcom.go:usesComputedSelfAttribute: true when the method callsgetattr,setattr,delattrorhasattrwithselfas the first argument and a name that is not a string literal.collectClassAttributes: the names the class body binds by assignment (_names = ...,__fields__ = ...,__slots__ = ...) that no method assigns throughself.collectMethods: when a method uses a computed name and every variable it touches is one of those class attributes, its variables are cleared (they stay in theInstanceVariablescount), soexcludeStatelessMethodstakes it out unless it has a call edge. A method that also reads an instance field keeps all its accesses and stays in the graph.getattr(self, "x")is not affected.internal/analyzer/lcom_test.go:TestLCOMAnalyzer_ComputedSelfAttributeNameswith the issue'sPointandKey, a__slots__table, a literalgetattrnext to a computed one in the same method, and three cases that don't change: a method that also readsself.log, a name table that another method assigns throughself, and a literalgetattr(self, "a")next to a class-level read.Testing
make testpasses locally (allinternal/analyzertests pass; see below for the rest)make lintpasses locally (go vet ./...,golangci-lint run: 0 issues)go test ./...on Windows: fiveTestAggregate*ByModule_*tests indomainandTestComplexityService_Analyze,TestDeadCodeService_Analyze,TestFileReader_ErrorTypes,TestFileReader_PermissionHandling,TestProjectSnapshotResolvesRelativeModulePathsAtCaptureTimeinservicefail on this machine, and they fail the same way onmainwithout this change.Verified Against a Real Project
f0e9aa0,ppci/wasm/components.pypyscn analyze --json --no-open --select lcom ., then theInstructionandDefinitionentries of.pyscn/reports/analyze_*.jsonas{name} + metrics, one compact JSON line eachBoth
__getitem__methods arereturn getattr(self, self.__slots__[i]). The other 16 classes in the file are unchanged, and so are the 143 classes pyscn reports on its owntestdata/.Documentation
Additional Context
A class-level name that some method also assigns through
self(self._names = names) is treated as instance state, so the method reading it keeps that link.