Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eliminate some false positives on reportUninitializedInstanceVariable #975

Merged
merged 4 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/pyright-internal/src/analyzer/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5341,6 +5341,27 @@ export class Checker extends ParseTreeWalker {
return true;
}

// if containingClass (that initializes this symbol) is a method that is called in __init__
// then we consider this symbol initialized
if (containingClass.nodeType === ParseNodeType.Function) {
const initNode = ClassType.getSymbolTable(classType).get('__init__')?.getDeclarations().at(0)
beauxq marked this conversation as resolved.
Show resolved Hide resolved
?.node;
if (initNode && initNode.nodeType === ParseNodeType.Function) {
return initNode.d.suite.d.statements.some((maybeStatementList) => {
DetachHead marked this conversation as resolved.
Show resolved Hide resolved
if (maybeStatementList.nodeType === ParseNodeType.StatementList) {
return maybeStatementList.d.statements.some((maybeCall) => {
return (
maybeCall.nodeType === ParseNodeType.Call &&
maybeCall.d.leftExpr.nodeType === ParseNodeType.MemberAccess &&
maybeCall.d.leftExpr.d.member.d.value === containingClass.d.name.d.value
);
});
}
return false;
});
}
}

return false;
})
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from typing import final

# The objective of this test is to try a bunch of ways
# to trick the checker into thinking C.x is initialized
# when it's not initialized.


class C:
x: int # should be reported as uninitialized

def __init__(self) -> None:
self.a_method_called_by_init()

def foo() -> None: # pyright: ignore[reportUnusedFunction]
self.x = 3

def a_method_not_called_by_init(self) -> None:
self.x = 3

def a_method_called_by_init(self) -> None:
# reference x before initializing it
self.x += 3


def __init__() -> None:
c = C()
c.x = 3


class D:
def __init__(self) -> None:
c = C()
c.x = 3


@final
class E(C):
def __init__(self) -> None:
super().__init__()
self.x = 3


class F(C):
def __init__(self) -> None:
super().__init__()
self.x = 3 # pyright: ignore[reportUnannotatedClassAttribute]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class C:
x: int # should not be reported as uninitialized

def __init__(self) -> None:
self.reset()
DetachHead marked this conversation as resolved.
Show resolved Hide resolved

def reset(self) -> None:
self.x = 3
17 changes: 17 additions & 0 deletions packages/pyright-internal/src/tests/typeEvaluatorBased.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,20 @@ test('`reportUnusedFunction` on `@final` classes', () => {
],
});
});

describe('uninitialized variable checking with init method calling', () => {
DetachHead marked this conversation as resolved.
Show resolved Hide resolved
test('uninitialized', () => {
const configOptions = new BasedConfigOptions(Uri.empty());
const analysisResults = typeAnalyzeSampleFiles(['uninitializedVariableBased1.py'], configOptions);
validateResultsButBased(analysisResults, {
errors: [{ line: 8, code: DiagnosticRule.reportUninitializedInstanceVariable }],
});
});
test('initialized', () => {
const configOptions = new BasedConfigOptions(Uri.empty());
const analysisResults = typeAnalyzeSampleFiles(['uninitializedVariableBased2.py'], configOptions);
validateResultsButBased(analysisResults, {
errors: [],
});
});
});
Loading