Skip to content

fix(lcom): treat a computed getattr over a class-level name table as stateless - #890

Merged
DaisukeYoda merged 1 commit into
ludo-technologies:mainfrom
dodoshell:fix/lcom-computed-getattr
Nov 14, 2026
Merged

DaisukeYoda merged 1 commit into
ludo-technologies:mainfrom
dodoshell:fix/lcom-computed-getattr

Conversation

@dodoshell

Copy link
Copy Markdown
Contributor

Summary

A method that reaches its fields through a computed name, like getattr(self, self._names[i]) or delattr(self, name) in a loop over self.__fields__, became its own LCOM4 component. The only self.<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 literal getattr(self, "x").

Type of Change

  • Bug fix (non-breaking change)
  • New feature (non-breaking change)
  • Breaking change
  • Documentation update
  • Performance improvement
  • Refactoring

Related Issues

Closes #887

Changes

  • internal/analyzer/lcom.go:
    • usesComputedSelfAttribute: true when the method calls getattr, setattr, delattr or hasattr with self as 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 through self.
    • 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 the InstanceVariables count), so excludeStatelessMethods takes 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_ComputedSelfAttributeNames with the issue's Point and Key, a __slots__ table, a literal getattr next to a computed one in the same method, and three cases that don't change: a method that also reads self.log, a name table that another method assigns through self, and a literal getattr(self, "a") next to a class-level read.

Testing

  • make test passes locally (all internal/analyzer tests pass; see below for the rest)
  • make lint passes locally (go vet ./..., golangci-lint run: 0 issues)
  • Added tests for new functionality (if applicable)

go test ./... on Windows: five TestAggregate*ByModule_* tests in domain and TestComplexityService_Analyze, TestDeadCodeService_Analyze, TestFileReader_ErrorTypes, TestFileReader_PermissionHandling, TestProjectSnapshotResolvesRelativeModulePathsAtCaptureTime in service fail on this machine, and they fail the same way on main without this change.

Verified Against a Real Project

  • Repository (not the one named in the issue): windelbouwman/ppci at f0e9aa0, ppci/wasm/components.py
  • Command: pyscn analyze --json --no-open --select lcom ., then the Instruction and Definition entries of .pyscn/reports/analyze_*.json as {name} + metrics, one compact JSON line each
  • Output before (as printed):
    {"name":"Instruction","lcom4":2,"total_methods":4,"excluded_methods":1,"instance_variables":3,"method_groups":[["__getitem__"],["__repr__","_from_args"]]}
    {"name":"Definition","lcom4":2,"total_methods":2,"excluded_methods":0,"instance_variables":2,"method_groups":[["__getitem__"],["__name__"]]}
    
  • Output after (as printed):
    {"name":"Instruction","lcom4":1,"total_methods":4,"excluded_methods":2,"instance_variables":3,"method_groups":[["__repr__","_from_args"]]}
    {"name":"Definition","lcom4":1,"total_methods":2,"excluded_methods":1,"instance_variables":2,"method_groups":[["__name__"]]}
    

Both __getitem__ methods are return getattr(self, self.__slots__[i]). The other 16 classes in the file are unchanged, and so are the 143 classes pyscn reports on its own testdata/.

Documentation

  • Updated godoc comments
  • Updated README or other docs (if needed)

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.

…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 DaisukeYoda left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@DaisukeYoda
DaisukeYoda merged commit 456174a into ludo-technologies:main Nov 14, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][auto] lcom4-dynamic-setattr-edges-missed: a method using a computed getattr/setattr name becomes its own LCOM4 component

2 participants