| 1 |
[case testTheBigMatch_python3_10] |
| 2 |
class Person: |
| 3 |
__match_args__ = ("name", "age") |
| 4 |
|
| 5 |
name: str |
| 6 |
age: int |
| 7 |
|
| 8 |
def __init__(self, name: str, age: int) -> None: |
| 9 |
self.name = name |
| 10 |
self.age = age |
| 11 |
|
| 12 |
def __str__(self) -> str: |
| 13 |
return f"Person(name={self.name!r}, age={self.age})" |
| 14 |
|
| 15 |
|
| 16 |
def f(x: object) -> None: |
| 17 |
match x: |
| 18 |
case 123: |
| 19 |
print("test 1") |
| 20 |
|
| 21 |
case 456 | 789: |
| 22 |
print("test 2") |
| 23 |
|
| 24 |
case True | False | None: |
| 25 |
print("test 3") |
| 26 |
|
| 27 |
case Person("bob" as name, age): |
| 28 |
print(f"test 4 ({name=}, {age=})") |
| 29 |
|
| 30 |
case num if num == 5: |
| 31 |
print("test 5") |
| 32 |
|
| 33 |
case 6 as num: |
| 34 |
print(f"test 6 ({num=})") |
| 35 |
|
| 36 |
case (7 | "7") as value: |
| 37 |
print(f"test 7 ({value=})") |
| 38 |
|
| 39 |
case Person("alice", age=123): |
| 40 |
print("test 8") |
| 41 |
|
| 42 |
case Person("charlie", age=123 | 456): |
| 43 |
print("test 9") |
| 44 |
|
| 45 |
case Person("dave", 123) as dave: |
| 46 |
print(f"test 10 {dave}") |
| 47 |
|
| 48 |
case {"test": 11}: |
| 49 |
print("test 11") |
| 50 |
|
| 51 |
case {"test": 12, **rest}: |
| 52 |
print(f"test 12 (rest={rest})") |
| 53 |
|
| 54 |
case {}: |
| 55 |
print("test map final") |
| 56 |
|
| 57 |
case ["test", 13]: |
| 58 |
print("test 13") |
| 59 |
|
| 60 |
case ["test", 13, _]: |
| 61 |
print("test 13b") |
| 62 |
|
| 63 |
case ["test", 14, *_]: |
| 64 |
print("test 14") |
| 65 |
|
| 66 |
# TODO: Fix "rest" being used here coliding with above "rest" |
| 67 |
case ["test", 15, *rest2]: |
| 68 |
print(f"test 15 ({rest2})") |
| 69 |
|
| 70 |
case ["test", *rest3, 16]: |
| 71 |
print(f"test 16 ({rest3})") |
| 72 |
|
| 73 |
case [*rest4, "test", 17]: |
| 74 |
print(f"test 17 ({rest4})") |
| 75 |
|
| 76 |
case [*rest4, "test", 18, "some", "fluff"]: |
| 77 |
print(f"test 18 ({rest4})") |
| 78 |
|
| 79 |
case str("test 19"): |
| 80 |
print("test 19") |
| 81 |
|
| 82 |
case str(test_20) if test_20.startswith("test 20"): |
| 83 |
print(f"test 20 ({test_20[7:]!r})") |
| 84 |
|
| 85 |
case ("test 21" as value) | ("test 21 as well" as value): |
| 86 |
print(f"test 21 ({value[7:]!r})") |
| 87 |
|
| 88 |
case []: |
| 89 |
print("test sequence final") |
| 90 |
|
| 91 |
case _: |
| 92 |
print("test final") |
| 93 |
[file driver.py] |
| 94 |
from native import f, Person |
| 95 |
|
| 96 |
# test 1 |
| 97 |
f(123) |
| 98 |
|
| 99 |
# test 2 |
| 100 |
f(456) |
| 101 |
f(789) |
| 102 |
|
| 103 |
# test 3 |
| 104 |
f(True) |
| 105 |
f(False) |
| 106 |
f(None) |
| 107 |
|
| 108 |
# test 4 |
| 109 |
f(Person("bob", 123)) |
| 110 |
|
| 111 |
# test 5 |
| 112 |
f(5) |
| 113 |
|
| 114 |
# test 6 |
| 115 |
f(6) |
| 116 |
|
| 117 |
# test 7 |
| 118 |
f(7) |
| 119 |
f("7") |
| 120 |
|
| 121 |
# test 8 |
| 122 |
f(Person("alice", 123)) |
| 123 |
|
| 124 |
# test 9 |
| 125 |
f(Person("charlie", 123)) |
| 126 |
f(Person("charlie", 456)) |
| 127 |
|
| 128 |
# test 10 |
| 129 |
f(Person("dave", 123)) |
| 130 |
|
| 131 |
# test 11 |
| 132 |
f({"test": 11}) |
| 133 |
f({"test": 11, "some": "key"}) |
| 134 |
|
| 135 |
# test 12 |
| 136 |
f({"test": 12}) |
| 137 |
f({"test": 12, "key": "value"}) |
| 138 |
f({"test": 12, "key": "value", "abc": "123"}) |
| 139 |
|
| 140 |
# test map final |
| 141 |
f({}) |
| 142 |
|
| 143 |
# test 13 |
| 144 |
f(["test", 13]) |
| 145 |
|
| 146 |
# test 13b |
| 147 |
f(["test", 13, "fail"]) |
| 148 |
|
| 149 |
# test 14 |
| 150 |
f(["test", 14]) |
| 151 |
f(["test", 14, "something"]) |
| 152 |
|
| 153 |
# test 15 |
| 154 |
f(["test", 15]) |
| 155 |
f(["test", 15, "something"]) |
| 156 |
|
| 157 |
# test 16 |
| 158 |
f(["test", 16]) |
| 159 |
f(["test", "filler", 16]) |
| 160 |
f(["test", "more", "filler", 16]) |
| 161 |
|
| 162 |
# test 17 |
| 163 |
f(["test", 17]) |
| 164 |
f(["stuff", "test", 17]) |
| 165 |
f(["more", "stuff", "test", 17]) |
| 166 |
|
| 167 |
# test 18 |
| 168 |
f(["test", 18, "some", "fluff"]) |
| 169 |
f(["stuff", "test", 18, "some", "fluff"]) |
| 170 |
f(["more", "stuff", "test", 18, "some", "fluff"]) |
| 171 |
|
| 172 |
# test 19 |
| 173 |
f("test 19") |
| 174 |
|
| 175 |
# test 20 |
| 176 |
f("test 20") |
| 177 |
f("test 20 something else") |
| 178 |
|
| 179 |
# test 21 |
| 180 |
f("test 21") |
| 181 |
f("test 21 as well") |
| 182 |
|
| 183 |
# test sequence final |
| 184 |
f([]) |
| 185 |
|
| 186 |
# test final |
| 187 |
f("") |
| 188 |
|
| 189 |
[out] |
| 190 |
test 1 |
| 191 |
test 2 |
| 192 |
test 2 |
| 193 |
test 3 |
| 194 |
test 3 |
| 195 |
test 3 |
| 196 |
test 4 (name='bob', age=123) |
| 197 |
test 5 |
| 198 |
test 6 (num=6) |
| 199 |
test 7 (value=7) |
| 200 |
test 7 (value='7') |
| 201 |
test 8 |
| 202 |
test 9 |
| 203 |
test 9 |
| 204 |
test 10 Person(name='dave', age=123) |
| 205 |
test 11 |
| 206 |
test 11 |
| 207 |
test 12 (rest={}) |
| 208 |
test 12 (rest={'key': 'value'}) |
| 209 |
test 12 (rest={'key': 'value', 'abc': '123'}) |
| 210 |
test map final |
| 211 |
test 13 |
| 212 |
test 13b |
| 213 |
test 14 |
| 214 |
test 14 |
| 215 |
test 15 ([]) |
| 216 |
test 15 (['something']) |
| 217 |
test 16 ([]) |
| 218 |
test 16 (['filler']) |
| 219 |
test 16 (['more', 'filler']) |
| 220 |
test 17 ([]) |
| 221 |
test 17 (['stuff']) |
| 222 |
test 17 (['more', 'stuff']) |
| 223 |
test 18 ([]) |
| 224 |
test 18 (['stuff']) |
| 225 |
test 18 (['more', 'stuff']) |
| 226 |
test 19 |
| 227 |
test 20 ('') |
| 228 |
test 20 (' something else') |
| 229 |
test 21 ('') |
| 230 |
test 21 (' as well') |
| 231 |
test sequence final |
| 232 |
test final |
| 233 |
|
| 234 |
[case testMatchOrSequencePattern_python3_10] |
| 235 |
def f(x: tuple[str, str]) -> str: |
| 236 |
match x: |
| 237 |
case ("X", "Y") | ("X", "Z"): |
| 238 |
return "THERE" |
| 239 |
case _: |
| 240 |
return "OTHER" |
| 241 |
|
| 242 |
[file driver.py] |
| 243 |
from native import f |
| 244 |
|
| 245 |
print(f(("X", "Y"))) |
| 246 |
print(f(("X", "Z"))) |
| 247 |
print(f(("X", "A"))) |
| 248 |
|
| 249 |
[out] |
| 250 |
THERE |
| 251 |
THERE |
| 252 |
OTHER |
| 253 |
|
| 254 |
[case testCustomMappingAndSequenceObjects_python3_10] |
| 255 |
def f(x: object) -> None: |
| 256 |
match x: |
| 257 |
case {"key": "value", **rest}: |
| 258 |
print(rest, type(rest)) |
| 259 |
|
| 260 |
case [1, 2, *rest2]: |
| 261 |
print(rest2, type(rest2)) |
| 262 |
|
| 263 |
[file driver.py] |
| 264 |
from collections.abc import Mapping, Sequence |
| 265 |
|
| 266 |
from native import f |
| 267 |
|
| 268 |
class CustomMapping(Mapping): |
| 269 |
inner: dict |
| 270 |
|
| 271 |
def __init__(self, inner: dict) -> None: |
| 272 |
self.inner = inner |
| 273 |
|
| 274 |
def __getitem__(self, key): |
| 275 |
return self.inner[key] |
| 276 |
|
| 277 |
def __iter__(self): |
| 278 |
return iter(self.inner) |
| 279 |
|
| 280 |
def __len__(self) -> int: |
| 281 |
return len(self.inner) |
| 282 |
|
| 283 |
|
| 284 |
class CustomSequence(Sequence): |
| 285 |
inner: list |
| 286 |
|
| 287 |
def __init__(self, inner: list) -> None: |
| 288 |
self.inner = inner |
| 289 |
|
| 290 |
def __getitem__(self, index: int) -> None: |
| 291 |
return self.inner[index] |
| 292 |
|
| 293 |
def __len__(self) -> int: |
| 294 |
return len(self.inner) |
| 295 |
|
| 296 |
mapping = CustomMapping({"key": "value", "some": "data"}) |
| 297 |
sequence = CustomSequence([1, 2, 3]) |
| 298 |
|
| 299 |
f(mapping) |
| 300 |
f(sequence) |
| 301 |
|
| 302 |
[out] |
| 303 |
{'some': 'data'} <class 'dict'> |
| 304 |
[3] <class 'list'> |