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

(cherry picked from commit 30a865e64cee7be630567b7d23223fc79eac9dbf)

IJ-MR-168826

GitOrigin-RevId: 8383913015485a6b6e477131c47d502bb655dd30
This commit is contained in:
Aleksandr.Govenko
2025-07-01 18:51:26 +02:00
committed by intellij-monorepo-bot
parent 0dfd1f65e4
commit 21e8b573a7
2 changed files with 21 additions and 1 deletions

View File

@@ -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;

View File

@@ -673,6 +673,26 @@ match x:
""");
}
// 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() {
doTestByText("""