Skip to content

Quality: Mutable internal state is exposed directly by logger#1769

Open
tuanaiseo wants to merge 1 commit into
DetachHead:mainfrom
tuanaiseo:contribai/improve/quality/mutable-internal-state-is-exposed-direct
Open

Quality: Mutable internal state is exposed directly by logger#1769
tuanaiseo wants to merge 1 commit into
DetachHead:mainfrom
tuanaiseo:contribai/improve/quality/mutable-internal-state-is-exposed-direct

Conversation

@tuanaiseo
Copy link
Copy Markdown

Problem

ImportLogger.getLogs() returns the backing _logs array directly, allowing callers to mutate internal state (e.g. push, splice, clearing logs) and break encapsulation.

Severity: medium
File: packages/pyright-internal/src/analyzer/importLogger.ts

Solution

Return a defensive copy (return [...this._logs]) or a readonly view (ReadonlyArray<string>) to prevent external mutation.

Changes

  • packages/pyright-internal/src/analyzer/importLogger.ts (modified)

Testing

  • Existing tests pass
  • Manual review completed
  • No new warnings/errors introduced

…y by logg

`ImportLogger.getLogs()` returns the backing `_logs` array directly, allowing callers to mutate internal state (e.g. `push`, `splice`, clearing logs) and break encapsulation.

Affected files: importLogger.ts

Signed-off-by: tuanaiseo <221258316+tuanaiseo@users.noreply.github.com>

getLogs() {
return this._logs;
return [...this._logs];
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

we should avoid copying the array at runtime which could potentially impact performance by just widening the return type to readonly string[] instead. this also prevents the returned list from being mutated, which shouldn't be allowed either

    getLogs(): readonly string[] {
        return this._logs;
    }
}

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 7, 2026

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

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.

2 participants