Skip to content

fix(lcom): don't count inherited calls as variables - #865

Merged
DaisukeYoda merged 1 commit into
ludo-technologies:mainfrom
dodoshell:fix/lcom4-inherited-self-calls
Oct 26, 2026
Merged

DaisukeYoda merged 1 commit into
ludo-technologies:mainfrom
dodoshell:fix/lcom4-inherited-self-calls

Conversation

@dodoshell

Copy link
Copy Markdown
Contributor

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 as super().m() or Base.m(self) recorded nothing and the method was excluded as stateless (#810). With this PR both spellings give the same result.

Type of Change

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

Related Issues

Closes #862

Changes

  • internal/analyzer/lcom.go: new collectInheritedCalls. In a class with at least one base, a name counts as an inherited method when:

    • every 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.
    • the class doesn't bind or declare it: no method, nested class, assignment, bare annotation (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 collectProtocolDefinitions and 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_variables no 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 their self is a different object. A class without bases keeps the current behaviour, so self._callback() in TestLCOMAnalyzer_MethodReferencesAreNotVariables still counts.

  • internal/analyzer/lcom_test.go: new TestLCOMAnalyzer_InheritedMethodCallsAreNotVariables:

    • the issue repro, written with self. and with super(): both give LCOM4 1, 3/3 excluded, 0 instance variables
    • inherited calls next to real state (BookmarksView, based on the hike example): only self.bookmarks is counted
    • a callable assigned in __init__ and then called stays a variable
    • a name bound in the class body (assignment or bare annotation) stays a variable
    • names listed in __slots__ stay variables
    • ctypes callback fields called as self.open_cb() stay variables (with _fields_ assigned after the class)
    • two methods calling a @staticmethod of the class stay connected
    • a nested class assigning self.close doesn't affect the outer class
    • a class without bases is unchanged
  • website/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 test passes locally (all of internal/analyzer passes; see below for the rest)
  • make lint passes locally (go vet, golangci-lint run; gofmt is clean on the changed files)
  • Added tests for new functionality (if applicable)
  • Manual testing completed (if applicable)

Other checks:

  • Without the lcom.go change, 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 in internal/analyzer passes. Ten tests in domain and service fail on this machine (path and permission tests), but they fail the same way on main without this change.
  • The issue repro with pyscn analyze --json --no-open --select lcom:
    • before: Scope lcom4 3, instance_variables: 3, ScopeViaSuper lcom4 1
    • after: both lcom4 1, excluded_methods: 3, instance_variables: 0
  • A/B on CPython 3.13's email, logging, http and asyncio (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 (calls Message methods through self)
    • asyncio.unix_events._UnixSelectorEventLoop: 5 → 4, instance variables 11 → 3
    • logging.config.ConvertingList: 2 → 1 (self.convert / self.convert_with_key from ConvertingMixin)

Documentation

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

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.

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 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.

Thanks, this looks good.

@dodoshell

Copy link
Copy Markdown
Contributor Author

Thanks for the review! GitHub still shows this as blocked: the branch ruleset on main requires a check named test (1.22), but the CI job now reports as plain test, so the required check never arrives. Updating the required check name in the ruleset should unblock it.

@DaisukeYoda

Copy link
Copy Markdown
Member

@dodoshell Okay. Merging.

@DaisukeYoda
DaisukeYoda merged commit 122ddfb into ludo-technologies:main Oct 26, 2026
6 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.

[ENH][auto] lcom4-inherited-method-call-as-ivar: self.<inherited>() counts as instance state, super().<m>() does not

2 participants