PY-81861 Pattern matching for dataclasses - allow matching a subset of attributes

GitOrigin-RevId: 30a865e64cee7be630567b7d23223fc79eac9dbf
This commit is contained in:
Aleksandr.Govenko
2025-07-15 18:59:48 +00:00
committed by intellij-monorepo-bot
parent 3332759510
commit 7efaf9a137
2 changed files with 21 additions and 1 deletions
@@ -115,7 +115,7 @@ public class PyClassPatternImpl extends PyElementImpl implements PyClassPattern
}
List<String> matchArgs = getMatchArgs(classType, context);
if (matchArgs == null || matchArgs.size() > arguments.size()) return null;
if (matchArgs == null || index >= matchArgs.size()) return null;
final PyTypedElement instanceAttribute = as(resolveTypeMember(classType, matchArgs.get(index), context), PyTypedElement.class);
if (instanceAttribute == null) return null;
@@ -672,6 +672,26 @@ match x:
assert_type(z, str)
""");
}
// PY-81861
public void testDataclassPartialCapture() {
doTestByText("""
from dataclasses import dataclass
from typing import assert_type
@dataclass
class A:
a: int
b: str
x = A(1, "a")
match x:
case A(y):
assert_type(y, int)
""");
}
// PY-79832
public void testMatchClassPatternSelfCaptureParameterized() {