PY-26947 Class variable mistaken as module global variable

Prevent resolving class-level target expressions in global scope.


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

GitOrigin-RevId: 2452801387ee2480e61b2f5119e7f3f2704cb4ef
This commit is contained in:
Aleksandr.Govenko
2025-02-20 19:19:41 +00:00
committed by intellij-monorepo-bot
parent ab83df801d
commit 0c54074d30
3 changed files with 15 additions and 1 deletions
@@ -1419,6 +1419,15 @@ public abstract class PyCommonResolveTest extends PyCommonResolveTestCase {
assertEquals("global", ((PyStringLiteralExpression)value).getStringValue());
}
// PY-26947
public void testVariableDeclaredOnClassLevelResolvesOnlyToItself() {
final PyTargetExpression foo = assertResolvesTo(PyTargetExpression.class, "foo");
final PyExpression value = foo.findAssignedValue();
assertInstanceOf(value, PyStringLiteralExpression.class);
assertEquals("correct", ((PyStringLiteralExpression)value).getStringValue());
}
// PY-29975
public void testUnboundVariableOnClassLevelNotDeclaredBelow() {
assertResolvesTo(PyNamedParameter.class, "foo");
@@ -256,7 +256,7 @@ public class PyReferenceImpl implements PsiReferenceEx, PsiPolyVariantReference
})
.toImmutableList();
}
else if (resolvedOwner instanceof PyClass) {
else if (resolvedOwner instanceof PyClass && !(referenceAnchor instanceof PyTargetExpression)) {
resolveInParentScope = () -> PyResolveUtil.parentScopeForUnresolvedClassLevelName((PyClass)resolvedOwner, referencedName);
}
else if (instructions.isEmpty() && allInOwnScopeComprehensions(resolvedElements)) {
@@ -0,0 +1,5 @@
foo = 'incorrect'
class A:
foo = 'correct'
# <ref>