PY-79743 Assigning to a Final variable imported from another module is not reported as an error

Merge-request: IJ-MR-157418
Merged-by: Aleksandr Govenko <aleksandr.govenko@jetbrains.com>

GitOrigin-RevId: da45a9d7f5ff1792c24582315d4b95225025bf7a
This commit is contained in:
Aleksandr.Govenko
2025-03-14 01:00:18 +00:00
committed by intellij-monorepo-bot
parent 304b1e4ae9
commit 885d17e72a
5 changed files with 19 additions and 2 deletions
@@ -323,7 +323,11 @@ class PyFinalInspection : PyInspection() {
val scopeOwner = ScopeUtil.getScopeOwner(target);
if (!target.isQualified && scopeOwner != null) {
// multiResolve finds last assignments, but we need all earlier assignments
resolved += ControlFlowCache.getScope(scopeOwner).getNamedElements(target.referencedName, false)
val scope = ControlFlowCache.getScope(scopeOwner)
resolved += scope.getNamedElements(target.referencedName, false)
target.name?.let { name ->
resolved += scope.importedNameDefiners.flatMap { it.multiResolveName(name).mapNotNull { it.element } }
}
}
@@ -2,4 +2,4 @@ import b
<warning descr="'a' is 'Final' and could not be reassigned">b.a</warning> = 2
from b import a
a = 3
<warning descr="'a' is 'Final' and could not be reassigned">a</warning> = 3
@@ -0,0 +1,5 @@
from b import *
print(BAR)
<warning descr="'BAR' is 'Final' and could not be reassigned">BAR</warning> = 43
@@ -0,0 +1,3 @@
from typing import Final
BAR: Final = 42
@@ -8,6 +8,11 @@ import org.jetbrains.annotations.NotNull;
public class PyFinalInspectionTest extends PyInspectionTestCase {
// PY-79743
public void testImportedVariableFinalReassignment() {
doMultiFileTest();
}
// PY-34945
public void testSubclassingFinalClass() {
doMultiFileTest();