Skip to content

Commit

Permalink
Revert "Revert "fix reportUninitializedInstanceVariable false posit…
Browse files Browse the repository at this point in the history
…ive on `NamedTuple` (new bug in upstream)" since it has now been fixed upstream"

This reverts commit 9f80bab.
  • Loading branch information
DetachHead committed Jul 6, 2024
1 parent 181a2b4 commit bcaba64
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
20 changes: 13 additions & 7 deletions packages/pyright-internal/src/analyzer/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5232,13 +5232,19 @@ export class Checker extends ParseTreeWalker {
if (parentSymbol) {
return;
}

// Report the variable as uninitialized only on the first decl.
this._evaluator.addDiagnostic(
DiagnosticRule.reportUninitializedInstanceVariable,
LocMessage.uninitializedInstanceVariable().format({ name: name }),
decls[0].node
);
if (
!classType.details.baseClasses.some(
(baseClass) =>
baseClass.category === TypeCategory.Class && baseClass.details.fullName === 'typing.NamedTuple'
)
) {
// Report the variable as uninitialized only on the first decl.
this._evaluator.addDiagnostic(
DiagnosticRule.reportUninitializedInstanceVariable,
LocMessage.uninitializedInstanceVariable().format({ name: name }),
decls[0].node
);
}
});

// See if there are any variables from abstract base classes
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from typing import NamedTuple

class Foo(NamedTuple):
a: int # no error

class Bar:
b: str # error
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,12 @@ test('subscript context manager types on 3.8', () => {
],
});
});

test('no reportUninitializedInstanceVariable on NamedTuple', () => {
const configOptions = new ConfigOptions(Uri.empty());
configOptions.diagnosticRuleSet.reportUninitializedInstanceVariable = 'error';
const analysisResults = typeAnalyzeSampleFiles(['uninitializedVariableBased.py'], configOptions);
validateResultsButBased(analysisResults, {
errors: [{ code: DiagnosticRule.reportUninitializedInstanceVariable, line: 6 }],
});
});

0 comments on commit bcaba64

Please sign in to comment.