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

Added check for importing a Final variable from another module and … #9531

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
32 changes: 31 additions & 1 deletion packages/pyright-internal/src/analyzer/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ import * as AnalyzerNodeInfo from './analyzerNodeInfo';
import { ConstraintTracker } from './constraintTracker';
import { getBoundCallMethod, getBoundInitMethod, getBoundNewMethod } from './constructors';
import { addInheritedDataClassEntries } from './dataClasses';
import { Declaration, DeclarationType, isAliasDeclaration } from './declaration';
import { Declaration, DeclarationType, isAliasDeclaration, isVariableDeclaration } from './declaration';
import { getNameNodeForDeclaration } from './declarationUtils';
import { deprecatedAliases, deprecatedSpecialForms } from './deprecatedSymbols';
import { getEnumDeclaredValueType, isEnumClassWithMembers, transformTypeForEnumMember } from './enums';
Expand Down Expand Up @@ -3205,8 +3205,38 @@ export class Checker extends ParseTreeWalker {
}
}

private _reportOverwriteOfImportedFinal(name: string, symbol: Symbol) {
const decls = symbol.getDeclarations();

const finalImportDecl = decls.find((decl) => {
if (decl.type === DeclarationType.Alias) {
const resolvedDecl = this._evaluator.resolveAliasDeclaration(decl, /* resolveLocalNames */ true);
if (resolvedDecl && isVariableDeclaration(resolvedDecl) && resolvedDecl.isFinal) {
return true;
}
}

return false;
});

if (!finalImportDecl) {
return;
}

decls.forEach((decl) => {
if (decl !== finalImportDecl) {
this._evaluator.addDiagnostic(
DiagnosticRule.reportGeneralTypeIssues,
LocMessage.finalReassigned().format({ name }),
getNameNodeForDeclaration(decl) ?? decl.node
);
}
});
}

private _reportMultipleFinalDeclarations(name: string, symbol: Symbol, scopeType: ScopeType) {
if (!this._evaluator.isFinalVariable(symbol)) {
this._reportOverwriteOfImportedFinal(name, symbol);
return;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/pyright-internal/src/analyzer/typeEvaluatorTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { AnalyzerFileInfo } from './analyzerFileInfo';
import { CodeFlowReferenceExpressionNode, FlowNode } from './codeFlowTypes';
import { ConstraintTracker } from './constraintTracker';
import { Declaration } from './declaration';
import * as DeclarationUtils from './declarationUtils';
import { ResolvedAliasInfo } from './declarationUtils';
import { SymbolWithScope } from './scope';
import { Symbol, SynthesizedTypeInfo } from './symbol';
import { PrintTypeFlags } from './typePrinter';
Expand Down Expand Up @@ -684,7 +684,7 @@ export interface TypeEvaluator {
declaration: Declaration,
resolveLocalNames: boolean,
options?: ResolveAliasOptions
) => DeclarationUtils.ResolvedAliasInfo | undefined;
) => ResolvedAliasInfo | undefined;
getTypeOfIterable: (
typeResult: TypeResult,
isAsync: boolean,
Expand Down
8 changes: 8 additions & 0 deletions packages/pyright-internal/src/tests/samples/final7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# This sample is used in conjunction with final8.py to test that imported
# Final symbols cannot be overwritten.

from typing import Final


var1: Final[int] = 1
var2: Final = 2
20 changes: 20 additions & 0 deletions packages/pyright-internal/src/tests/samples/final8.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# This sample tests that final variables imported from another module
# cannot be overwritten.

from .final7 import *

# This should generate an error.
var1 = 1

# This should generate an error.
var2 = 1


def func1():
from .final7 import var1, var2

# This should generate an error.
var1 = 1

# This should generate an error.
var2 = 1
5 changes: 5 additions & 0 deletions packages/pyright-internal/src/tests/typeEvaluator4.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ test('Final6', () => {
TestUtils.validateResults(analysisResults, 2);
});

test('Final8', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['final8.py']);
TestUtils.validateResults(analysisResults, 4);
});

test('InferredTypes1', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['inferredTypes1.py']);
TestUtils.validateResults(analysisResults, 0);
Expand Down
Loading