mirror of
https://github.com/microsoft/pyright.git
synced 2024-09-11 07:55:56 +03:00
Fixed bug that results in a false negative when a Final
class variable is assigned a value in the class body and reassigned a value in the __init__
method. This addresses #8013. (#8015)
This commit is contained in:
parent
59eabcb60f
commit
ece525e960
@ -116,6 +116,7 @@ import {
|
||||
FunctionDeclaration,
|
||||
ModuleLoaderActions,
|
||||
SpecialBuiltInClassDeclaration,
|
||||
VariableDeclaration,
|
||||
} from './declaration';
|
||||
import {
|
||||
ResolvedAliasInfo,
|
||||
@ -5872,7 +5873,12 @@ export function createTypeEvaluator(
|
||||
// If a Final instance variable is declared in the class body but is
|
||||
// being assigned within an __init__ method, it's allowed.
|
||||
const enclosingFunctionNode = ParseTreeUtils.getEnclosingFunction(errorNode);
|
||||
if (!enclosingFunctionNode || enclosingFunctionNode.name.value !== '__init__') {
|
||||
if (
|
||||
!enclosingFunctionNode ||
|
||||
enclosingFunctionNode.name.value !== '__init__' ||
|
||||
(finalVarTypeDecl as VariableDeclaration).inferredTypeSource !== undefined ||
|
||||
isInstantiableClass(classType)
|
||||
) {
|
||||
diag?.addMessage(LocMessage.finalReassigned().format({ name: memberName }));
|
||||
isDescriptorError = true;
|
||||
}
|
||||
|
@ -121,6 +121,8 @@ b: list[Final[int]] = []
|
||||
class ClassA:
|
||||
member1: Final = 3
|
||||
member2: Final
|
||||
member4: Final
|
||||
member5: Final = 3
|
||||
|
||||
def __init__(self):
|
||||
# This should generate an error.
|
||||
@ -130,6 +132,12 @@ class ClassA:
|
||||
|
||||
self.member3: Final = "hi"
|
||||
|
||||
# This should generate an error.
|
||||
ClassA.member4 = "hi"
|
||||
|
||||
# This should generate an error.
|
||||
ClassA.member5 = 3
|
||||
|
||||
def other(self):
|
||||
# This should generate an error.
|
||||
self.member1 = 5
|
||||
|
@ -32,7 +32,7 @@ test('Final2', () => {
|
||||
|
||||
test('Final3', () => {
|
||||
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['final3.py']);
|
||||
TestUtils.validateResults(analysisResults, 33);
|
||||
TestUtils.validateResults(analysisResults, 35);
|
||||
});
|
||||
|
||||
test('Final4', () => {
|
||||
|
Loading…
Reference in New Issue
Block a user