| 1 |
import array |
| 2 |
import collections |
| 3 |
import dataclasses |
| 4 |
import dis |
| 5 |
import enum |
| 6 |
import inspect |
| 7 |
import sys |
| 8 |
import unittest |
| 9 |
from test import support |
| 10 |
|
| 11 |
|
| 12 |
@dataclasses.dataclass |
| 13 |
class Point: |
| 14 |
x: int |
| 15 |
y: int |
| 16 |
|
| 17 |
|
| 18 |
class TestCompiler(unittest.TestCase): |
| 19 |
|
| 20 |
def test_refleaks(self): |
| 21 |
# Hunting for leaks using -R doesn't catch leaks in the compiler itself, |
| 22 |
# just the code under test. This test ensures that if there are leaks in |
| 23 |
# the pattern compiler, those runs will fail: |
| 24 |
with open(__file__) as file: |
| 25 |
compile(file.read(), __file__, "exec") |
| 26 |
|
| 27 |
|
| 28 |
class TestInheritance(unittest.TestCase): |
| 29 |
|
| 30 |
@staticmethod |
| 31 |
def check_sequence_then_mapping(x): |
| 32 |
match x: |
| 33 |
case [*_]: |
| 34 |
return "seq" |
| 35 |
case {}: |
| 36 |
return "map" |
| 37 |
|
| 38 |
@staticmethod |
| 39 |
def check_mapping_then_sequence(x): |
| 40 |
match x: |
| 41 |
case {}: |
| 42 |
return "map" |
| 43 |
case [*_]: |
| 44 |
return "seq" |
| 45 |
|
| 46 |
def test_multiple_inheritance_mapping(self): |
| 47 |
class C: |
| 48 |
pass |
| 49 |
class M1(collections.UserDict, collections.abc.Sequence): |
| 50 |
pass |
| 51 |
class M2(C, collections.UserDict, collections.abc.Sequence): |
| 52 |
pass |
| 53 |
class M3(collections.UserDict, C, list): |
| 54 |
pass |
| 55 |
class M4(dict, collections.abc.Sequence, C): |
| 56 |
pass |
| 57 |
self.assertEqual(self.check_sequence_then_mapping(M1()), "map") |
| 58 |
self.assertEqual(self.check_sequence_then_mapping(M2()), "map") |
| 59 |
self.assertEqual(self.check_sequence_then_mapping(M3()), "map") |
| 60 |
self.assertEqual(self.check_sequence_then_mapping(M4()), "map") |
| 61 |
self.assertEqual(self.check_mapping_then_sequence(M1()), "map") |
| 62 |
self.assertEqual(self.check_mapping_then_sequence(M2()), "map") |
| 63 |
self.assertEqual(self.check_mapping_then_sequence(M3()), "map") |
| 64 |
self.assertEqual(self.check_mapping_then_sequence(M4()), "map") |
| 65 |
|
| 66 |
def test_multiple_inheritance_sequence(self): |
| 67 |
class C: |
| 68 |
pass |
| 69 |
class S1(collections.UserList, collections.abc.Mapping): |
| 70 |
pass |
| 71 |
class S2(C, collections.UserList, collections.abc.Mapping): |
| 72 |
pass |
| 73 |
class S3(list, C, collections.abc.Mapping): |
| 74 |
pass |
| 75 |
class S4(collections.UserList, dict, C): |
| 76 |
pass |
| 77 |
self.assertEqual(self.check_sequence_then_mapping(S1()), "seq") |
| 78 |
self.assertEqual(self.check_sequence_then_mapping(S2()), "seq") |
| 79 |
self.assertEqual(self.check_sequence_then_mapping(S3()), "seq") |
| 80 |
self.assertEqual(self.check_sequence_then_mapping(S4()), "seq") |
| 81 |
self.assertEqual(self.check_mapping_then_sequence(S1()), "seq") |
| 82 |
self.assertEqual(self.check_mapping_then_sequence(S2()), "seq") |
| 83 |
self.assertEqual(self.check_mapping_then_sequence(S3()), "seq") |
| 84 |
self.assertEqual(self.check_mapping_then_sequence(S4()), "seq") |
| 85 |
|
| 86 |
def test_late_registration_mapping(self): |
| 87 |
class Parent: |
| 88 |
pass |
| 89 |
class ChildPre(Parent): |
| 90 |
pass |
| 91 |
class GrandchildPre(ChildPre): |
| 92 |
pass |
| 93 |
collections.abc.Mapping.register(Parent) |
| 94 |
class ChildPost(Parent): |
| 95 |
pass |
| 96 |
class GrandchildPost(ChildPost): |
| 97 |
pass |
| 98 |
self.assertEqual(self.check_sequence_then_mapping(Parent()), "map") |
| 99 |
self.assertEqual(self.check_sequence_then_mapping(ChildPre()), "map") |
| 100 |
self.assertEqual(self.check_sequence_then_mapping(GrandchildPre()), "map") |
| 101 |
self.assertEqual(self.check_sequence_then_mapping(ChildPost()), "map") |
| 102 |
self.assertEqual(self.check_sequence_then_mapping(GrandchildPost()), "map") |
| 103 |
self.assertEqual(self.check_mapping_then_sequence(Parent()), "map") |
| 104 |
self.assertEqual(self.check_mapping_then_sequence(ChildPre()), "map") |
| 105 |
self.assertEqual(self.check_mapping_then_sequence(GrandchildPre()), "map") |
| 106 |
self.assertEqual(self.check_mapping_then_sequence(ChildPost()), "map") |
| 107 |
self.assertEqual(self.check_mapping_then_sequence(GrandchildPost()), "map") |
| 108 |
|
| 109 |
def test_late_registration_sequence(self): |
| 110 |
class Parent: |
| 111 |
pass |
| 112 |
class ChildPre(Parent): |
| 113 |
pass |
| 114 |
class GrandchildPre(ChildPre): |
| 115 |
pass |
| 116 |
collections.abc.Sequence.register(Parent) |
| 117 |
class ChildPost(Parent): |
| 118 |
pass |
| 119 |
class GrandchildPost(ChildPost): |
| 120 |
pass |
| 121 |
self.assertEqual(self.check_sequence_then_mapping(Parent()), "seq") |
| 122 |
self.assertEqual(self.check_sequence_then_mapping(ChildPre()), "seq") |
| 123 |
self.assertEqual(self.check_sequence_then_mapping(GrandchildPre()), "seq") |
| 124 |
self.assertEqual(self.check_sequence_then_mapping(ChildPost()), "seq") |
| 125 |
self.assertEqual(self.check_sequence_then_mapping(GrandchildPost()), "seq") |
| 126 |
self.assertEqual(self.check_mapping_then_sequence(Parent()), "seq") |
| 127 |
self.assertEqual(self.check_mapping_then_sequence(ChildPre()), "seq") |
| 128 |
self.assertEqual(self.check_mapping_then_sequence(GrandchildPre()), "seq") |
| 129 |
self.assertEqual(self.check_mapping_then_sequence(ChildPost()), "seq") |
| 130 |
self.assertEqual(self.check_mapping_then_sequence(GrandchildPost()), "seq") |
| 131 |
|
| 132 |
|
| 133 |
class TestPatma(unittest.TestCase): |
| 134 |
|
| 135 |
def test_patma_000(self): |
| 136 |
match 0: |
| 137 |
case 0: |
| 138 |
x = True |
| 139 |
self.assertIs(x, True) |
| 140 |
|
| 141 |
def test_patma_001(self): |
| 142 |
match 0: |
| 143 |
case 0 if False: |
| 144 |
x = False |
| 145 |
case 0 if True: |
| 146 |
x = True |
| 147 |
self.assertIs(x, True) |
| 148 |
|
| 149 |
def test_patma_002(self): |
| 150 |
match 0: |
| 151 |
case 0: |
| 152 |
x = True |
| 153 |
case 0: |
| 154 |
x = False |
| 155 |
self.assertIs(x, True) |
| 156 |
|
| 157 |
def test_patma_003(self): |
| 158 |
x = False |
| 159 |
match 0: |
| 160 |
case 0 | 1 | 2 | 3: |
| 161 |
x = True |
| 162 |
self.assertIs(x, True) |
| 163 |
|
| 164 |
def test_patma_004(self): |
| 165 |
x = False |
| 166 |
match 1: |
| 167 |
case 0 | 1 | 2 | 3: |
| 168 |
x = True |
| 169 |
self.assertIs(x, True) |
| 170 |
|
| 171 |
def test_patma_005(self): |
| 172 |
x = False |
| 173 |
match 2: |
| 174 |
case 0 | 1 | 2 | 3: |
| 175 |
x = True |
| 176 |
self.assertIs(x, True) |
| 177 |
|
| 178 |
def test_patma_006(self): |
| 179 |
x = False |
| 180 |
match 3: |
| 181 |
case 0 | 1 | 2 | 3: |
| 182 |
x = True |
| 183 |
self.assertIs(x, True) |
| 184 |
|
| 185 |
def test_patma_007(self): |
| 186 |
x = False |
| 187 |
match 4: |
| 188 |
case 0 | 1 | 2 | 3: |
| 189 |
x = True |
| 190 |
self.assertIs(x, False) |
| 191 |
|
| 192 |
def test_patma_008(self): |
| 193 |
x = 0 |
| 194 |
class A: |
| 195 |
y = 1 |
| 196 |
match x: |
| 197 |
case A.y as z: |
| 198 |
pass |
| 199 |
self.assertEqual(x, 0) |
| 200 |
self.assertEqual(A.y, 1) |
| 201 |
|
| 202 |
def test_patma_009(self): |
| 203 |
class A: |
| 204 |
B = 0 |
| 205 |
match 0: |
| 206 |
case x if x: |
| 207 |
z = 0 |
| 208 |
case _ as y if y == x and y: |
| 209 |
z = 1 |
| 210 |
case A.B: |
| 211 |
z = 2 |
| 212 |
self.assertEqual(A.B, 0) |
| 213 |
self.assertEqual(x, 0) |
| 214 |
self.assertEqual(y, 0) |
| 215 |
self.assertEqual(z, 2) |
| 216 |
|
| 217 |
def test_patma_010(self): |
| 218 |
match (): |
| 219 |
case []: |
| 220 |
x = 0 |
| 221 |
self.assertEqual(x, 0) |
| 222 |
|
| 223 |
def test_patma_011(self): |
| 224 |
match (0, 1, 2): |
| 225 |
case [*x]: |
| 226 |
y = 0 |
| 227 |
self.assertEqual(x, [0, 1, 2]) |
| 228 |
self.assertEqual(y, 0) |
| 229 |
|
| 230 |
def test_patma_012(self): |
| 231 |
match (0, 1, 2): |
| 232 |
case [0, *x]: |
| 233 |
y = 0 |
| 234 |
self.assertEqual(x, [1, 2]) |
| 235 |
self.assertEqual(y, 0) |
| 236 |
|
| 237 |
def test_patma_013(self): |
| 238 |
match (0, 1, 2): |
| 239 |
case [0, 1, *x,]: |
| 240 |
y = 0 |
| 241 |
self.assertEqual(x, [2]) |
| 242 |
self.assertEqual(y, 0) |
| 243 |
|
| 244 |
def test_patma_014(self): |
| 245 |
match (0, 1, 2): |
| 246 |
case [0, 1, 2, *x]: |
| 247 |
y = 0 |
| 248 |
self.assertEqual(x, []) |
| 249 |
self.assertEqual(y, 0) |
| 250 |
|
| 251 |
def test_patma_015(self): |
| 252 |
match (0, 1, 2): |
| 253 |
case [*x, 2,]: |
| 254 |
y = 0 |
| 255 |
self.assertEqual(x, [0, 1]) |
| 256 |
self.assertEqual(y, 0) |
| 257 |
|
| 258 |
def test_patma_016(self): |
| 259 |
match (0, 1, 2): |
| 260 |
case [*x, 1, 2]: |
| 261 |
y = 0 |
| 262 |
self.assertEqual(x, [0]) |
| 263 |
self.assertEqual(y, 0) |
| 264 |
|
| 265 |
def test_patma_017(self): |
| 266 |
match (0, 1, 2): |
| 267 |
case [*x, 0, 1, 2,]: |
| 268 |
y = 0 |
| 269 |
self.assertEqual(x, []) |
| 270 |
self.assertEqual(y, 0) |
| 271 |
|
| 272 |
def test_patma_018(self): |
| 273 |
match (0, 1, 2): |
| 274 |
case [0, *x, 2]: |
| 275 |
y = 0 |
| 276 |
self.assertEqual(x, [1]) |
| 277 |
self.assertEqual(y, 0) |
| 278 |
|
| 279 |
def test_patma_019(self): |
| 280 |
match (0, 1, 2): |
| 281 |
case [0, 1, *x, 2,]: |
| 282 |
y = 0 |
| 283 |
self.assertEqual(x, []) |
| 284 |
self.assertEqual(y, 0) |
| 285 |
|
| 286 |
def test_patma_020(self): |
| 287 |
match (0, 1, 2): |
| 288 |
case [0, *x, 1, 2]: |
| 289 |
y = 0 |
| 290 |
self.assertEqual(x, []) |
| 291 |
self.assertEqual(y, 0) |
| 292 |
|
| 293 |
def test_patma_021(self): |
| 294 |
match (0, 1, 2): |
| 295 |
case [*x,]: |
| 296 |
y = 0 |
| 297 |
self.assertEqual(x, [0, 1, 2]) |
| 298 |
self.assertEqual(y, 0) |
| 299 |
|
| 300 |
def test_patma_022(self): |
| 301 |
x = {} |
| 302 |
match x: |
| 303 |
case {}: |
| 304 |
y = 0 |
| 305 |
self.assertEqual(x, {}) |
| 306 |
self.assertEqual(y, 0) |
| 307 |
|
| 308 |
def test_patma_023(self): |
| 309 |
x = {0: 0} |
| 310 |
match x: |
| 311 |
case {}: |
| 312 |
y = 0 |
| 313 |
self.assertEqual(x, {0: 0}) |
| 314 |
self.assertEqual(y, 0) |
| 315 |
|
| 316 |
def test_patma_024(self): |
| 317 |
x = {} |
| 318 |
y = None |
| 319 |
match x: |
| 320 |
case {0: 0}: |
| 321 |
y = 0 |
| 322 |
self.assertEqual(x, {}) |
| 323 |
self.assertIs(y, None) |
| 324 |
|
| 325 |
def test_patma_025(self): |
| 326 |
x = {0: 0} |
| 327 |
match x: |
| 328 |
case {0: (0 | 1 | 2 as z)}: |
| 329 |
y = 0 |
| 330 |
self.assertEqual(x, {0: 0}) |
| 331 |
self.assertEqual(y, 0) |
| 332 |
self.assertEqual(z, 0) |
| 333 |
|
| 334 |
def test_patma_026(self): |
| 335 |
x = {0: 1} |
| 336 |
match x: |
| 337 |
case {0: (0 | 1 | 2 as z)}: |
| 338 |
y = 0 |
| 339 |
self.assertEqual(x, {0: 1}) |
| 340 |
self.assertEqual(y, 0) |
| 341 |
self.assertEqual(z, 1) |
| 342 |
|
| 343 |
def test_patma_027(self): |
| 344 |
x = {0: 2} |
| 345 |
match x: |
| 346 |
case {0: (0 | 1 | 2 as z)}: |
| 347 |
y = 0 |
| 348 |
self.assertEqual(x, {0: 2}) |
| 349 |
self.assertEqual(y, 0) |
| 350 |
self.assertEqual(z, 2) |
| 351 |
|
| 352 |
def test_patma_028(self): |
| 353 |
x = {0: 3} |
| 354 |
y = None |
| 355 |
match x: |
| 356 |
case {0: (0 | 1 | 2 as z)}: |
| 357 |
y = 0 |
| 358 |
self.assertEqual(x, {0: 3}) |
| 359 |
self.assertIs(y, None) |
| 360 |
|
| 361 |
def test_patma_029(self): |
| 362 |
x = {} |
| 363 |
y = None |
| 364 |
match x: |
| 365 |
case {0: [1, 2, {}]}: |
| 366 |
y = 0 |
| 367 |
case {0: [1, 2, {}], 1: [[]]}: |
| 368 |
y = 1 |
| 369 |
case []: |
| 370 |
y = 2 |
| 371 |
self.assertEqual(x, {}) |
| 372 |
self.assertIs(y, None) |
| 373 |
|
| 374 |
def test_patma_030(self): |
| 375 |
x = {False: (True, 2.0, {})} |
| 376 |
match x: |
| 377 |
case {0: [1, 2, {}]}: |
| 378 |
y = 0 |
| 379 |
case {0: [1, 2, {}], 1: [[]]}: |
| 380 |
y = 1 |
| 381 |
case []: |
| 382 |
y = 2 |
| 383 |
self.assertEqual(x, {False: (True, 2.0, {})}) |
| 384 |
self.assertEqual(y, 0) |
| 385 |
|
| 386 |
def test_patma_031(self): |
| 387 |
x = {False: (True, 2.0, {}), 1: [[]], 2: 0} |
| 388 |
match x: |
| 389 |
case {0: [1, 2, {}]}: |
| 390 |
y = 0 |
| 391 |
case {0: [1, 2, {}], 1: [[]]}: |
| 392 |
y = 1 |
| 393 |
case []: |
| 394 |
y = 2 |
| 395 |
self.assertEqual(x, {False: (True, 2.0, {}), 1: [[]], 2: 0}) |
| 396 |
self.assertEqual(y, 0) |
| 397 |
|
| 398 |
def test_patma_032(self): |
| 399 |
x = {False: (True, 2.0, {}), 1: [[]], 2: 0} |
| 400 |
match x: |
| 401 |
case {0: [1, 2]}: |
| 402 |
y = 0 |
| 403 |
case {0: [1, 2, {}], 1: [[]]}: |
| 404 |
y = 1 |
| 405 |
case []: |
| 406 |
y = 2 |
| 407 |
self.assertEqual(x, {False: (True, 2.0, {}), 1: [[]], 2: 0}) |
| 408 |
self.assertEqual(y, 1) |
| 409 |
|
| 410 |
def test_patma_033(self): |
| 411 |
x = [] |
| 412 |
match x: |
| 413 |
case {0: [1, 2, {}]}: |
| 414 |
y = 0 |
| 415 |
case {0: [1, 2, {}], 1: [[]]}: |
| 416 |
y = 1 |
| 417 |
case []: |
| 418 |
y = 2 |
| 419 |
self.assertEqual(x, []) |
| 420 |
self.assertEqual(y, 2) |
| 421 |
|
| 422 |
def test_patma_034(self): |
| 423 |
x = {0: 0} |
| 424 |
match x: |
| 425 |
case {0: [1, 2, {}]}: |
| 426 |
y = 0 |
| 427 |
case {0: ([1, 2, {}] | False)} | {1: [[]]} | {0: [1, 2, {}]} | [] | "X" | {}: |
| 428 |
y = 1 |
| 429 |
case []: |
| 430 |
y = 2 |
| 431 |
self.assertEqual(x, {0: 0}) |
| 432 |
self.assertEqual(y, 1) |
| 433 |
|
| 434 |
def test_patma_035(self): |
| 435 |
x = {0: 0} |
| 436 |
match x: |
| 437 |
case {0: [1, 2, {}]}: |
| 438 |
y = 0 |
| 439 |
case {0: [1, 2, {}] | True} | {1: [[]]} | {0: [1, 2, {}]} | [] | "X" | {}: |
| 440 |
y = 1 |
| 441 |
case []: |
| 442 |
y = 2 |
| 443 |
self.assertEqual(x, {0: 0}) |
| 444 |
self.assertEqual(y, 1) |
| 445 |
|
| 446 |
def test_patma_036(self): |
| 447 |
x = 0 |
| 448 |
match x: |
| 449 |
case 0 | 1 | 2: |
| 450 |
y = 0 |
| 451 |
self.assertEqual(x, 0) |
| 452 |
self.assertEqual(y, 0) |
| 453 |
|
| 454 |
def test_patma_037(self): |
| 455 |
x = 1 |
| 456 |
match x: |
| 457 |
case 0 | 1 | 2: |
| 458 |
y = 0 |
| 459 |
self.assertEqual(x, 1) |
| 460 |
self.assertEqual(y, 0) |
| 461 |
|
| 462 |
def test_patma_038(self): |
| 463 |
x = 2 |
| 464 |
match x: |
| 465 |
case 0 | 1 | 2: |
| 466 |
y = 0 |
| 467 |
self.assertEqual(x, 2) |
| 468 |
self.assertEqual(y, 0) |
| 469 |
|
| 470 |
def test_patma_039(self): |
| 471 |
x = 3 |
| 472 |
y = None |
| 473 |
match x: |
| 474 |
case 0 | 1 | 2: |
| 475 |
y = 0 |
| 476 |
self.assertEqual(x, 3) |
| 477 |
self.assertIs(y, None) |
| 478 |
|
| 479 |
def test_patma_040(self): |
| 480 |
x = 0 |
| 481 |
match x: |
| 482 |
case (0 as z) | (1 as z) | (2 as z) if z == x % 2: |
| 483 |
y = 0 |
| 484 |
self.assertEqual(x, 0) |
| 485 |
self.assertEqual(y, 0) |
| 486 |
self.assertEqual(z, 0) |
| 487 |
|
| 488 |
def test_patma_041(self): |
| 489 |
x = 1 |
| 490 |
match x: |
| 491 |
case (0 as z) | (1 as z) | (2 as z) if z == x % 2: |
| 492 |
y = 0 |
| 493 |
self.assertEqual(x, 1) |
| 494 |
self.assertEqual(y, 0) |
| 495 |
self.assertEqual(z, 1) |
| 496 |
|
| 497 |
def test_patma_042(self): |
| 498 |
x = 2 |
| 499 |
y = None |
| 500 |
match x: |
| 501 |
case (0 as z) | (1 as z) | (2 as z) if z == x % 2: |
| 502 |
y = 0 |
| 503 |
self.assertEqual(x, 2) |
| 504 |
self.assertIs(y, None) |
| 505 |
self.assertEqual(z, 2) |
| 506 |
|
| 507 |
def test_patma_043(self): |
| 508 |
x = 3 |
| 509 |
y = None |
| 510 |
match x: |
| 511 |
case (0 as z) | (1 as z) | (2 as z) if z == x % 2: |
| 512 |
y = 0 |
| 513 |
self.assertEqual(x, 3) |
| 514 |
self.assertIs(y, None) |
| 515 |
|
| 516 |
def test_patma_044(self): |
| 517 |
x = () |
| 518 |
match x: |
| 519 |
case []: |
| 520 |
y = 0 |
| 521 |
self.assertEqual(x, ()) |
| 522 |
self.assertEqual(y, 0) |
| 523 |
|
| 524 |
def test_patma_045(self): |
| 525 |
x = () |
| 526 |
match x: |
| 527 |
case (): |
| 528 |
y = 0 |
| 529 |
self.assertEqual(x, ()) |
| 530 |
self.assertEqual(y, 0) |
| 531 |
|
| 532 |
def test_patma_046(self): |
| 533 |
x = (0,) |
| 534 |
match x: |
| 535 |
case [0]: |
| 536 |
y = 0 |
| 537 |
self.assertEqual(x, (0,)) |
| 538 |
self.assertEqual(y, 0) |
| 539 |
|
| 540 |
def test_patma_047(self): |
| 541 |
x = ((),) |
| 542 |
match x: |
| 543 |
case [[]]: |
| 544 |
y = 0 |
| 545 |
self.assertEqual(x, ((),)) |
| 546 |
self.assertEqual(y, 0) |
| 547 |
|
| 548 |
def test_patma_048(self): |
| 549 |
x = [0, 1] |
| 550 |
match x: |
| 551 |
case [0, 1] | [1, 0]: |
| 552 |
y = 0 |
| 553 |
self.assertEqual(x, [0, 1]) |
| 554 |
self.assertEqual(y, 0) |
| 555 |
|
| 556 |
def test_patma_049(self): |
| 557 |
x = [1, 0] |
| 558 |
match x: |
| 559 |
case [0, 1] | [1, 0]: |
| 560 |
y = 0 |
| 561 |
self.assertEqual(x, [1, 0]) |
| 562 |
self.assertEqual(y, 0) |
| 563 |
|
| 564 |
def test_patma_050(self): |
| 565 |
x = [0, 0] |
| 566 |
y = None |
| 567 |
match x: |
| 568 |
case [0, 1] | [1, 0]: |
| 569 |
y = 0 |
| 570 |
self.assertEqual(x, [0, 0]) |
| 571 |
self.assertIs(y, None) |
| 572 |
|
| 573 |
def test_patma_051(self): |
| 574 |
w = None |
| 575 |
x = [1, 0] |
| 576 |
match x: |
| 577 |
case [(0 as w)]: |
| 578 |
y = 0 |
| 579 |
case [z] | [1, (0 | 1 as z)] | [z]: |
| 580 |
y = 1 |
| 581 |
self.assertIs(w, None) |
| 582 |
self.assertEqual(x, [1, 0]) |
| 583 |
self.assertEqual(y, 1) |
| 584 |
self.assertEqual(z, 0) |
| 585 |
|
| 586 |
def test_patma_052(self): |
| 587 |
x = [1, 0] |
| 588 |
match x: |
| 589 |
case [0]: |
| 590 |
y = 0 |
| 591 |
case [1, 0] if (x := x[:0]): |
| 592 |
y = 1 |
| 593 |
case [1, 0]: |
| 594 |
y = 2 |
| 595 |
self.assertEqual(x, []) |
| 596 |
self.assertEqual(y, 2) |
| 597 |
|
| 598 |
def test_patma_053(self): |
| 599 |
x = {0} |
| 600 |
y = None |
| 601 |
match x: |
| 602 |
case [0]: |
| 603 |
y = 0 |
| 604 |
self.assertEqual(x, {0}) |
| 605 |
self.assertIs(y, None) |
| 606 |
|
| 607 |
def test_patma_054(self): |
| 608 |
x = set() |
| 609 |
y = None |
| 610 |
match x: |
| 611 |
case []: |
| 612 |
y = 0 |
| 613 |
self.assertEqual(x, set()) |
| 614 |
self.assertIs(y, None) |
| 615 |
|
| 616 |
def test_patma_055(self): |
| 617 |
x = iter([1, 2, 3]) |
| 618 |
y = None |
| 619 |
match x: |
| 620 |
case []: |
| 621 |
y = 0 |
| 622 |
self.assertEqual([*x], [1, 2, 3]) |
| 623 |
self.assertIs(y, None) |
| 624 |
|
| 625 |
def test_patma_056(self): |
| 626 |
x = {} |
| 627 |
y = None |
| 628 |
match x: |
| 629 |
case []: |
| 630 |
y = 0 |
| 631 |
self.assertEqual(x, {}) |
| 632 |
self.assertIs(y, None) |
| 633 |
|
| 634 |
def test_patma_057(self): |
| 635 |
x = {0: False, 1: True} |
| 636 |
y = None |
| 637 |
match x: |
| 638 |
case [0, 1]: |
| 639 |
y = 0 |
| 640 |
self.assertEqual(x, {0: False, 1: True}) |
| 641 |
self.assertIs(y, None) |
| 642 |
|
| 643 |
def test_patma_058(self): |
| 644 |
x = 0 |
| 645 |
match x: |
| 646 |
case 0: |
| 647 |
y = 0 |
| 648 |
self.assertEqual(x, 0) |
| 649 |
self.assertEqual(y, 0) |
| 650 |
|
| 651 |
def test_patma_059(self): |
| 652 |
x = 0 |
| 653 |
y = None |
| 654 |
match x: |
| 655 |
case False: |
| 656 |
y = 0 |
| 657 |
self.assertEqual(x, 0) |
| 658 |
self.assertEqual(y, None) |
| 659 |
|
| 660 |
def test_patma_060(self): |
| 661 |
x = 0 |
| 662 |
y = None |
| 663 |
match x: |
| 664 |
case 1: |
| 665 |
y = 0 |
| 666 |
self.assertEqual(x, 0) |
| 667 |
self.assertIs(y, None) |
| 668 |
|
| 669 |
def test_patma_061(self): |
| 670 |
x = 0 |
| 671 |
y = None |
| 672 |
match x: |
| 673 |
case None: |
| 674 |
y = 0 |
| 675 |
self.assertEqual(x, 0) |
| 676 |
self.assertIs(y, None) |
| 677 |
|
| 678 |
def test_patma_062(self): |
| 679 |
x = 0 |
| 680 |
match x: |
| 681 |
case 0: |
| 682 |
y = 0 |
| 683 |
case 0: |
| 684 |
y = 1 |
| 685 |
self.assertEqual(x, 0) |
| 686 |
self.assertEqual(y, 0) |
| 687 |
|
| 688 |
def test_patma_063(self): |
| 689 |
x = 0 |
| 690 |
y = None |
| 691 |
match x: |
| 692 |
case 1: |
| 693 |
y = 0 |
| 694 |
case 1: |
| 695 |
y = 1 |
| 696 |
self.assertEqual(x, 0) |
| 697 |
self.assertIs(y, None) |
| 698 |
|
| 699 |
def test_patma_064(self): |
| 700 |
x = "x" |
| 701 |
match x: |
| 702 |
case "x": |
| 703 |
y = 0 |
| 704 |
case "y": |
| 705 |
y = 1 |
| 706 |
self.assertEqual(x, "x") |
| 707 |
self.assertEqual(y, 0) |
| 708 |
|
| 709 |
def test_patma_065(self): |
| 710 |
x = "x" |
| 711 |
match x: |
| 712 |
case "y": |
| 713 |
y = 0 |
| 714 |
case "x": |
| 715 |
y = 1 |
| 716 |
self.assertEqual(x, "x") |
| 717 |
self.assertEqual(y, 1) |
| 718 |
|
| 719 |
def test_patma_066(self): |
| 720 |
x = "x" |
| 721 |
match x: |
| 722 |
case "": |
| 723 |
y = 0 |
| 724 |
case "x": |
| 725 |
y = 1 |
| 726 |
self.assertEqual(x, "x") |
| 727 |
self.assertEqual(y, 1) |
| 728 |
|
| 729 |
def test_patma_067(self): |
| 730 |
x = b"x" |
| 731 |
match x: |
| 732 |
case b"y": |
| 733 |
y = 0 |
| 734 |
case b"x": |
| 735 |
y = 1 |
| 736 |
self.assertEqual(x, b"x") |
| 737 |
self.assertEqual(y, 1) |
| 738 |
|
| 739 |
def test_patma_068(self): |
| 740 |
x = 0 |
| 741 |
match x: |
| 742 |
case 0 if False: |
| 743 |
y = 0 |
| 744 |
case 0: |
| 745 |
y = 1 |
| 746 |
self.assertEqual(x, 0) |
| 747 |
self.assertEqual(y, 1) |
| 748 |
|
| 749 |
def test_patma_069(self): |
| 750 |
x = 0 |
| 751 |
y = None |
| 752 |
match x: |
| 753 |
case 0 if 0: |
| 754 |
y = 0 |
| 755 |
case 0 if 0: |
| 756 |
y = 1 |
| 757 |
self.assertEqual(x, 0) |
| 758 |
self.assertIs(y, None) |
| 759 |
|
| 760 |
def test_patma_070(self): |
| 761 |
x = 0 |
| 762 |
match x: |
| 763 |
case 0 if True: |
| 764 |
y = 0 |
| 765 |
case 0 if True: |
| 766 |
y = 1 |
| 767 |
self.assertEqual(x, 0) |
| 768 |
self.assertEqual(y, 0) |
| 769 |
|
| 770 |
def test_patma_071(self): |
| 771 |
x = 0 |
| 772 |
match x: |
| 773 |
case 0 if 1: |
| 774 |
y = 0 |
| 775 |
case 0 if 1: |
| 776 |
y = 1 |
| 777 |
self.assertEqual(x, 0) |
| 778 |
self.assertEqual(y, 0) |
| 779 |
|
| 780 |
def test_patma_072(self): |
| 781 |
x = 0 |
| 782 |
match x: |
| 783 |
case 0 if True: |
| 784 |
y = 0 |
| 785 |
case 0 if True: |
| 786 |
y = 1 |
| 787 |
y = 2 |
| 788 |
self.assertEqual(x, 0) |
| 789 |
self.assertEqual(y, 2) |
| 790 |
|
| 791 |
def test_patma_073(self): |
| 792 |
x = 0 |
| 793 |
match x: |
| 794 |
case 0 if 0: |
| 795 |
y = 0 |
| 796 |
case 0 if 1: |
| 797 |
y = 1 |
| 798 |
y = 2 |
| 799 |
self.assertEqual(x, 0) |
| 800 |
self.assertEqual(y, 2) |
| 801 |
|
| 802 |
def test_patma_074(self): |
| 803 |
x = 0 |
| 804 |
y = None |
| 805 |
match x: |
| 806 |
case 0 if not (x := 1): |
| 807 |
y = 0 |
| 808 |
case 1: |
| 809 |
y = 1 |
| 810 |
self.assertEqual(x, 1) |
| 811 |
self.assertIs(y, None) |
| 812 |
|
| 813 |
def test_patma_075(self): |
| 814 |
x = "x" |
| 815 |
match x: |
| 816 |
case ["x"]: |
| 817 |
y = 0 |
| 818 |
case "x": |
| 819 |
y = 1 |
| 820 |
self.assertEqual(x, "x") |
| 821 |
self.assertEqual(y, 1) |
| 822 |
|
| 823 |
def test_patma_076(self): |
| 824 |
x = b"x" |
| 825 |
match x: |
| 826 |
case [b"x"]: |
| 827 |
y = 0 |
| 828 |
case ["x"]: |
| 829 |
y = 1 |
| 830 |
case [120]: |
| 831 |
y = 2 |
| 832 |
case b"x": |
| 833 |
y = 4 |
| 834 |
self.assertEqual(x, b"x") |
| 835 |
self.assertEqual(y, 4) |
| 836 |
|
| 837 |
def test_patma_077(self): |
| 838 |
x = bytearray(b"x") |
| 839 |
y = None |
| 840 |
match x: |
| 841 |
case [120]: |
| 842 |
y = 0 |
| 843 |
case 120: |
| 844 |
y = 1 |
| 845 |
self.assertEqual(x, b"x") |
| 846 |
self.assertIs(y, None) |
| 847 |
|
| 848 |
def test_patma_078(self): |
| 849 |
x = "" |
| 850 |
match x: |
| 851 |
case []: |
| 852 |
y = 0 |
| 853 |
case [""]: |
| 854 |
y = 1 |
| 855 |
case "": |
| 856 |
y = 2 |
| 857 |
self.assertEqual(x, "") |
| 858 |
self.assertEqual(y, 2) |
| 859 |
|
| 860 |
def test_patma_079(self): |
| 861 |
x = "xxx" |
| 862 |
match x: |
| 863 |
case ["x", "x", "x"]: |
| 864 |
y = 0 |
| 865 |
case ["xxx"]: |
| 866 |
y = 1 |
| 867 |
case "xxx": |
| 868 |
y = 2 |
| 869 |
self.assertEqual(x, "xxx") |
| 870 |
self.assertEqual(y, 2) |
| 871 |
|
| 872 |
def test_patma_080(self): |
| 873 |
x = b"xxx" |
| 874 |
match x: |
| 875 |
case [120, 120, 120]: |
| 876 |
y = 0 |
| 877 |
case [b"xxx"]: |
| 878 |
y = 1 |
| 879 |
case b"xxx": |
| 880 |
y = 2 |
| 881 |
self.assertEqual(x, b"xxx") |
| 882 |
self.assertEqual(y, 2) |
| 883 |
|
| 884 |
def test_patma_081(self): |
| 885 |
x = 0 |
| 886 |
match x: |
| 887 |
case 0 if not (x := 1): |
| 888 |
y = 0 |
| 889 |
case (0 as z): |
| 890 |
y = 1 |
| 891 |
self.assertEqual(x, 1) |
| 892 |
self.assertEqual(y, 1) |
| 893 |
self.assertEqual(z, 0) |
| 894 |
|
| 895 |
def test_patma_082(self): |
| 896 |
x = 0 |
| 897 |
match x: |
| 898 |
case (1 as z) if not (x := 1): |
| 899 |
y = 0 |
| 900 |
case 0: |
| 901 |
y = 1 |
| 902 |
self.assertEqual(x, 0) |
| 903 |
self.assertEqual(y, 1) |
| 904 |
|
| 905 |
def test_patma_083(self): |
| 906 |
x = 0 |
| 907 |
match x: |
| 908 |
case (0 as z): |
| 909 |
y = 0 |
| 910 |
self.assertEqual(x, 0) |
| 911 |
self.assertEqual(y, 0) |
| 912 |
self.assertEqual(z, 0) |
| 913 |
|
| 914 |
def test_patma_084(self): |
| 915 |
x = 0 |
| 916 |
y = None |
| 917 |
match x: |
| 918 |
case (1 as z): |
| 919 |
y = 0 |
| 920 |
self.assertEqual(x, 0) |
| 921 |
self.assertIs(y, None) |
| 922 |
|
| 923 |
def test_patma_085(self): |
| 924 |
x = 0 |
| 925 |
y = None |
| 926 |
match x: |
| 927 |
case (0 as z) if (w := 0): |
| 928 |
y = 0 |
| 929 |
self.assertEqual(w, 0) |
| 930 |
self.assertEqual(x, 0) |
| 931 |
self.assertIs(y, None) |
| 932 |
self.assertEqual(z, 0) |
| 933 |
|
| 934 |
def test_patma_086(self): |
| 935 |
x = 0 |
| 936 |
match x: |
| 937 |
case ((0 as w) as z): |
| 938 |
y = 0 |
| 939 |
self.assertEqual(w, 0) |
| 940 |
self.assertEqual(x, 0) |
| 941 |
self.assertEqual(y, 0) |
| 942 |
self.assertEqual(z, 0) |
| 943 |
|
| 944 |
def test_patma_087(self): |
| 945 |
x = 0 |
| 946 |
match x: |
| 947 |
case (0 | 1) | 2: |
| 948 |
y = 0 |
| 949 |
self.assertEqual(x, 0) |
| 950 |
self.assertEqual(y, 0) |
| 951 |
|
| 952 |
def test_patma_088(self): |
| 953 |
x = 1 |
| 954 |
match x: |
| 955 |
case (0 | 1) | 2: |
| 956 |
y = 0 |
| 957 |
self.assertEqual(x, 1) |
| 958 |
self.assertEqual(y, 0) |
| 959 |
|
| 960 |
def test_patma_089(self): |
| 961 |
x = 2 |
| 962 |
match x: |
| 963 |
case (0 | 1) | 2: |
| 964 |
y = 0 |
| 965 |
self.assertEqual(x, 2) |
| 966 |
self.assertEqual(y, 0) |
| 967 |
|
| 968 |
def test_patma_090(self): |
| 969 |
x = 3 |
| 970 |
y = None |
| 971 |
match x: |
| 972 |
case (0 | 1) | 2: |
| 973 |
y = 0 |
| 974 |
self.assertEqual(x, 3) |
| 975 |
self.assertIs(y, None) |
| 976 |
|
| 977 |
def test_patma_091(self): |
| 978 |
x = 0 |
| 979 |
match x: |
| 980 |
case 0 | (1 | 2): |
| 981 |
y = 0 |
| 982 |
self.assertEqual(x, 0) |
| 983 |
self.assertEqual(y, 0) |
| 984 |
|
| 985 |
def test_patma_092(self): |
| 986 |
x = 1 |
| 987 |
match x: |
| 988 |
case 0 | (1 | 2): |
| 989 |
y = 0 |
| 990 |
self.assertEqual(x, 1) |
| 991 |
self.assertEqual(y, 0) |
| 992 |
|
| 993 |
def test_patma_093(self): |
| 994 |
x = 2 |
| 995 |
match x: |
| 996 |
case 0 | (1 | 2): |
| 997 |
y = 0 |
| 998 |
self.assertEqual(x, 2) |
| 999 |
self.assertEqual(y, 0) |
| 1000 |
|
| 1001 |
def test_patma_094(self): |
| 1002 |
x = 3 |
| 1003 |
y = None |
| 1004 |
match x: |
| 1005 |
case 0 | (1 | 2): |
| 1006 |
y = 0 |
| 1007 |
self.assertEqual(x, 3) |
| 1008 |
self.assertIs(y, None) |
| 1009 |
|
| 1010 |
def test_patma_095(self): |
| 1011 |
x = 0 |
| 1012 |
match x: |
| 1013 |
case -0: |
| 1014 |
y = 0 |
| 1015 |
self.assertEqual(x, 0) |
| 1016 |
self.assertEqual(y, 0) |
| 1017 |
|
| 1018 |
def test_patma_096(self): |
| 1019 |
x = 0 |
| 1020 |
match x: |
| 1021 |
case -0.0: |
| 1022 |
y = 0 |
| 1023 |
self.assertEqual(x, 0) |
| 1024 |
self.assertEqual(y, 0) |
| 1025 |
|
| 1026 |
def test_patma_097(self): |
| 1027 |
x = 0 |
| 1028 |
match x: |
| 1029 |
case -0j: |
| 1030 |
y = 0 |
| 1031 |
self.assertEqual(x, 0) |
| 1032 |
self.assertEqual(y, 0) |
| 1033 |
|
| 1034 |
def test_patma_098(self): |
| 1035 |
x = 0 |
| 1036 |
match x: |
| 1037 |
case -0.0j: |
| 1038 |
y = 0 |
| 1039 |
self.assertEqual(x, 0) |
| 1040 |
self.assertEqual(y, 0) |
| 1041 |
|
| 1042 |
def test_patma_099(self): |
| 1043 |
x = -1 |
| 1044 |
match x: |
| 1045 |
case -1: |
| 1046 |
y = 0 |
| 1047 |
self.assertEqual(x, -1) |
| 1048 |
self.assertEqual(y, 0) |
| 1049 |
|
| 1050 |
def test_patma_100(self): |
| 1051 |
x = -1.5 |
| 1052 |
match x: |
| 1053 |
case -1.5: |
| 1054 |
y = 0 |
| 1055 |
self.assertEqual(x, -1.5) |
| 1056 |
self.assertEqual(y, 0) |
| 1057 |
|
| 1058 |
def test_patma_101(self): |
| 1059 |
x = -1j |
| 1060 |
match x: |
| 1061 |
case -1j: |
| 1062 |
y = 0 |
| 1063 |
self.assertEqual(x, -1j) |
| 1064 |
self.assertEqual(y, 0) |
| 1065 |
|
| 1066 |
def test_patma_102(self): |
| 1067 |
x = -1.5j |
| 1068 |
match x: |
| 1069 |
case -1.5j: |
| 1070 |
y = 0 |
| 1071 |
self.assertEqual(x, -1.5j) |
| 1072 |
self.assertEqual(y, 0) |
| 1073 |
|
| 1074 |
def test_patma_103(self): |
| 1075 |
x = 0 |
| 1076 |
match x: |
| 1077 |
case 0 + 0j: |
| 1078 |
y = 0 |
| 1079 |
self.assertEqual(x, 0) |
| 1080 |
self.assertEqual(y, 0) |
| 1081 |
|
| 1082 |
def test_patma_104(self): |
| 1083 |
x = 0 |
| 1084 |
match x: |
| 1085 |
case 0 - 0j: |
| 1086 |
y = 0 |
| 1087 |
self.assertEqual(x, 0) |
| 1088 |
self.assertEqual(y, 0) |
| 1089 |
|
| 1090 |
def test_patma_105(self): |
| 1091 |
x = 0 |
| 1092 |
match x: |
| 1093 |
case -0 + 0j: |
| 1094 |
y = 0 |
| 1095 |
self.assertEqual(x, 0) |
| 1096 |
self.assertEqual(y, 0) |
| 1097 |
|
| 1098 |
def test_patma_106(self): |
| 1099 |
x = 0 |
| 1100 |
match x: |
| 1101 |
case -0 - 0j: |
| 1102 |
y = 0 |
| 1103 |
self.assertEqual(x, 0) |
| 1104 |
self.assertEqual(y, 0) |
| 1105 |
|
| 1106 |
def test_patma_107(self): |
| 1107 |
x = 0.25 + 1.75j |
| 1108 |
match x: |
| 1109 |
case 0.25 + 1.75j: |
| 1110 |
y = 0 |
| 1111 |
self.assertEqual(x, 0.25 + 1.75j) |
| 1112 |
self.assertEqual(y, 0) |
| 1113 |
|
| 1114 |
def test_patma_108(self): |
| 1115 |
x = 0.25 - 1.75j |
| 1116 |
match x: |
| 1117 |
case 0.25 - 1.75j: |
| 1118 |
y = 0 |
| 1119 |
self.assertEqual(x, 0.25 - 1.75j) |
| 1120 |
self.assertEqual(y, 0) |
| 1121 |
|
| 1122 |
def test_patma_109(self): |
| 1123 |
x = -0.25 + 1.75j |
| 1124 |
match x: |
| 1125 |
case -0.25 + 1.75j: |
| 1126 |
y = 0 |
| 1127 |
self.assertEqual(x, -0.25 + 1.75j) |
| 1128 |
self.assertEqual(y, 0) |
| 1129 |
|
| 1130 |
def test_patma_110(self): |
| 1131 |
x = -0.25 - 1.75j |
| 1132 |
match x: |
| 1133 |
case -0.25 - 1.75j: |
| 1134 |
y = 0 |
| 1135 |
self.assertEqual(x, -0.25 - 1.75j) |
| 1136 |
self.assertEqual(y, 0) |
| 1137 |
|
| 1138 |
def test_patma_111(self): |
| 1139 |
class A: |
| 1140 |
B = 0 |
| 1141 |
x = 0 |
| 1142 |
match x: |
| 1143 |
case A.B: |
| 1144 |
y = 0 |
| 1145 |
self.assertEqual(A.B, 0) |
| 1146 |
self.assertEqual(x, 0) |
| 1147 |
self.assertEqual(y, 0) |
| 1148 |
|
| 1149 |
def test_patma_112(self): |
| 1150 |
class A: |
| 1151 |
class B: |
| 1152 |
C = 0 |
| 1153 |
x = 0 |
| 1154 |
match x: |
| 1155 |
case A.B.C: |
| 1156 |
y = 0 |
| 1157 |
self.assertEqual(A.B.C, 0) |
| 1158 |
self.assertEqual(x, 0) |
| 1159 |
self.assertEqual(y, 0) |
| 1160 |
|
| 1161 |
def test_patma_113(self): |
| 1162 |
class A: |
| 1163 |
class B: |
| 1164 |
C = 0 |
| 1165 |
D = 1 |
| 1166 |
x = 1 |
| 1167 |
match x: |
| 1168 |
case A.B.C: |
| 1169 |
y = 0 |
| 1170 |
case A.B.D: |
| 1171 |
y = 1 |
| 1172 |
self.assertEqual(A.B.C, 0) |
| 1173 |
self.assertEqual(A.B.D, 1) |
| 1174 |
self.assertEqual(x, 1) |
| 1175 |
self.assertEqual(y, 1) |
| 1176 |
|
| 1177 |
def test_patma_114(self): |
| 1178 |
class A: |
| 1179 |
class B: |
| 1180 |
class C: |
| 1181 |
D = 0 |
| 1182 |
x = 0 |
| 1183 |
match x: |
| 1184 |
case A.B.C.D: |
| 1185 |
y = 0 |
| 1186 |
self.assertEqual(A.B.C.D, 0) |
| 1187 |
self.assertEqual(x, 0) |
| 1188 |
self.assertEqual(y, 0) |
| 1189 |
|
| 1190 |
def test_patma_115(self): |
| 1191 |
class A: |
| 1192 |
class B: |
| 1193 |
class C: |
| 1194 |
D = 0 |
| 1195 |
E = 1 |
| 1196 |
x = 1 |
| 1197 |
match x: |
| 1198 |
case A.B.C.D: |
| 1199 |
y = 0 |
| 1200 |
case A.B.C.E: |
| 1201 |
y = 1 |
| 1202 |
self.assertEqual(A.B.C.D, 0) |
| 1203 |
self.assertEqual(A.B.C.E, 1) |
| 1204 |
self.assertEqual(x, 1) |
| 1205 |
self.assertEqual(y, 1) |
| 1206 |
|
| 1207 |
def test_patma_116(self): |
| 1208 |
match = case = 0 |
| 1209 |
match match: |
| 1210 |
case case: |
| 1211 |
x = 0 |
| 1212 |
self.assertEqual(match, 0) |
| 1213 |
self.assertEqual(case, 0) |
| 1214 |
self.assertEqual(x, 0) |
| 1215 |
|
| 1216 |
def test_patma_117(self): |
| 1217 |
match = case = 0 |
| 1218 |
match case: |
| 1219 |
case match: |
| 1220 |
x = 0 |
| 1221 |
self.assertEqual(match, 0) |
| 1222 |
self.assertEqual(case, 0) |
| 1223 |
self.assertEqual(x, 0) |
| 1224 |
|
| 1225 |
def test_patma_118(self): |
| 1226 |
x = [] |
| 1227 |
match x: |
| 1228 |
case [*_, _]: |
| 1229 |
y = 0 |
| 1230 |
case []: |
| 1231 |
y = 1 |
| 1232 |
self.assertEqual(x, []) |
| 1233 |
self.assertEqual(y, 1) |
| 1234 |
|
| 1235 |
def test_patma_119(self): |
| 1236 |
x = collections.defaultdict(int) |
| 1237 |
match x: |
| 1238 |
case {0: 0}: |
| 1239 |
y = 0 |
| 1240 |
case {}: |
| 1241 |
y = 1 |
| 1242 |
self.assertEqual(x, {}) |
| 1243 |
self.assertEqual(y, 1) |
| 1244 |
|
| 1245 |
def test_patma_120(self): |
| 1246 |
x = collections.defaultdict(int) |
| 1247 |
match x: |
| 1248 |
case {0: 0}: |
| 1249 |
y = 0 |
| 1250 |
case {**z}: |
| 1251 |
y = 1 |
| 1252 |
self.assertEqual(x, {}) |
| 1253 |
self.assertEqual(y, 1) |
| 1254 |
self.assertEqual(z, {}) |
| 1255 |
|
| 1256 |
def test_patma_121(self): |
| 1257 |
match (): |
| 1258 |
case (): |
| 1259 |
x = 0 |
| 1260 |
self.assertEqual(x, 0) |
| 1261 |
|
| 1262 |
def test_patma_122(self): |
| 1263 |
match (0, 1, 2): |
| 1264 |
case (*x,): |
| 1265 |
y = 0 |
| 1266 |
self.assertEqual(x, [0, 1, 2]) |
| 1267 |
self.assertEqual(y, 0) |
| 1268 |
|
| 1269 |
def test_patma_123(self): |
| 1270 |
match (0, 1, 2): |
| 1271 |
case 0, *x: |
| 1272 |
y = 0 |
| 1273 |
self.assertEqual(x, [1, 2]) |
| 1274 |
self.assertEqual(y, 0) |
| 1275 |
|
| 1276 |
def test_patma_124(self): |
| 1277 |
match (0, 1, 2): |
| 1278 |
case (0, 1, *x,): |
| 1279 |
y = 0 |
| 1280 |
self.assertEqual(x, [2]) |
| 1281 |
self.assertEqual(y, 0) |
| 1282 |
|
| 1283 |
def test_patma_125(self): |
| 1284 |
match (0, 1, 2): |
| 1285 |
case 0, 1, 2, *x: |
| 1286 |
y = 0 |
| 1287 |
self.assertEqual(x, []) |
| 1288 |
self.assertEqual(y, 0) |
| 1289 |
|
| 1290 |
def test_patma_126(self): |
| 1291 |
match (0, 1, 2): |
| 1292 |
case *x, 2,: |
| 1293 |
y = 0 |
| 1294 |
self.assertEqual(x, [0, 1]) |
| 1295 |
self.assertEqual(y, 0) |
| 1296 |
|
| 1297 |
def test_patma_127(self): |
| 1298 |
match (0, 1, 2): |
| 1299 |
case (*x, 1, 2): |
| 1300 |
y = 0 |
| 1301 |
self.assertEqual(x, [0]) |
| 1302 |
self.assertEqual(y, 0) |
| 1303 |
|
| 1304 |
def test_patma_128(self): |
| 1305 |
match (0, 1, 2): |
| 1306 |
case *x, 0, 1, 2,: |
| 1307 |
y = 0 |
| 1308 |
self.assertEqual(x, []) |
| 1309 |
self.assertEqual(y, 0) |
| 1310 |
|
| 1311 |
def test_patma_129(self): |
| 1312 |
match (0, 1, 2): |
| 1313 |
case (0, *x, 2): |
| 1314 |
y = 0 |
| 1315 |
self.assertEqual(x, [1]) |
| 1316 |
self.assertEqual(y, 0) |
| 1317 |
|
| 1318 |
def test_patma_130(self): |
| 1319 |
match (0, 1, 2): |
| 1320 |
case 0, 1, *x, 2,: |
| 1321 |
y = 0 |
| 1322 |
self.assertEqual(x, []) |
| 1323 |
self.assertEqual(y, 0) |
| 1324 |
|
| 1325 |
def test_patma_131(self): |
| 1326 |
match (0, 1, 2): |
| 1327 |
case (0, *x, 1, 2): |
| 1328 |
y = 0 |
| 1329 |
self.assertEqual(x, []) |
| 1330 |
self.assertEqual(y, 0) |
| 1331 |
|
| 1332 |
def test_patma_132(self): |
| 1333 |
match (0, 1, 2): |
| 1334 |
case *x,: |
| 1335 |
y = 0 |
| 1336 |
self.assertEqual(x, [0, 1, 2]) |
| 1337 |
self.assertEqual(y, 0) |
| 1338 |
|
| 1339 |
def test_patma_133(self): |
| 1340 |
x = collections.defaultdict(int, {0: 1}) |
| 1341 |
match x: |
| 1342 |
case {1: 0}: |
| 1343 |
y = 0 |
| 1344 |
case {0: 0}: |
| 1345 |
y = 1 |
| 1346 |
case {}: |
| 1347 |
y = 2 |
| 1348 |
self.assertEqual(x, {0: 1}) |
| 1349 |
self.assertEqual(y, 2) |
| 1350 |
|
| 1351 |
def test_patma_134(self): |
| 1352 |
x = collections.defaultdict(int, {0: 1}) |
| 1353 |
match x: |
| 1354 |
case {1: 0}: |
| 1355 |
y = 0 |
| 1356 |
case {0: 0}: |
| 1357 |
y = 1 |
| 1358 |
case {**z}: |
| 1359 |
y = 2 |
| 1360 |
self.assertEqual(x, {0: 1}) |
| 1361 |
self.assertEqual(y, 2) |
| 1362 |
self.assertEqual(z, {0: 1}) |
| 1363 |
|
| 1364 |
def test_patma_135(self): |
| 1365 |
x = collections.defaultdict(int, {0: 1}) |
| 1366 |
match x: |
| 1367 |
case {1: 0}: |
| 1368 |
y = 0 |
| 1369 |
case {0: 0}: |
| 1370 |
y = 1 |
| 1371 |
case {0: _, **z}: |
| 1372 |
y = 2 |
| 1373 |
self.assertEqual(x, {0: 1}) |
| 1374 |
self.assertEqual(y, 2) |
| 1375 |
self.assertEqual(z, {}) |
| 1376 |
|
| 1377 |
def test_patma_136(self): |
| 1378 |
x = {0: 1} |
| 1379 |
match x: |
| 1380 |
case {1: 0}: |
| 1381 |
y = 0 |
| 1382 |
case {0: 0}: |
| 1383 |
y = 0 |
| 1384 |
case {}: |
| 1385 |
y = 1 |
| 1386 |
self.assertEqual(x, {0: 1}) |
| 1387 |
self.assertEqual(y, 1) |
| 1388 |
|
| 1389 |
def test_patma_137(self): |
| 1390 |
x = {0: 1} |
| 1391 |
match x: |
| 1392 |
case {1: 0}: |
| 1393 |
y = 0 |
| 1394 |
case {0: 0}: |
| 1395 |
y = 0 |
| 1396 |
case {**z}: |
| 1397 |
y = 1 |
| 1398 |
self.assertEqual(x, {0: 1}) |
| 1399 |
self.assertEqual(y, 1) |
| 1400 |
self.assertEqual(z, {0: 1}) |
| 1401 |
|
| 1402 |
def test_patma_138(self): |
| 1403 |
x = {0: 1} |
| 1404 |
match x: |
| 1405 |
case {1: 0}: |
| 1406 |
y = 0 |
| 1407 |
case {0: 0}: |
| 1408 |
y = 0 |
| 1409 |
case {0: _, **z}: |
| 1410 |
y = 1 |
| 1411 |
self.assertEqual(x, {0: 1}) |
| 1412 |
self.assertEqual(y, 1) |
| 1413 |
self.assertEqual(z, {}) |
| 1414 |
|
| 1415 |
def test_patma_139(self): |
| 1416 |
x = False |
| 1417 |
match x: |
| 1418 |
case bool(z): |
| 1419 |
y = 0 |
| 1420 |
self.assertIs(x, False) |
| 1421 |
self.assertEqual(y, 0) |
| 1422 |
self.assertIs(z, x) |
| 1423 |
|
| 1424 |
def test_patma_140(self): |
| 1425 |
x = True |
| 1426 |
match x: |
| 1427 |
case bool(z): |
| 1428 |
y = 0 |
| 1429 |
self.assertIs(x, True) |
| 1430 |
self.assertEqual(y, 0) |
| 1431 |
self.assertIs(z, x) |
| 1432 |
|
| 1433 |
def test_patma_141(self): |
| 1434 |
x = bytearray() |
| 1435 |
match x: |
| 1436 |
case bytearray(z): |
| 1437 |
y = 0 |
| 1438 |
self.assertEqual(x, bytearray()) |
| 1439 |
self.assertEqual(y, 0) |
| 1440 |
self.assertIs(z, x) |
| 1441 |
|
| 1442 |
def test_patma_142(self): |
| 1443 |
x = b"" |
| 1444 |
match x: |
| 1445 |
case bytes(z): |
| 1446 |
y = 0 |
| 1447 |
self.assertEqual(x, b"") |
| 1448 |
self.assertEqual(y, 0) |
| 1449 |
self.assertIs(z, x) |
| 1450 |
|
| 1451 |
def test_patma_143(self): |
| 1452 |
x = {} |
| 1453 |
match x: |
| 1454 |
case dict(z): |
| 1455 |
y = 0 |
| 1456 |
self.assertEqual(x, {}) |
| 1457 |
self.assertEqual(y, 0) |
| 1458 |
self.assertIs(z, x) |
| 1459 |
|
| 1460 |
def test_patma_144(self): |
| 1461 |
x = 0.0 |
| 1462 |
match x: |
| 1463 |
case float(z): |
| 1464 |
y = 0 |
| 1465 |
self.assertEqual(x, 0.0) |
| 1466 |
self.assertEqual(y, 0) |
| 1467 |
self.assertIs(z, x) |
| 1468 |
|
| 1469 |
def test_patma_145(self): |
| 1470 |
x = frozenset() |
| 1471 |
match x: |
| 1472 |
case frozenset(z): |
| 1473 |
y = 0 |
| 1474 |
self.assertEqual(x, frozenset()) |
| 1475 |
self.assertEqual(y, 0) |
| 1476 |
self.assertIs(z, x) |
| 1477 |
|
| 1478 |
def test_patma_146(self): |
| 1479 |
x = 0 |
| 1480 |
match x: |
| 1481 |
case int(z): |
| 1482 |
y = 0 |
| 1483 |
self.assertEqual(x, 0) |
| 1484 |
self.assertEqual(y, 0) |
| 1485 |
self.assertIs(z, x) |
| 1486 |
|
| 1487 |
def test_patma_147(self): |
| 1488 |
x = [] |
| 1489 |
match x: |
| 1490 |
case list(z): |
| 1491 |
y = 0 |
| 1492 |
self.assertEqual(x, []) |
| 1493 |
self.assertEqual(y, 0) |
| 1494 |
self.assertIs(z, x) |
| 1495 |
|
| 1496 |
def test_patma_148(self): |
| 1497 |
x = set() |
| 1498 |
match x: |
| 1499 |
case set(z): |
| 1500 |
y = 0 |
| 1501 |
self.assertEqual(x, set()) |
| 1502 |
self.assertEqual(y, 0) |
| 1503 |
self.assertIs(z, x) |
| 1504 |
|
| 1505 |
def test_patma_149(self): |
| 1506 |
x = "" |
| 1507 |
match x: |
| 1508 |
case str(z): |
| 1509 |
y = 0 |
| 1510 |
self.assertEqual(x, "") |
| 1511 |
self.assertEqual(y, 0) |
| 1512 |
self.assertIs(z, x) |
| 1513 |
|
| 1514 |
def test_patma_150(self): |
| 1515 |
x = () |
| 1516 |
match x: |
| 1517 |
case tuple(z): |
| 1518 |
y = 0 |
| 1519 |
self.assertEqual(x, ()) |
| 1520 |
self.assertEqual(y, 0) |
| 1521 |
self.assertIs(z, x) |
| 1522 |
|
| 1523 |
def test_patma_151(self): |
| 1524 |
x = 0 |
| 1525 |
match x,: |
| 1526 |
case y,: |
| 1527 |
z = 0 |
| 1528 |
self.assertEqual(x, 0) |
| 1529 |
self.assertIs(y, x) |
| 1530 |
self.assertIs(z, 0) |
| 1531 |
|
| 1532 |
def test_patma_152(self): |
| 1533 |
w = 0 |
| 1534 |
x = 0 |
| 1535 |
match w, x: |
| 1536 |
case y, z: |
| 1537 |
v = 0 |
| 1538 |
self.assertEqual(w, 0) |
| 1539 |
self.assertEqual(x, 0) |
| 1540 |
self.assertIs(y, w) |
| 1541 |
self.assertIs(z, x) |
| 1542 |
self.assertEqual(v, 0) |
| 1543 |
|
| 1544 |
def test_patma_153(self): |
| 1545 |
x = 0 |
| 1546 |
match w := x,: |
| 1547 |
case y as v,: |
| 1548 |
z = 0 |
| 1549 |
self.assertEqual(x, 0) |
| 1550 |
self.assertIs(y, x) |
| 1551 |
self.assertEqual(z, 0) |
| 1552 |
self.assertIs(w, x) |
| 1553 |
self.assertIs(v, y) |
| 1554 |
|
| 1555 |
def test_patma_154(self): |
| 1556 |
x = 0 |
| 1557 |
y = None |
| 1558 |
match x: |
| 1559 |
case 0 if x: |
| 1560 |
y = 0 |
| 1561 |
self.assertEqual(x, 0) |
| 1562 |
self.assertIs(y, None) |
| 1563 |
|
| 1564 |
def test_patma_155(self): |
| 1565 |
x = 0 |
| 1566 |
y = None |
| 1567 |
match x: |
| 1568 |
case 1e1000: |
| 1569 |
y = 0 |
| 1570 |
self.assertEqual(x, 0) |
| 1571 |
self.assertIs(y, None) |
| 1572 |
|
| 1573 |
def test_patma_156(self): |
| 1574 |
x = 0 |
| 1575 |
match x: |
| 1576 |
case z: |
| 1577 |
y = 0 |
| 1578 |
self.assertEqual(x, 0) |
| 1579 |
self.assertEqual(y, 0) |
| 1580 |
self.assertIs(z, x) |
| 1581 |
|
| 1582 |
def test_patma_157(self): |
| 1583 |
x = 0 |
| 1584 |
y = None |
| 1585 |
match x: |
| 1586 |
case _ if x: |
| 1587 |
y = 0 |
| 1588 |
self.assertEqual(x, 0) |
| 1589 |
self.assertIs(y, None) |
| 1590 |
|
| 1591 |
def test_patma_158(self): |
| 1592 |
x = 0 |
| 1593 |
match x: |
| 1594 |
case -1e1000: |
| 1595 |
y = 0 |
| 1596 |
case 0: |
| 1597 |
y = 1 |
| 1598 |
self.assertEqual(x, 0) |
| 1599 |
self.assertEqual(y, 1) |
| 1600 |
|
| 1601 |
def test_patma_159(self): |
| 1602 |
x = 0 |
| 1603 |
match x: |
| 1604 |
case 0 if not x: |
| 1605 |
y = 0 |
| 1606 |
case 1: |
| 1607 |
y = 1 |
| 1608 |
self.assertEqual(x, 0) |
| 1609 |
self.assertEqual(y, 0) |
| 1610 |
|
| 1611 |
def test_patma_160(self): |
| 1612 |
x = 0 |
| 1613 |
z = None |
| 1614 |
match x: |
| 1615 |
case 0: |
| 1616 |
y = 0 |
| 1617 |
case z if x: |
| 1618 |
y = 1 |
| 1619 |
self.assertEqual(x, 0) |
| 1620 |
self.assertEqual(y, 0) |
| 1621 |
self.assertIs(z, None) |
| 1622 |
|
| 1623 |
def test_patma_161(self): |
| 1624 |
x = 0 |
| 1625 |
match x: |
| 1626 |
case 0: |
| 1627 |
y = 0 |
| 1628 |
case _: |
| 1629 |
y = 1 |
| 1630 |
self.assertEqual(x, 0) |
| 1631 |
self.assertEqual(y, 0) |
| 1632 |
|
| 1633 |
def test_patma_162(self): |
| 1634 |
x = 0 |
| 1635 |
match x: |
| 1636 |
case 1 if x: |
| 1637 |
y = 0 |
| 1638 |
case 0: |
| 1639 |
y = 1 |
| 1640 |
self.assertEqual(x, 0) |
| 1641 |
self.assertEqual(y, 1) |
| 1642 |
|
| 1643 |
def test_patma_163(self): |
| 1644 |
x = 0 |
| 1645 |
y = None |
| 1646 |
match x: |
| 1647 |
case 1: |
| 1648 |
y = 0 |
| 1649 |
case 1 if not x: |
| 1650 |
y = 1 |
| 1651 |
self.assertEqual(x, 0) |
| 1652 |
self.assertIs(y, None) |
| 1653 |
|
| 1654 |
def test_patma_164(self): |
| 1655 |
x = 0 |
| 1656 |
match x: |
| 1657 |
case 1: |
| 1658 |
y = 0 |
| 1659 |
case z: |
| 1660 |
y = 1 |
| 1661 |
self.assertEqual(x, 0) |
| 1662 |
self.assertEqual(y, 1) |
| 1663 |
self.assertIs(z, x) |
| 1664 |
|
| 1665 |
def test_patma_165(self): |
| 1666 |
x = 0 |
| 1667 |
match x: |
| 1668 |
case 1 if x: |
| 1669 |
y = 0 |
| 1670 |
case _: |
| 1671 |
y = 1 |
| 1672 |
self.assertEqual(x, 0) |
| 1673 |
self.assertEqual(y, 1) |
| 1674 |
|
| 1675 |
def test_patma_166(self): |
| 1676 |
x = 0 |
| 1677 |
match x: |
| 1678 |
case z if not z: |
| 1679 |
y = 0 |
| 1680 |
case 0 if x: |
| 1681 |
y = 1 |
| 1682 |
self.assertEqual(x, 0) |
| 1683 |
self.assertEqual(y, 0) |
| 1684 |
self.assertIs(z, x) |
| 1685 |
|
| 1686 |
def test_patma_167(self): |
| 1687 |
x = 0 |
| 1688 |
match x: |
| 1689 |
case z if not z: |
| 1690 |
y = 0 |
| 1691 |
case 1: |
| 1692 |
y = 1 |
| 1693 |
self.assertEqual(x, 0) |
| 1694 |
self.assertEqual(y, 0) |
| 1695 |
self.assertIs(z, x) |
| 1696 |
|
| 1697 |
def test_patma_168(self): |
| 1698 |
x = 0 |
| 1699 |
match x: |
| 1700 |
case z if not x: |
| 1701 |
y = 0 |
| 1702 |
case z: |
| 1703 |
y = 1 |
| 1704 |
self.assertEqual(x, 0) |
| 1705 |
self.assertEqual(y, 0) |
| 1706 |
self.assertIs(z, x) |
| 1707 |
|
| 1708 |
def test_patma_169(self): |
| 1709 |
x = 0 |
| 1710 |
match x: |
| 1711 |
case z if not z: |
| 1712 |
y = 0 |
| 1713 |
case _ if x: |
| 1714 |
y = 1 |
| 1715 |
self.assertEqual(x, 0) |
| 1716 |
self.assertEqual(y, 0) |
| 1717 |
self.assertIs(z, x) |
| 1718 |
|
| 1719 |
def test_patma_170(self): |
| 1720 |
x = 0 |
| 1721 |
match x: |
| 1722 |
case _ if not x: |
| 1723 |
y = 0 |
| 1724 |
case 0: |
| 1725 |
y = 1 |
| 1726 |
self.assertEqual(x, 0) |
| 1727 |
self.assertEqual(y, 0) |
| 1728 |
|
| 1729 |
def test_patma_171(self): |
| 1730 |
x = 0 |
| 1731 |
y = None |
| 1732 |
match x: |
| 1733 |
case _ if x: |
| 1734 |
y = 0 |
| 1735 |
case 1: |
| 1736 |
y = 1 |
| 1737 |
self.assertEqual(x, 0) |
| 1738 |
self.assertIs(y, None) |
| 1739 |
|
| 1740 |
def test_patma_172(self): |
| 1741 |
x = 0 |
| 1742 |
z = None |
| 1743 |
match x: |
| 1744 |
case _ if not x: |
| 1745 |
y = 0 |
| 1746 |
case z if not x: |
| 1747 |
y = 1 |
| 1748 |
self.assertEqual(x, 0) |
| 1749 |
self.assertEqual(y, 0) |
| 1750 |
self.assertIs(z, None) |
| 1751 |
|
| 1752 |
def test_patma_173(self): |
| 1753 |
x = 0 |
| 1754 |
match x: |
| 1755 |
case _ if not x: |
| 1756 |
y = 0 |
| 1757 |
case _: |
| 1758 |
y = 1 |
| 1759 |
self.assertEqual(x, 0) |
| 1760 |
self.assertEqual(y, 0) |
| 1761 |
|
| 1762 |
def test_patma_174(self): |
| 1763 |
def http_error(status): |
| 1764 |
match status: |
| 1765 |
case 400: |
| 1766 |
return "Bad request" |
| 1767 |
case 401: |
| 1768 |
return "Unauthorized" |
| 1769 |
case 403: |
| 1770 |
return "Forbidden" |
| 1771 |
case 404: |
| 1772 |
return "Not found" |
| 1773 |
case 418: |
| 1774 |
return "I'm a teapot" |
| 1775 |
case _: |
| 1776 |
return "Something else" |
| 1777 |
self.assertEqual(http_error(400), "Bad request") |
| 1778 |
self.assertEqual(http_error(401), "Unauthorized") |
| 1779 |
self.assertEqual(http_error(403), "Forbidden") |
| 1780 |
self.assertEqual(http_error(404), "Not found") |
| 1781 |
self.assertEqual(http_error(418), "I'm a teapot") |
| 1782 |
self.assertEqual(http_error(123), "Something else") |
| 1783 |
self.assertEqual(http_error("400"), "Something else") |
| 1784 |
self.assertEqual(http_error(401 | 403 | 404), "Something else") # 407 |
| 1785 |
|
| 1786 |
def test_patma_175(self): |
| 1787 |
def http_error(status): |
| 1788 |
match status: |
| 1789 |
case 400: |
| 1790 |
return "Bad request" |
| 1791 |
case 401 | 403 | 404: |
| 1792 |
return "Not allowed" |
| 1793 |
case 418: |
| 1794 |
return "I'm a teapot" |
| 1795 |
self.assertEqual(http_error(400), "Bad request") |
| 1796 |
self.assertEqual(http_error(401), "Not allowed") |
| 1797 |
self.assertEqual(http_error(403), "Not allowed") |
| 1798 |
self.assertEqual(http_error(404), "Not allowed") |
| 1799 |
self.assertEqual(http_error(418), "I'm a teapot") |
| 1800 |
self.assertIs(http_error(123), None) |
| 1801 |
self.assertIs(http_error("400"), None) |
| 1802 |
self.assertIs(http_error(401 | 403 | 404), None) # 407 |
| 1803 |
|
| 1804 |
def test_patma_176(self): |
| 1805 |
def whereis(point): |
| 1806 |
match point: |
| 1807 |
case (0, 0): |
| 1808 |
return "Origin" |
| 1809 |
case (0, y): |
| 1810 |
return f"Y={y}" |
| 1811 |
case (x, 0): |
| 1812 |
return f"X={x}" |
| 1813 |
case (x, y): |
| 1814 |
return f"X={x}, Y={y}" |
| 1815 |
case _: |
| 1816 |
return "Not a point" |
| 1817 |
self.assertEqual(whereis((0, 0)), "Origin") |
| 1818 |
self.assertEqual(whereis((0, -1.0)), "Y=-1.0") |
| 1819 |
self.assertEqual(whereis(("X", 0)), "X=X") |
| 1820 |
self.assertEqual(whereis((None, 1j)), "X=None, Y=1j") |
| 1821 |
self.assertEqual(whereis(42), "Not a point") |
| 1822 |
|
| 1823 |
def test_patma_177(self): |
| 1824 |
def whereis(point): |
| 1825 |
match point: |
| 1826 |
case Point(0, 0): |
| 1827 |
return "Origin" |
| 1828 |
case Point(0, y): |
| 1829 |
return f"Y={y}" |
| 1830 |
case Point(x, 0): |
| 1831 |
return f"X={x}" |
| 1832 |
case Point(): |
| 1833 |
return "Somewhere else" |
| 1834 |
case _: |
| 1835 |
return "Not a point" |
| 1836 |
self.assertEqual(whereis(Point(1, 0)), "X=1") |
| 1837 |
self.assertEqual(whereis(Point(0, 0)), "Origin") |
| 1838 |
self.assertEqual(whereis(10), "Not a point") |
| 1839 |
self.assertEqual(whereis(Point(False, False)), "Origin") |
| 1840 |
self.assertEqual(whereis(Point(0, -1.0)), "Y=-1.0") |
| 1841 |
self.assertEqual(whereis(Point("X", 0)), "X=X") |
| 1842 |
self.assertEqual(whereis(Point(None, 1j)), "Somewhere else") |
| 1843 |
self.assertEqual(whereis(Point), "Not a point") |
| 1844 |
self.assertEqual(whereis(42), "Not a point") |
| 1845 |
|
| 1846 |
def test_patma_178(self): |
| 1847 |
def whereis(point): |
| 1848 |
match point: |
| 1849 |
case Point(1, var): |
| 1850 |
return var |
| 1851 |
self.assertEqual(whereis(Point(1, 0)), 0) |
| 1852 |
self.assertIs(whereis(Point(0, 0)), None) |
| 1853 |
|
| 1854 |
def test_patma_179(self): |
| 1855 |
def whereis(point): |
| 1856 |
match point: |
| 1857 |
case Point(1, y=var): |
| 1858 |
return var |
| 1859 |
self.assertEqual(whereis(Point(1, 0)), 0) |
| 1860 |
self.assertIs(whereis(Point(0, 0)), None) |
| 1861 |
|
| 1862 |
def test_patma_180(self): |
| 1863 |
def whereis(point): |
| 1864 |
match point: |
| 1865 |
case Point(x=1, y=var): |
| 1866 |
return var |
| 1867 |
self.assertEqual(whereis(Point(1, 0)), 0) |
| 1868 |
self.assertIs(whereis(Point(0, 0)), None) |
| 1869 |
|
| 1870 |
def test_patma_181(self): |
| 1871 |
def whereis(point): |
| 1872 |
match point: |
| 1873 |
case Point(y=var, x=1): |
| 1874 |
return var |
| 1875 |
self.assertEqual(whereis(Point(1, 0)), 0) |
| 1876 |
self.assertIs(whereis(Point(0, 0)), None) |
| 1877 |
|
| 1878 |
def test_patma_182(self): |
| 1879 |
def whereis(points): |
| 1880 |
match points: |
| 1881 |
case []: |
| 1882 |
return "No points" |
| 1883 |
case [Point(0, 0)]: |
| 1884 |
return "The origin" |
| 1885 |
case [Point(x, y)]: |
| 1886 |
return f"Single point {x}, {y}" |
| 1887 |
case [Point(0, y1), Point(0, y2)]: |
| 1888 |
return f"Two on the Y axis at {y1}, {y2}" |
| 1889 |
case _: |
| 1890 |
return "Something else" |
| 1891 |
self.assertEqual(whereis([]), "No points") |
| 1892 |
self.assertEqual(whereis([Point(0, 0)]), "The origin") |
| 1893 |
self.assertEqual(whereis([Point(0, 1)]), "Single point 0, 1") |
| 1894 |
self.assertEqual(whereis([Point(0, 0), Point(0, 0)]), "Two on the Y axis at 0, 0") |
| 1895 |
self.assertEqual(whereis([Point(0, 1), Point(0, 1)]), "Two on the Y axis at 1, 1") |
| 1896 |
self.assertEqual(whereis([Point(0, 0), Point(1, 0)]), "Something else") |
| 1897 |
self.assertEqual(whereis([Point(0, 0), Point(0, 0), Point(0, 0)]), "Something else") |
| 1898 |
self.assertEqual(whereis([Point(0, 1), Point(0, 1), Point(0, 1)]), "Something else") |
| 1899 |
|
| 1900 |
def test_patma_183(self): |
| 1901 |
def whereis(point): |
| 1902 |
match point: |
| 1903 |
case Point(x, y) if x == y: |
| 1904 |
return f"Y=X at {x}" |
| 1905 |
case Point(x, y): |
| 1906 |
return "Not on the diagonal" |
| 1907 |
self.assertEqual(whereis(Point(0, 0)), "Y=X at 0") |
| 1908 |
self.assertEqual(whereis(Point(0, False)), "Y=X at 0") |
| 1909 |
self.assertEqual(whereis(Point(False, 0)), "Y=X at False") |
| 1910 |
self.assertEqual(whereis(Point(-1 - 1j, -1 - 1j)), "Y=X at (-1-1j)") |
| 1911 |
self.assertEqual(whereis(Point("X", "X")), "Y=X at X") |
| 1912 |
self.assertEqual(whereis(Point("X", "x")), "Not on the diagonal") |
| 1913 |
|
| 1914 |
def test_patma_184(self): |
| 1915 |
class Seq(collections.abc.Sequence): |
| 1916 |
__getitem__ = None |
| 1917 |
def __len__(self): |
| 1918 |
return 0 |
| 1919 |
match Seq(): |
| 1920 |
case []: |
| 1921 |
y = 0 |
| 1922 |
self.assertEqual(y, 0) |
| 1923 |
|
| 1924 |
def test_patma_185(self): |
| 1925 |
class Seq(collections.abc.Sequence): |
| 1926 |
__getitem__ = None |
| 1927 |
def __len__(self): |
| 1928 |
return 42 |
| 1929 |
match Seq(): |
| 1930 |
case [*_]: |
| 1931 |
y = 0 |
| 1932 |
self.assertEqual(y, 0) |
| 1933 |
|
| 1934 |
def test_patma_186(self): |
| 1935 |
class Seq(collections.abc.Sequence): |
| 1936 |
def __getitem__(self, i): |
| 1937 |
return i |
| 1938 |
def __len__(self): |
| 1939 |
return 42 |
| 1940 |
match Seq(): |
| 1941 |
case [x, *_, y]: |
| 1942 |
z = 0 |
| 1943 |
self.assertEqual(x, 0) |
| 1944 |
self.assertEqual(y, 41) |
| 1945 |
self.assertEqual(z, 0) |
| 1946 |
|
| 1947 |
def test_patma_187(self): |
| 1948 |
w = range(10) |
| 1949 |
match w: |
| 1950 |
case [x, y, *rest]: |
| 1951 |
z = 0 |
| 1952 |
self.assertEqual(w, range(10)) |
| 1953 |
self.assertEqual(x, 0) |
| 1954 |
self.assertEqual(y, 1) |
| 1955 |
self.assertEqual(z, 0) |
| 1956 |
self.assertEqual(rest, list(range(2, 10))) |
| 1957 |
|
| 1958 |
def test_patma_188(self): |
| 1959 |
w = range(100) |
| 1960 |
match w: |
| 1961 |
case (x, y, *rest): |
| 1962 |
z = 0 |
| 1963 |
self.assertEqual(w, range(100)) |
| 1964 |
self.assertEqual(x, 0) |
| 1965 |
self.assertEqual(y, 1) |
| 1966 |
self.assertEqual(z, 0) |
| 1967 |
self.assertEqual(rest, list(range(2, 100))) |
| 1968 |
|
| 1969 |
def test_patma_189(self): |
| 1970 |
w = range(1000) |
| 1971 |
match w: |
| 1972 |
case x, y, *rest: |
| 1973 |
z = 0 |
| 1974 |
self.assertEqual(w, range(1000)) |
| 1975 |
self.assertEqual(x, 0) |
| 1976 |
self.assertEqual(y, 1) |
| 1977 |
self.assertEqual(z, 0) |
| 1978 |
self.assertEqual(rest, list(range(2, 1000))) |
| 1979 |
|
| 1980 |
def test_patma_190(self): |
| 1981 |
w = range(1 << 10) |
| 1982 |
match w: |
| 1983 |
case [x, y, *_]: |
| 1984 |
z = 0 |
| 1985 |
self.assertEqual(w, range(1 << 10)) |
| 1986 |
self.assertEqual(x, 0) |
| 1987 |
self.assertEqual(y, 1) |
| 1988 |
self.assertEqual(z, 0) |
| 1989 |
|
| 1990 |
def test_patma_191(self): |
| 1991 |
w = range(1 << 20) |
| 1992 |
match w: |
| 1993 |
case (x, y, *_): |
| 1994 |
z = 0 |
| 1995 |
self.assertEqual(w, range(1 << 20)) |
| 1996 |
self.assertEqual(x, 0) |
| 1997 |
self.assertEqual(y, 1) |
| 1998 |
self.assertEqual(z, 0) |
| 1999 |
|
| 2000 |
def test_patma_192(self): |
| 2001 |
w = range(1 << 30) |
| 2002 |
match w: |
| 2003 |
case x, y, *_: |
| 2004 |
z = 0 |
| 2005 |
self.assertEqual(w, range(1 << 30)) |
| 2006 |
self.assertEqual(x, 0) |
| 2007 |
self.assertEqual(y, 1) |
| 2008 |
self.assertEqual(z, 0) |
| 2009 |
|
| 2010 |
def test_patma_193(self): |
| 2011 |
x = {"bandwidth": 0, "latency": 1} |
| 2012 |
match x: |
| 2013 |
case {"bandwidth": b, "latency": l}: |
| 2014 |
y = 0 |
| 2015 |
self.assertEqual(x, {"bandwidth": 0, "latency": 1}) |
| 2016 |
self.assertIs(b, x["bandwidth"]) |
| 2017 |
self.assertIs(l, x["latency"]) |
| 2018 |
self.assertEqual(y, 0) |
| 2019 |
|
| 2020 |
def test_patma_194(self): |
| 2021 |
x = {"bandwidth": 0, "latency": 1, "key": "value"} |
| 2022 |
match x: |
| 2023 |
case {"latency": l, "bandwidth": b}: |
| 2024 |
y = 0 |
| 2025 |
self.assertEqual(x, {"bandwidth": 0, "latency": 1, "key": "value"}) |
| 2026 |
self.assertIs(l, x["latency"]) |
| 2027 |
self.assertIs(b, x["bandwidth"]) |
| 2028 |
self.assertEqual(y, 0) |
| 2029 |
|
| 2030 |
def test_patma_195(self): |
| 2031 |
x = {"bandwidth": 0, "latency": 1, "key": "value"} |
| 2032 |
match x: |
| 2033 |
case {"bandwidth": b, "latency": l, **rest}: |
| 2034 |
y = 0 |
| 2035 |
self.assertEqual(x, {"bandwidth": 0, "latency": 1, "key": "value"}) |
| 2036 |
self.assertIs(b, x["bandwidth"]) |
| 2037 |
self.assertIs(l, x["latency"]) |
| 2038 |
self.assertEqual(rest, {"key": "value"}) |
| 2039 |
self.assertEqual(y, 0) |
| 2040 |
|
| 2041 |
def test_patma_196(self): |
| 2042 |
x = {"bandwidth": 0, "latency": 1} |
| 2043 |
match x: |
| 2044 |
case {"latency": l, "bandwidth": b, **rest}: |
| 2045 |
y = 0 |
| 2046 |
self.assertEqual(x, {"bandwidth": 0, "latency": 1}) |
| 2047 |
self.assertIs(l, x["latency"]) |
| 2048 |
self.assertIs(b, x["bandwidth"]) |
| 2049 |
self.assertEqual(rest, {}) |
| 2050 |
self.assertEqual(y, 0) |
| 2051 |
|
| 2052 |
def test_patma_197(self): |
| 2053 |
w = [Point(-1, 0), Point(1, 2)] |
| 2054 |
match w: |
| 2055 |
case (Point(x1, y1), Point(x2, y2) as p2): |
| 2056 |
z = 0 |
| 2057 |
self.assertEqual(w, [Point(-1, 0), Point(1, 2)]) |
| 2058 |
self.assertIs(x1, w[0].x) |
| 2059 |
self.assertIs(y1, w[0].y) |
| 2060 |
self.assertIs(p2, w[1]) |
| 2061 |
self.assertIs(x2, w[1].x) |
| 2062 |
self.assertIs(y2, w[1].y) |
| 2063 |
self.assertIs(z, 0) |
| 2064 |
|
| 2065 |
def test_patma_198(self): |
| 2066 |
class Color(enum.Enum): |
| 2067 |
RED = 0 |
| 2068 |
GREEN = 1 |
| 2069 |
BLUE = 2 |
| 2070 |
def f(color): |
| 2071 |
match color: |
| 2072 |
case Color.RED: |
| 2073 |
return "I see red!" |
| 2074 |
case Color.GREEN: |
| 2075 |
return "Grass is green" |
| 2076 |
case Color.BLUE: |
| 2077 |
return "I'm feeling the blues :(" |
| 2078 |
self.assertEqual(f(Color.RED), "I see red!") |
| 2079 |
self.assertEqual(f(Color.GREEN), "Grass is green") |
| 2080 |
self.assertEqual(f(Color.BLUE), "I'm feeling the blues :(") |
| 2081 |
self.assertIs(f(Color), None) |
| 2082 |
self.assertIs(f(0), None) |
| 2083 |
self.assertIs(f(1), None) |
| 2084 |
self.assertIs(f(2), None) |
| 2085 |
self.assertIs(f(3), None) |
| 2086 |
self.assertIs(f(False), None) |
| 2087 |
self.assertIs(f(True), None) |
| 2088 |
self.assertIs(f(2+0j), None) |
| 2089 |
self.assertIs(f(3.0), None) |
| 2090 |
|
| 2091 |
def test_patma_199(self): |
| 2092 |
class Color(int, enum.Enum): |
| 2093 |
RED = 0 |
| 2094 |
GREEN = 1 |
| 2095 |
BLUE = 2 |
| 2096 |
def f(color): |
| 2097 |
match color: |
| 2098 |
case Color.RED: |
| 2099 |
return "I see red!" |
| 2100 |
case Color.GREEN: |
| 2101 |
return "Grass is green" |
| 2102 |
case Color.BLUE: |
| 2103 |
return "I'm feeling the blues :(" |
| 2104 |
self.assertEqual(f(Color.RED), "I see red!") |
| 2105 |
self.assertEqual(f(Color.GREEN), "Grass is green") |
| 2106 |
self.assertEqual(f(Color.BLUE), "I'm feeling the blues :(") |
| 2107 |
self.assertIs(f(Color), None) |
| 2108 |
self.assertEqual(f(0), "I see red!") |
| 2109 |
self.assertEqual(f(1), "Grass is green") |
| 2110 |
self.assertEqual(f(2), "I'm feeling the blues :(") |
| 2111 |
self.assertIs(f(3), None) |
| 2112 |
self.assertEqual(f(False), "I see red!") |
| 2113 |
self.assertEqual(f(True), "Grass is green") |
| 2114 |
self.assertEqual(f(2+0j), "I'm feeling the blues :(") |
| 2115 |
self.assertIs(f(3.0), None) |
| 2116 |
|
| 2117 |
def test_patma_200(self): |
| 2118 |
class Class: |
| 2119 |
__match_args__ = ("a", "b") |
| 2120 |
c = Class() |
| 2121 |
c.a = 0 |
| 2122 |
c.b = 1 |
| 2123 |
match c: |
| 2124 |
case Class(x, y): |
| 2125 |
z = 0 |
| 2126 |
self.assertIs(x, c.a) |
| 2127 |
self.assertIs(y, c.b) |
| 2128 |
self.assertEqual(z, 0) |
| 2129 |
|
| 2130 |
def test_patma_201(self): |
| 2131 |
class Class: |
| 2132 |
__match_args__ = ("a", "b") |
| 2133 |
c = Class() |
| 2134 |
c.a = 0 |
| 2135 |
c.b = 1 |
| 2136 |
match c: |
| 2137 |
case Class(x, b=y): |
| 2138 |
z = 0 |
| 2139 |
self.assertIs(x, c.a) |
| 2140 |
self.assertIs(y, c.b) |
| 2141 |
self.assertEqual(z, 0) |
| 2142 |
|
| 2143 |
def test_patma_202(self): |
| 2144 |
class Parent: |
| 2145 |
__match_args__ = "a", "b" |
| 2146 |
class Child(Parent): |
| 2147 |
__match_args__ = ("c", "d") |
| 2148 |
c = Child() |
| 2149 |
c.a = 0 |
| 2150 |
c.b = 1 |
| 2151 |
match c: |
| 2152 |
case Parent(x, y): |
| 2153 |
z = 0 |
| 2154 |
self.assertIs(x, c.a) |
| 2155 |
self.assertIs(y, c.b) |
| 2156 |
self.assertEqual(z, 0) |
| 2157 |
|
| 2158 |
def test_patma_203(self): |
| 2159 |
class Parent: |
| 2160 |
__match_args__ = ("a", "b") |
| 2161 |
class Child(Parent): |
| 2162 |
__match_args__ = "c", "d" |
| 2163 |
c = Child() |
| 2164 |
c.a = 0 |
| 2165 |
c.b = 1 |
| 2166 |
match c: |
| 2167 |
case Parent(x, b=y): |
| 2168 |
z = 0 |
| 2169 |
self.assertIs(x, c.a) |
| 2170 |
self.assertIs(y, c.b) |
| 2171 |
self.assertEqual(z, 0) |
| 2172 |
|
| 2173 |
def test_patma_204(self): |
| 2174 |
def f(w): |
| 2175 |
match w: |
| 2176 |
case 42: |
| 2177 |
out = locals() |
| 2178 |
del out["w"] |
| 2179 |
return out |
| 2180 |
self.assertEqual(f(42), {}) |
| 2181 |
self.assertIs(f(0), None) |
| 2182 |
self.assertEqual(f(42.0), {}) |
| 2183 |
self.assertIs(f("42"), None) |
| 2184 |
|
| 2185 |
def test_patma_205(self): |
| 2186 |
def f(w): |
| 2187 |
match w: |
| 2188 |
case 42.0: |
| 2189 |
out = locals() |
| 2190 |
del out["w"] |
| 2191 |
return out |
| 2192 |
self.assertEqual(f(42.0), {}) |
| 2193 |
self.assertEqual(f(42), {}) |
| 2194 |
self.assertIs(f(0.0), None) |
| 2195 |
self.assertIs(f(0), None) |
| 2196 |
|
| 2197 |
def test_patma_206(self): |
| 2198 |
def f(w): |
| 2199 |
match w: |
| 2200 |
case 1 | 2 | 3: |
| 2201 |
out = locals() |
| 2202 |
del out["w"] |
| 2203 |
return out |
| 2204 |
self.assertEqual(f(1), {}) |
| 2205 |
self.assertEqual(f(2), {}) |
| 2206 |
self.assertEqual(f(3), {}) |
| 2207 |
self.assertEqual(f(3.0), {}) |
| 2208 |
self.assertIs(f(0), None) |
| 2209 |
self.assertIs(f(4), None) |
| 2210 |
self.assertIs(f("1"), None) |
| 2211 |
|
| 2212 |
def test_patma_207(self): |
| 2213 |
def f(w): |
| 2214 |
match w: |
| 2215 |
case [1, 2] | [3, 4]: |
| 2216 |
out = locals() |
| 2217 |
del out["w"] |
| 2218 |
return out |
| 2219 |
self.assertEqual(f([1, 2]), {}) |
| 2220 |
self.assertEqual(f([3, 4]), {}) |
| 2221 |
self.assertIs(f(42), None) |
| 2222 |
self.assertIs(f([2, 3]), None) |
| 2223 |
self.assertIs(f([1, 2, 3]), None) |
| 2224 |
self.assertEqual(f([1, 2.0]), {}) |
| 2225 |
|
| 2226 |
def test_patma_208(self): |
| 2227 |
def f(w): |
| 2228 |
match w: |
| 2229 |
case x: |
| 2230 |
out = locals() |
| 2231 |
del out["w"] |
| 2232 |
return out |
| 2233 |
self.assertEqual(f(42), {"x": 42}) |
| 2234 |
self.assertEqual(f((1, 2)), {"x": (1, 2)}) |
| 2235 |
self.assertEqual(f(None), {"x": None}) |
| 2236 |
|
| 2237 |
def test_patma_209(self): |
| 2238 |
def f(w): |
| 2239 |
match w: |
| 2240 |
case _: |
| 2241 |
out = locals() |
| 2242 |
del out["w"] |
| 2243 |
return out |
| 2244 |
self.assertEqual(f(42), {}) |
| 2245 |
self.assertEqual(f(None), {}) |
| 2246 |
self.assertEqual(f((1, 2)), {}) |
| 2247 |
|
| 2248 |
def test_patma_210(self): |
| 2249 |
def f(w): |
| 2250 |
match w: |
| 2251 |
case (x, y, z): |
| 2252 |
out = locals() |
| 2253 |
del out["w"] |
| 2254 |
return out |
| 2255 |
self.assertEqual(f((1, 2, 3)), {"x": 1, "y": 2, "z": 3}) |
| 2256 |
self.assertIs(f((1, 2)), None) |
| 2257 |
self.assertIs(f((1, 2, 3, 4)), None) |
| 2258 |
self.assertIs(f(123), None) |
| 2259 |
self.assertIs(f("abc"), None) |
| 2260 |
self.assertIs(f(b"abc"), None) |
| 2261 |
self.assertEqual(f(array.array("b", b"abc")), {'x': 97, 'y': 98, 'z': 99}) |
| 2262 |
self.assertEqual(f(memoryview(b"abc")), {"x": 97, "y": 98, "z": 99}) |
| 2263 |
self.assertIs(f(bytearray(b"abc")), None) |
| 2264 |
|
| 2265 |
def test_patma_211(self): |
| 2266 |
def f(w): |
| 2267 |
match w: |
| 2268 |
case {"x": x, "y": "y", "z": z}: |
| 2269 |
out = locals() |
| 2270 |
del out["w"] |
| 2271 |
return out |
| 2272 |
self.assertEqual(f({"x": "x", "y": "y", "z": "z"}), {"x": "x", "z": "z"}) |
| 2273 |
self.assertEqual(f({"x": "x", "y": "y", "z": "z", "a": "a"}), {"x": "x", "z": "z"}) |
| 2274 |
self.assertIs(f(({"x": "x", "y": "yy", "z": "z", "a": "a"})), None) |
| 2275 |
self.assertIs(f(({"x": "x", "y": "y"})), None) |
| 2276 |
|
| 2277 |
def test_patma_212(self): |
| 2278 |
def f(w): |
| 2279 |
match w: |
| 2280 |
case Point(int(xx), y="hello"): |
| 2281 |
out = locals() |
| 2282 |
del out["w"] |
| 2283 |
return out |
| 2284 |
self.assertEqual(f(Point(42, "hello")), {"xx": 42}) |
| 2285 |
|
| 2286 |
def test_patma_213(self): |
| 2287 |
def f(w): |
| 2288 |
match w: |
| 2289 |
case (p, q) as x: |
| 2290 |
out = locals() |
| 2291 |
del out["w"] |
| 2292 |
return out |
| 2293 |
self.assertEqual(f((1, 2)), {"p": 1, "q": 2, "x": (1, 2)}) |
| 2294 |
self.assertEqual(f([1, 2]), {"p": 1, "q": 2, "x": [1, 2]}) |
| 2295 |
self.assertIs(f(12), None) |
| 2296 |
self.assertIs(f((1, 2, 3)), None) |
| 2297 |
|
| 2298 |
def test_patma_214(self): |
| 2299 |
def f(): |
| 2300 |
match 42: |
| 2301 |
case 42: |
| 2302 |
return locals() |
| 2303 |
self.assertEqual(set(f()), set()) |
| 2304 |
|
| 2305 |
def test_patma_215(self): |
| 2306 |
def f(): |
| 2307 |
match 1: |
| 2308 |
case 1 | 2 | 3: |
| 2309 |
return locals() |
| 2310 |
self.assertEqual(set(f()), set()) |
| 2311 |
|
| 2312 |
def test_patma_216(self): |
| 2313 |
def f(): |
| 2314 |
match ...: |
| 2315 |
case _: |
| 2316 |
return locals() |
| 2317 |
self.assertEqual(set(f()), set()) |
| 2318 |
|
| 2319 |
def test_patma_217(self): |
| 2320 |
def f(): |
| 2321 |
match ...: |
| 2322 |
case abc: |
| 2323 |
return locals() |
| 2324 |
self.assertEqual(set(f()), {"abc"}) |
| 2325 |
|
| 2326 |
def test_patma_218(self): |
| 2327 |
def f(): |
| 2328 |
match ..., ...: |
| 2329 |
case a, b: |
| 2330 |
return locals() |
| 2331 |
self.assertEqual(set(f()), {"a", "b"}) |
| 2332 |
|
| 2333 |
def test_patma_219(self): |
| 2334 |
def f(): |
| 2335 |
match {"k": ..., "l": ...}: |
| 2336 |
case {"k": a, "l": b}: |
| 2337 |
return locals() |
| 2338 |
self.assertEqual(set(f()), {"a", "b"}) |
| 2339 |
|
| 2340 |
def test_patma_220(self): |
| 2341 |
def f(): |
| 2342 |
match Point(..., ...): |
| 2343 |
case Point(x, y=y): |
| 2344 |
return locals() |
| 2345 |
self.assertEqual(set(f()), {"x", "y"}) |
| 2346 |
|
| 2347 |
def test_patma_221(self): |
| 2348 |
def f(): |
| 2349 |
match ...: |
| 2350 |
case b as a: |
| 2351 |
return locals() |
| 2352 |
self.assertEqual(set(f()), {"a", "b"}) |
| 2353 |
|
| 2354 |
def test_patma_222(self): |
| 2355 |
def f(x): |
| 2356 |
match x: |
| 2357 |
case _: |
| 2358 |
return 0 |
| 2359 |
self.assertEqual(f(0), 0) |
| 2360 |
self.assertEqual(f(1), 0) |
| 2361 |
self.assertEqual(f(2), 0) |
| 2362 |
self.assertEqual(f(3), 0) |
| 2363 |
|
| 2364 |
def test_patma_223(self): |
| 2365 |
def f(x): |
| 2366 |
match x: |
| 2367 |
case 0: |
| 2368 |
return 0 |
| 2369 |
self.assertEqual(f(0), 0) |
| 2370 |
self.assertIs(f(1), None) |
| 2371 |
self.assertIs(f(2), None) |
| 2372 |
self.assertIs(f(3), None) |
| 2373 |
|
| 2374 |
def test_patma_224(self): |
| 2375 |
def f(x): |
| 2376 |
match x: |
| 2377 |
case 0: |
| 2378 |
return 0 |
| 2379 |
case _: |
| 2380 |
return 1 |
| 2381 |
self.assertEqual(f(0), 0) |
| 2382 |
self.assertEqual(f(1), 1) |
| 2383 |
self.assertEqual(f(2), 1) |
| 2384 |
self.assertEqual(f(3), 1) |
| 2385 |
|
| 2386 |
def test_patma_225(self): |
| 2387 |
def f(x): |
| 2388 |
match x: |
| 2389 |
case 0: |
| 2390 |
return 0 |
| 2391 |
case 1: |
| 2392 |
return 1 |
| 2393 |
self.assertEqual(f(0), 0) |
| 2394 |
self.assertEqual(f(1), 1) |
| 2395 |
self.assertIs(f(2), None) |
| 2396 |
self.assertIs(f(3), None) |
| 2397 |
|
| 2398 |
def test_patma_226(self): |
| 2399 |
def f(x): |
| 2400 |
match x: |
| 2401 |
case 0: |
| 2402 |
return 0 |
| 2403 |
case 1: |
| 2404 |
return 1 |
| 2405 |
case _: |
| 2406 |
return 2 |
| 2407 |
self.assertEqual(f(0), 0) |
| 2408 |
self.assertEqual(f(1), 1) |
| 2409 |
self.assertEqual(f(2), 2) |
| 2410 |
self.assertEqual(f(3), 2) |
| 2411 |
|
| 2412 |
def test_patma_227(self): |
| 2413 |
def f(x): |
| 2414 |
match x: |
| 2415 |
case 0: |
| 2416 |
return 0 |
| 2417 |
case 1: |
| 2418 |
return 1 |
| 2419 |
case 2: |
| 2420 |
return 2 |
| 2421 |
self.assertEqual(f(0), 0) |
| 2422 |
self.assertEqual(f(1), 1) |
| 2423 |
self.assertEqual(f(2), 2) |
| 2424 |
self.assertIs(f(3), None) |
| 2425 |
|
| 2426 |
def test_patma_228(self): |
| 2427 |
match(): |
| 2428 |
case(): |
| 2429 |
x = 0 |
| 2430 |
self.assertEqual(x, 0) |
| 2431 |
|
| 2432 |
def test_patma_229(self): |
| 2433 |
x = 0 |
| 2434 |
match(x): |
| 2435 |
case(x): |
| 2436 |
y = 0 |
| 2437 |
self.assertEqual(x, 0) |
| 2438 |
self.assertEqual(y, 0) |
| 2439 |
|
| 2440 |
def test_patma_230(self): |
| 2441 |
x = 0 |
| 2442 |
match x: |
| 2443 |
case False: |
| 2444 |
y = 0 |
| 2445 |
case 0: |
| 2446 |
y = 1 |
| 2447 |
self.assertEqual(x, 0) |
| 2448 |
self.assertEqual(y, 1) |
| 2449 |
|
| 2450 |
def test_patma_231(self): |
| 2451 |
x = 1 |
| 2452 |
match x: |
| 2453 |
case True: |
| 2454 |
y = 0 |
| 2455 |
case 1: |
| 2456 |
y = 1 |
| 2457 |
self.assertEqual(x, 1) |
| 2458 |
self.assertEqual(y, 1) |
| 2459 |
|
| 2460 |
def test_patma_232(self): |
| 2461 |
class Eq: |
| 2462 |
def __eq__(self, other): |
| 2463 |
return True |
| 2464 |
x = eq = Eq() |
| 2465 |
# None |
| 2466 |
y = None |
| 2467 |
match x: |
| 2468 |
case None: |
| 2469 |
y = 0 |
| 2470 |
self.assertIs(x, eq) |
| 2471 |
self.assertEqual(y, None) |
| 2472 |
# True |
| 2473 |
y = None |
| 2474 |
match x: |
| 2475 |
case True: |
| 2476 |
y = 0 |
| 2477 |
self.assertIs(x, eq) |
| 2478 |
self.assertEqual(y, None) |
| 2479 |
# False |
| 2480 |
y = None |
| 2481 |
match x: |
| 2482 |
case False: |
| 2483 |
y = 0 |
| 2484 |
self.assertIs(x, eq) |
| 2485 |
self.assertEqual(y, None) |
| 2486 |
|
| 2487 |
def test_patma_233(self): |
| 2488 |
x = False |
| 2489 |
match x: |
| 2490 |
case False: |
| 2491 |
y = 0 |
| 2492 |
self.assertIs(x, False) |
| 2493 |
self.assertEqual(y, 0) |
| 2494 |
|
| 2495 |
def test_patma_234(self): |
| 2496 |
x = True |
| 2497 |
match x: |
| 2498 |
case True: |
| 2499 |
y = 0 |
| 2500 |
self.assertIs(x, True) |
| 2501 |
self.assertEqual(y, 0) |
| 2502 |
|
| 2503 |
def test_patma_235(self): |
| 2504 |
x = None |
| 2505 |
match x: |
| 2506 |
case None: |
| 2507 |
y = 0 |
| 2508 |
self.assertIs(x, None) |
| 2509 |
self.assertEqual(y, 0) |
| 2510 |
|
| 2511 |
def test_patma_236(self): |
| 2512 |
x = 0 |
| 2513 |
match x: |
| 2514 |
case (0 as w) as z: |
| 2515 |
y = 0 |
| 2516 |
self.assertEqual(w, 0) |
| 2517 |
self.assertEqual(x, 0) |
| 2518 |
self.assertEqual(y, 0) |
| 2519 |
self.assertEqual(z, 0) |
| 2520 |
|
| 2521 |
def test_patma_237(self): |
| 2522 |
x = 0 |
| 2523 |
match x: |
| 2524 |
case (0 as w) as z: |
| 2525 |
y = 0 |
| 2526 |
self.assertEqual(w, 0) |
| 2527 |
self.assertEqual(x, 0) |
| 2528 |
self.assertEqual(y, 0) |
| 2529 |
self.assertEqual(z, 0) |
| 2530 |
|
| 2531 |
def test_patma_238(self): |
| 2532 |
x = ((0, 1), (2, 3)) |
| 2533 |
match x: |
| 2534 |
case ((a as b, c as d) as e) as w, ((f as g, h) as i) as z: |
| 2535 |
y = 0 |
| 2536 |
self.assertEqual(a, 0) |
| 2537 |
self.assertEqual(b, 0) |
| 2538 |
self.assertEqual(c, 1) |
| 2539 |
self.assertEqual(d, 1) |
| 2540 |
self.assertEqual(e, (0, 1)) |
| 2541 |
self.assertEqual(f, 2) |
| 2542 |
self.assertEqual(g, 2) |
| 2543 |
self.assertEqual(h, 3) |
| 2544 |
self.assertEqual(i, (2, 3)) |
| 2545 |
self.assertEqual(w, (0, 1)) |
| 2546 |
self.assertEqual(x, ((0, 1), (2, 3))) |
| 2547 |
self.assertEqual(y, 0) |
| 2548 |
self.assertEqual(z, (2, 3)) |
| 2549 |
|
| 2550 |
def test_patma_239(self): |
| 2551 |
x = collections.UserDict({0: 1, 2: 3}) |
| 2552 |
match x: |
| 2553 |
case {2: 3}: |
| 2554 |
y = 0 |
| 2555 |
self.assertEqual(x, {0: 1, 2: 3}) |
| 2556 |
self.assertEqual(y, 0) |
| 2557 |
|
| 2558 |
def test_patma_240(self): |
| 2559 |
x = collections.UserDict({0: 1, 2: 3}) |
| 2560 |
match x: |
| 2561 |
case {2: 3, **z}: |
| 2562 |
y = 0 |
| 2563 |
self.assertEqual(x, {0: 1, 2: 3}) |
| 2564 |
self.assertEqual(y, 0) |
| 2565 |
self.assertEqual(z, {0: 1}) |
| 2566 |
|
| 2567 |
def test_patma_241(self): |
| 2568 |
x = [[{0: 0}]] |
| 2569 |
match x: |
| 2570 |
case list([({-0-0j: int(real=0+0j, imag=0-0j) | (1) as z},)]): |
| 2571 |
y = 0 |
| 2572 |
self.assertEqual(x, [[{0: 0}]]) |
| 2573 |
self.assertEqual(y, 0) |
| 2574 |
self.assertEqual(z, 0) |
| 2575 |
|
| 2576 |
def test_patma_242(self): |
| 2577 |
x = range(3) |
| 2578 |
match x: |
| 2579 |
case [y, *_, z]: |
| 2580 |
w = 0 |
| 2581 |
self.assertEqual(w, 0) |
| 2582 |
self.assertEqual(x, range(3)) |
| 2583 |
self.assertEqual(y, 0) |
| 2584 |
self.assertEqual(z, 2) |
| 2585 |
|
| 2586 |
def test_patma_243(self): |
| 2587 |
x = range(3) |
| 2588 |
match x: |
| 2589 |
case [_, *_, y]: |
| 2590 |
z = 0 |
| 2591 |
self.assertEqual(x, range(3)) |
| 2592 |
self.assertEqual(y, 2) |
| 2593 |
self.assertEqual(z, 0) |
| 2594 |
|
| 2595 |
def test_patma_244(self): |
| 2596 |
x = range(3) |
| 2597 |
match x: |
| 2598 |
case [*_, y]: |
| 2599 |
z = 0 |
| 2600 |
self.assertEqual(x, range(3)) |
| 2601 |
self.assertEqual(y, 2) |
| 2602 |
self.assertEqual(z, 0) |
| 2603 |
|
| 2604 |
def test_patma_245(self): |
| 2605 |
x = {"y": 1} |
| 2606 |
match x: |
| 2607 |
case {"y": (0 as y) | (1 as y)}: |
| 2608 |
z = 0 |
| 2609 |
self.assertEqual(x, {"y": 1}) |
| 2610 |
self.assertEqual(y, 1) |
| 2611 |
self.assertEqual(z, 0) |
| 2612 |
|
| 2613 |
def test_patma_246(self): |
| 2614 |
def f(x): |
| 2615 |
match x: |
| 2616 |
case ((a, b, c, d, e, f, g, h, i, 9) | |
| 2617 |
(h, g, i, a, b, d, e, c, f, 10) | |
| 2618 |
(g, b, a, c, d, -5, e, h, i, f) | |
| 2619 |
(-1, d, f, b, g, e, i, a, h, c)): |
| 2620 |
w = 0 |
| 2621 |
out = locals() |
| 2622 |
del out["x"] |
| 2623 |
return out |
| 2624 |
alts = [ |
| 2625 |
dict(a=0, b=1, c=2, d=3, e=4, f=5, g=6, h=7, i=8, w=0), |
| 2626 |
dict(h=1, g=2, i=3, a=4, b=5, d=6, e=7, c=8, f=9, w=0), |
| 2627 |
dict(g=0, b=-1, a=-2, c=-3, d=-4, e=-6, h=-7, i=-8, f=-9, w=0), |
| 2628 |
dict(d=-2, f=-3, b=-4, g=-5, e=-6, i=-7, a=-8, h=-9, c=-10, w=0), |
| 2629 |
dict(), |
| 2630 |
] |
| 2631 |
self.assertEqual(f(range(10)), alts[0]) |
| 2632 |
self.assertEqual(f(range(1, 11)), alts[1]) |
| 2633 |
self.assertEqual(f(range(0, -10, -1)), alts[2]) |
| 2634 |
self.assertEqual(f(range(-1, -11, -1)), alts[3]) |
| 2635 |
self.assertEqual(f(range(10, 20)), alts[4]) |
| 2636 |
|
| 2637 |
def test_patma_247(self): |
| 2638 |
def f(x): |
| 2639 |
match x: |
| 2640 |
case [y, (a, b, c, d, e, f, g, h, i, 9) | |
| 2641 |
(h, g, i, a, b, d, e, c, f, 10) | |
| 2642 |
(g, b, a, c, d, -5, e, h, i, f) | |
| 2643 |
(-1, d, f, b, g, e, i, a, h, c), z]: |
| 2644 |
w = 0 |
| 2645 |
out = locals() |
| 2646 |
del out["x"] |
| 2647 |
return out |
| 2648 |
alts = [ |
| 2649 |
dict(a=0, b=1, c=2, d=3, e=4, f=5, g=6, h=7, i=8, w=0, y=False, z=True), |
| 2650 |
dict(h=1, g=2, i=3, a=4, b=5, d=6, e=7, c=8, f=9, w=0, y=False, z=True), |
| 2651 |
dict(g=0, b=-1, a=-2, c=-3, d=-4, e=-6, h=-7, i=-8, f=-9, w=0, y=False, z=True), |
| 2652 |
dict(d=-2, f=-3, b=-4, g=-5, e=-6, i=-7, a=-8, h=-9, c=-10, w=0, y=False, z=True), |
| 2653 |
dict(), |
| 2654 |
] |
| 2655 |
self.assertEqual(f((False, range(10), True)), alts[0]) |
| 2656 |
self.assertEqual(f((False, range(1, 11), True)), alts[1]) |
| 2657 |
self.assertEqual(f((False, range(0, -10, -1), True)), alts[2]) |
| 2658 |
self.assertEqual(f((False, range(-1, -11, -1), True)), alts[3]) |
| 2659 |
self.assertEqual(f((False, range(10, 20), True)), alts[4]) |
| 2660 |
|
| 2661 |
def test_patma_248(self): |
| 2662 |
class C(dict): |
| 2663 |
@staticmethod |
| 2664 |
def get(key, default=None): |
| 2665 |
return 'bar' |
| 2666 |
|
| 2667 |
x = C({'foo': 'bar'}) |
| 2668 |
match x: |
| 2669 |
case {'foo': bar}: |
| 2670 |
y = bar |
| 2671 |
|
| 2672 |
self.assertEqual(y, 'bar') |
| 2673 |
|
| 2674 |
def test_patma_249(self): |
| 2675 |
class C: |
| 2676 |
__attr = "eggs" # mangled to _C__attr |
| 2677 |
_Outer__attr = "bacon" |
| 2678 |
class Outer: |
| 2679 |
def f(self, x): |
| 2680 |
match x: |
| 2681 |
# looks up __attr, not _C__attr or _Outer__attr |
| 2682 |
case C(__attr=y): |
| 2683 |
return y |
| 2684 |
c = C() |
| 2685 |
setattr(c, "__attr", "spam") # setattr is needed because we're in a class scope |
| 2686 |
self.assertEqual(Outer().f(c), "spam") |
| 2687 |
|
| 2688 |
def test_patma_250(self): |
| 2689 |
def f(x): |
| 2690 |
match x: |
| 2691 |
case {"foo": y} if y >= 0: |
| 2692 |
return True |
| 2693 |
case {"foo": y} if y < 0: |
| 2694 |
return False |
| 2695 |
|
| 2696 |
self.assertIs(f({"foo": 1}), True) |
| 2697 |
self.assertIs(f({"foo": -1}), False) |
| 2698 |
|
| 2699 |
def test_patma_251(self): |
| 2700 |
def f(v, x): |
| 2701 |
match v: |
| 2702 |
case x.attr if x.attr >= 0: |
| 2703 |
return True |
| 2704 |
case x.attr if x.attr < 0: |
| 2705 |
return False |
| 2706 |
case _: |
| 2707 |
return None |
| 2708 |
|
| 2709 |
class X: |
| 2710 |
def __init__(self, attr): |
| 2711 |
self.attr = attr |
| 2712 |
|
| 2713 |
self.assertIs(f(1, X(1)), True) |
| 2714 |
self.assertIs(f(-1, X(-1)), False) |
| 2715 |
self.assertIs(f(1, X(-1)), None) |
| 2716 |
|
| 2717 |
def test_patma_252(self): |
| 2718 |
# Side effects must be possible in guards: |
| 2719 |
effects = [] |
| 2720 |
def lt(x, y): |
| 2721 |
effects.append((x, y)) |
| 2722 |
return x < y |
| 2723 |
|
| 2724 |
res = None |
| 2725 |
match {"foo": 1}: |
| 2726 |
case {"foo": x} if lt(x, 0): |
| 2727 |
res = 0 |
| 2728 |
case {"foo": x} if lt(x, 1): |
| 2729 |
res = 1 |
| 2730 |
case {"foo": x} if lt(x, 2): |
| 2731 |
res = 2 |
| 2732 |
|
| 2733 |
self.assertEqual(res, 2) |
| 2734 |
self.assertEqual(effects, [(1, 0), (1, 1), (1, 2)]) |
| 2735 |
|
| 2736 |
def test_patma_253(self): |
| 2737 |
def f(v): |
| 2738 |
match v: |
| 2739 |
case [x] | x: |
| 2740 |
return x |
| 2741 |
|
| 2742 |
self.assertEqual(f(1), 1) |
| 2743 |
self.assertEqual(f([1]), 1) |
| 2744 |
|
| 2745 |
def test_patma_254(self): |
| 2746 |
def f(v): |
| 2747 |
match v: |
| 2748 |
case {"x": x} | x: |
| 2749 |
return x |
| 2750 |
|
| 2751 |
self.assertEqual(f(1), 1) |
| 2752 |
self.assertEqual(f({"x": 1}), 1) |
| 2753 |
|
| 2754 |
def test_patma_255(self): |
| 2755 |
x = [] |
| 2756 |
match x: |
| 2757 |
case [] as z if z.append(None): |
| 2758 |
y = 0 |
| 2759 |
case [None]: |
| 2760 |
y = 1 |
| 2761 |
self.assertEqual(x, [None]) |
| 2762 |
self.assertEqual(y, 1) |
| 2763 |
self.assertIs(z, x) |
| 2764 |
|
| 2765 |
def test_patma_runtime_checkable_protocol(self): |
| 2766 |
# Runtime-checkable protocol |
| 2767 |
from typing import Protocol, runtime_checkable |
| 2768 |
|
| 2769 |
@runtime_checkable |
| 2770 |
class P(Protocol): |
| 2771 |
x: int |
| 2772 |
y: int |
| 2773 |
|
| 2774 |
class A: |
| 2775 |
def __init__(self, x: int, y: int): |
| 2776 |
self.x = x |
| 2777 |
self.y = y |
| 2778 |
|
| 2779 |
class B(A): ... |
| 2780 |
|
| 2781 |
for cls in (A, B): |
| 2782 |
with self.subTest(cls=cls.__name__): |
| 2783 |
inst = cls(1, 2) |
| 2784 |
w = 0 |
| 2785 |
match inst: |
| 2786 |
case P() as p: |
| 2787 |
self.assertIsInstance(p, cls) |
| 2788 |
self.assertEqual(p.x, 1) |
| 2789 |
self.assertEqual(p.y, 2) |
| 2790 |
w = 1 |
| 2791 |
self.assertEqual(w, 1) |
| 2792 |
|
| 2793 |
q = 0 |
| 2794 |
match inst: |
| 2795 |
case P(x=x, y=y): |
| 2796 |
self.assertEqual(x, 1) |
| 2797 |
self.assertEqual(y, 2) |
| 2798 |
q = 1 |
| 2799 |
self.assertEqual(q, 1) |
| 2800 |
|
| 2801 |
|
| 2802 |
def test_patma_generic_protocol(self): |
| 2803 |
# Runtime-checkable generic protocol |
| 2804 |
from typing import Generic, TypeVar, Protocol, runtime_checkable |
| 2805 |
|
| 2806 |
T = TypeVar('T') # not using PEP695 to be able to backport changes |
| 2807 |
|
| 2808 |
@runtime_checkable |
| 2809 |
class P(Protocol[T]): |
| 2810 |
a: T |
| 2811 |
b: T |
| 2812 |
|
| 2813 |
class A: |
| 2814 |
def __init__(self, x: int, y: int): |
| 2815 |
self.x = x |
| 2816 |
self.y = y |
| 2817 |
|
| 2818 |
class G(Generic[T]): |
| 2819 |
def __init__(self, x: T, y: T): |
| 2820 |
self.x = x |
| 2821 |
self.y = y |
| 2822 |
|
| 2823 |
for cls in (A, G): |
| 2824 |
with self.subTest(cls=cls.__name__): |
| 2825 |
inst = cls(1, 2) |
| 2826 |
w = 0 |
| 2827 |
match inst: |
| 2828 |
case P(): |
| 2829 |
w = 1 |
| 2830 |
self.assertEqual(w, 0) |
| 2831 |
|
| 2832 |
def test_patma_protocol_with_match_args(self): |
| 2833 |
# Runtime-checkable protocol with `__match_args__` |
| 2834 |
from typing import Protocol, runtime_checkable |
| 2835 |
|
| 2836 |
# Used to fail before |
| 2837 |
# https://github.com/python/cpython/issues/110682 |
| 2838 |
@runtime_checkable |
| 2839 |
class P(Protocol): |
| 2840 |
__match_args__ = ('x', 'y') |
| 2841 |
x: int |
| 2842 |
y: int |
| 2843 |
|
| 2844 |
class A: |
| 2845 |
def __init__(self, x: int, y: int): |
| 2846 |
self.x = x |
| 2847 |
self.y = y |
| 2848 |
|
| 2849 |
class B(A): ... |
| 2850 |
|
| 2851 |
for cls in (A, B): |
| 2852 |
with self.subTest(cls=cls.__name__): |
| 2853 |
inst = cls(1, 2) |
| 2854 |
w = 0 |
| 2855 |
match inst: |
| 2856 |
case P() as p: |
| 2857 |
self.assertIsInstance(p, cls) |
| 2858 |
self.assertEqual(p.x, 1) |
| 2859 |
self.assertEqual(p.y, 2) |
| 2860 |
w = 1 |
| 2861 |
self.assertEqual(w, 1) |
| 2862 |
|
| 2863 |
q = 0 |
| 2864 |
match inst: |
| 2865 |
case P(x=x, y=y): |
| 2866 |
self.assertEqual(x, 1) |
| 2867 |
self.assertEqual(y, 2) |
| 2868 |
q = 1 |
| 2869 |
self.assertEqual(q, 1) |
| 2870 |
|
| 2871 |
j = 0 |
| 2872 |
match inst: |
| 2873 |
case P(x=1, y=2): |
| 2874 |
j = 1 |
| 2875 |
self.assertEqual(j, 1) |
| 2876 |
|
| 2877 |
g = 0 |
| 2878 |
match inst: |
| 2879 |
case P(x, y): |
| 2880 |
self.assertEqual(x, 1) |
| 2881 |
self.assertEqual(y, 2) |
| 2882 |
g = 1 |
| 2883 |
self.assertEqual(g, 1) |
| 2884 |
|
| 2885 |
h = 0 |
| 2886 |
match inst: |
| 2887 |
case P(1, 2): |
| 2888 |
h = 1 |
| 2889 |
self.assertEqual(h, 1) |
| 2890 |
|
| 2891 |
|
| 2892 |
class TestSyntaxErrors(unittest.TestCase): |
| 2893 |
|
| 2894 |
def assert_syntax_error(self, code: str): |
| 2895 |
with self.assertRaises(SyntaxError): |
| 2896 |
compile(inspect.cleandoc(code), "<test>", "exec") |
| 2897 |
|
| 2898 |
def test_alternative_patterns_bind_different_names_0(self): |
| 2899 |
self.assert_syntax_error(""" |
| 2900 |
match ...: |
| 2901 |
case "a" | a: |
| 2902 |
pass |
| 2903 |
""") |
| 2904 |
|
| 2905 |
def test_alternative_patterns_bind_different_names_1(self): |
| 2906 |
self.assert_syntax_error(""" |
| 2907 |
match ...: |
| 2908 |
case [a, [b] | [c] | [d]]: |
| 2909 |
pass |
| 2910 |
""") |
| 2911 |
|
| 2912 |
|
| 2913 |
def test_attribute_name_repeated_in_class_pattern(self): |
| 2914 |
self.assert_syntax_error(""" |
| 2915 |
match ...: |
| 2916 |
case Class(a=_, a=_): |
| 2917 |
pass |
| 2918 |
""") |
| 2919 |
|
| 2920 |
def test_imaginary_number_required_in_complex_literal_0(self): |
| 2921 |
self.assert_syntax_error(""" |
| 2922 |
match ...: |
| 2923 |
case 0+0: |
| 2924 |
pass |
| 2925 |
""") |
| 2926 |
|
| 2927 |
def test_imaginary_number_required_in_complex_literal_1(self): |
| 2928 |
self.assert_syntax_error(""" |
| 2929 |
match ...: |
| 2930 |
case {0+0: _}: |
| 2931 |
pass |
| 2932 |
""") |
| 2933 |
|
| 2934 |
def test_invalid_syntax_0(self): |
| 2935 |
self.assert_syntax_error(""" |
| 2936 |
match ...: |
| 2937 |
case {**rest, "key": value}: |
| 2938 |
pass |
| 2939 |
""") |
| 2940 |
|
| 2941 |
def test_invalid_syntax_1(self): |
| 2942 |
self.assert_syntax_error(""" |
| 2943 |
match ...: |
| 2944 |
case {"first": first, **rest, "last": last}: |
| 2945 |
pass |
| 2946 |
""") |
| 2947 |
|
| 2948 |
def test_invalid_syntax_2(self): |
| 2949 |
self.assert_syntax_error(""" |
| 2950 |
match ...: |
| 2951 |
case {**_}: |
| 2952 |
pass |
| 2953 |
""") |
| 2954 |
|
| 2955 |
def test_invalid_syntax_3(self): |
| 2956 |
self.assert_syntax_error(""" |
| 2957 |
match ...: |
| 2958 |
case 42 as _: |
| 2959 |
pass |
| 2960 |
""") |
| 2961 |
|
| 2962 |
def test_len1_tuple_sequence_pattern_comma(self): |
| 2963 |
# correct syntax would be `case(*x,):` |
| 2964 |
self.assert_syntax_error(""" |
| 2965 |
match ...: |
| 2966 |
case (*x): |
| 2967 |
pass |
| 2968 |
""") |
| 2969 |
|
| 2970 |
def test_mapping_pattern_keys_may_only_match_literals_and_attribute_lookups(self): |
| 2971 |
self.assert_syntax_error(""" |
| 2972 |
match ...: |
| 2973 |
case {f"": _}: |
| 2974 |
pass |
| 2975 |
""") |
| 2976 |
|
| 2977 |
def test_multiple_assignments_to_name_in_pattern_0(self): |
| 2978 |
self.assert_syntax_error(""" |
| 2979 |
match ...: |
| 2980 |
case a, a: |
| 2981 |
pass |
| 2982 |
""") |
| 2983 |
|
| 2984 |
def test_multiple_assignments_to_name_in_pattern_1(self): |
| 2985 |
self.assert_syntax_error(""" |
| 2986 |
match ...: |
| 2987 |
case {"k": a, "l": a}: |
| 2988 |
pass |
| 2989 |
""") |
| 2990 |
|
| 2991 |
def test_multiple_assignments_to_name_in_pattern_2(self): |
| 2992 |
self.assert_syntax_error(""" |
| 2993 |
match ...: |
| 2994 |
case MyClass(x, x): |
| 2995 |
pass |
| 2996 |
""") |
| 2997 |
|
| 2998 |
def test_multiple_assignments_to_name_in_pattern_3(self): |
| 2999 |
self.assert_syntax_error(""" |
| 3000 |
match ...: |
| 3001 |
case MyClass(x=x, y=x): |
| 3002 |
pass |
| 3003 |
""") |
| 3004 |
|
| 3005 |
def test_multiple_assignments_to_name_in_pattern_4(self): |
| 3006 |
self.assert_syntax_error(""" |
| 3007 |
match ...: |
| 3008 |
case MyClass(x, y=x): |
| 3009 |
pass |
| 3010 |
""") |
| 3011 |
|
| 3012 |
def test_multiple_assignments_to_name_in_pattern_5(self): |
| 3013 |
self.assert_syntax_error(""" |
| 3014 |
match ...: |
| 3015 |
case a as a: |
| 3016 |
pass |
| 3017 |
""") |
| 3018 |
|
| 3019 |
def test_multiple_assignments_to_name_in_pattern_6(self): |
| 3020 |
self.assert_syntax_error(""" |
| 3021 |
match ...: |
| 3022 |
case a as a + 1: # NAME and expression with no () |
| 3023 |
pass |
| 3024 |
""") |
| 3025 |
|
| 3026 |
def test_multiple_starred_names_in_sequence_pattern_0(self): |
| 3027 |
self.assert_syntax_error(""" |
| 3028 |
match ...: |
| 3029 |
case *a, b, *c, d, *e: |
| 3030 |
pass |
| 3031 |
""") |
| 3032 |
|
| 3033 |
def test_multiple_starred_names_in_sequence_pattern_1(self): |
| 3034 |
self.assert_syntax_error(""" |
| 3035 |
match ...: |
| 3036 |
case a, *b, c, *d, e: |
| 3037 |
pass |
| 3038 |
""") |
| 3039 |
|
| 3040 |
def test_name_capture_makes_remaining_patterns_unreachable_0(self): |
| 3041 |
self.assert_syntax_error(""" |
| 3042 |
match ...: |
| 3043 |
case a | "a": |
| 3044 |
pass |
| 3045 |
""") |
| 3046 |
|
| 3047 |
def test_name_capture_makes_remaining_patterns_unreachable_1(self): |
| 3048 |
self.assert_syntax_error(""" |
| 3049 |
match 42: |
| 3050 |
case x: |
| 3051 |
pass |
| 3052 |
case y: |
| 3053 |
pass |
| 3054 |
""") |
| 3055 |
|
| 3056 |
def test_name_capture_makes_remaining_patterns_unreachable_2(self): |
| 3057 |
self.assert_syntax_error(""" |
| 3058 |
match ...: |
| 3059 |
case x | [_ as x] if x: |
| 3060 |
pass |
| 3061 |
""") |
| 3062 |
|
| 3063 |
def test_name_capture_makes_remaining_patterns_unreachable_3(self): |
| 3064 |
self.assert_syntax_error(""" |
| 3065 |
match ...: |
| 3066 |
case x: |
| 3067 |
pass |
| 3068 |
case [x] if x: |
| 3069 |
pass |
| 3070 |
""") |
| 3071 |
|
| 3072 |
def test_name_capture_makes_remaining_patterns_unreachable_4(self): |
| 3073 |
self.assert_syntax_error(""" |
| 3074 |
match ...: |
| 3075 |
case x: |
| 3076 |
pass |
| 3077 |
case _: |
| 3078 |
pass |
| 3079 |
""") |
| 3080 |
|
| 3081 |
def test_patterns_may_only_match_literals_and_attribute_lookups_0(self): |
| 3082 |
self.assert_syntax_error(""" |
| 3083 |
match ...: |
| 3084 |
case f"": |
| 3085 |
pass |
| 3086 |
""") |
| 3087 |
|
| 3088 |
def test_patterns_may_only_match_literals_and_attribute_lookups_1(self): |
| 3089 |
self.assert_syntax_error(""" |
| 3090 |
match ...: |
| 3091 |
case f"{x}": |
| 3092 |
pass |
| 3093 |
""") |
| 3094 |
|
| 3095 |
def test_real_number_required_in_complex_literal_0(self): |
| 3096 |
self.assert_syntax_error(""" |
| 3097 |
match ...: |
| 3098 |
case 0j+0: |
| 3099 |
pass |
| 3100 |
""") |
| 3101 |
|
| 3102 |
def test_real_number_required_in_complex_literal_1(self): |
| 3103 |
self.assert_syntax_error(""" |
| 3104 |
match ...: |
| 3105 |
case 0j+0j: |
| 3106 |
pass |
| 3107 |
""") |
| 3108 |
|
| 3109 |
def test_real_number_required_in_complex_literal_2(self): |
| 3110 |
self.assert_syntax_error(""" |
| 3111 |
match ...: |
| 3112 |
case {0j+0: _}: |
| 3113 |
pass |
| 3114 |
""") |
| 3115 |
|
| 3116 |
def test_real_number_required_in_complex_literal_3(self): |
| 3117 |
self.assert_syntax_error(""" |
| 3118 |
match ...: |
| 3119 |
case {0j+0j: _}: |
| 3120 |
pass |
| 3121 |
""") |
| 3122 |
|
| 3123 |
def test_real_number_multiple_ops(self): |
| 3124 |
self.assert_syntax_error(""" |
| 3125 |
match ...: |
| 3126 |
case 0 + 0j + 0: |
| 3127 |
pass |
| 3128 |
""") |
| 3129 |
|
| 3130 |
def test_real_number_wrong_ops(self): |
| 3131 |
for op in ["*", "/", "@", "**", "%", "//"]: |
| 3132 |
with self.subTest(op=op): |
| 3133 |
self.assert_syntax_error(f""" |
| 3134 |
match ...: |
| 3135 |
case 0 {op} 0j: |
| 3136 |
pass |
| 3137 |
""") |
| 3138 |
self.assert_syntax_error(f""" |
| 3139 |
match ...: |
| 3140 |
case 0j {op} 0: |
| 3141 |
pass |
| 3142 |
""") |
| 3143 |
self.assert_syntax_error(f""" |
| 3144 |
match ...: |
| 3145 |
case -0j {op} 0: |
| 3146 |
pass |
| 3147 |
""") |
| 3148 |
self.assert_syntax_error(f""" |
| 3149 |
match ...: |
| 3150 |
case 0j {op} -0: |
| 3151 |
pass |
| 3152 |
""") |
| 3153 |
|
| 3154 |
def test_wildcard_makes_remaining_patterns_unreachable_0(self): |
| 3155 |
self.assert_syntax_error(""" |
| 3156 |
match ...: |
| 3157 |
case _ | _: |
| 3158 |
pass |
| 3159 |
""") |
| 3160 |
|
| 3161 |
def test_wildcard_makes_remaining_patterns_unreachable_1(self): |
| 3162 |
self.assert_syntax_error(""" |
| 3163 |
match ...: |
| 3164 |
case (_ as x) | [x]: |
| 3165 |
pass |
| 3166 |
""") |
| 3167 |
|
| 3168 |
def test_wildcard_makes_remaining_patterns_unreachable_2(self): |
| 3169 |
self.assert_syntax_error(""" |
| 3170 |
match ...: |
| 3171 |
case _ | _ if condition(): |
| 3172 |
pass |
| 3173 |
""") |
| 3174 |
|
| 3175 |
def test_wildcard_makes_remaining_patterns_unreachable_3(self): |
| 3176 |
self.assert_syntax_error(""" |
| 3177 |
match ...: |
| 3178 |
case _: |
| 3179 |
pass |
| 3180 |
case None: |
| 3181 |
pass |
| 3182 |
""") |
| 3183 |
|
| 3184 |
def test_wildcard_makes_remaining_patterns_unreachable_4(self): |
| 3185 |
self.assert_syntax_error(""" |
| 3186 |
match ...: |
| 3187 |
case (None | _) | _: |
| 3188 |
pass |
| 3189 |
""") |
| 3190 |
|
| 3191 |
def test_wildcard_makes_remaining_patterns_unreachable_5(self): |
| 3192 |
self.assert_syntax_error(""" |
| 3193 |
match ...: |
| 3194 |
case _ | (True | False): |
| 3195 |
pass |
| 3196 |
""") |
| 3197 |
|
| 3198 |
def test_mapping_pattern_duplicate_key(self): |
| 3199 |
self.assert_syntax_error(""" |
| 3200 |
match ...: |
| 3201 |
case {"a": _, "a": _}: |
| 3202 |
pass |
| 3203 |
""") |
| 3204 |
|
| 3205 |
def test_mapping_pattern_duplicate_key_edge_case0(self): |
| 3206 |
self.assert_syntax_error(""" |
| 3207 |
match ...: |
| 3208 |
case {0: _, False: _}: |
| 3209 |
pass |
| 3210 |
""") |
| 3211 |
|
| 3212 |
def test_mapping_pattern_duplicate_key_edge_case1(self): |
| 3213 |
self.assert_syntax_error(""" |
| 3214 |
match ...: |
| 3215 |
case {0: _, 0.0: _}: |
| 3216 |
pass |
| 3217 |
""") |
| 3218 |
|
| 3219 |
def test_mapping_pattern_duplicate_key_edge_case2(self): |
| 3220 |
self.assert_syntax_error(""" |
| 3221 |
match ...: |
| 3222 |
case {0: _, -0: _}: |
| 3223 |
pass |
| 3224 |
""") |
| 3225 |
|
| 3226 |
def test_mapping_pattern_duplicate_key_edge_case3(self): |
| 3227 |
self.assert_syntax_error(""" |
| 3228 |
match ...: |
| 3229 |
case {0: _, 0j: _}: |
| 3230 |
pass |
| 3231 |
""") |
| 3232 |
|
| 3233 |
class TestTypeErrors(unittest.TestCase): |
| 3234 |
|
| 3235 |
def test_accepts_positional_subpatterns_0(self): |
| 3236 |
class Class: |
| 3237 |
__match_args__ = () |
| 3238 |
x = Class() |
| 3239 |
y = z = None |
| 3240 |
with self.assertRaises(TypeError): |
| 3241 |
match x: |
| 3242 |
case Class(y): |
| 3243 |
z = 0 |
| 3244 |
self.assertIs(y, None) |
| 3245 |
self.assertIs(z, None) |
| 3246 |
|
| 3247 |
def test_accepts_positional_subpatterns_1(self): |
| 3248 |
x = range(10) |
| 3249 |
y = None |
| 3250 |
with self.assertRaises(TypeError): |
| 3251 |
match x: |
| 3252 |
case range(10): |
| 3253 |
y = 0 |
| 3254 |
self.assertEqual(x, range(10)) |
| 3255 |
self.assertIs(y, None) |
| 3256 |
|
| 3257 |
def test_got_multiple_subpatterns_for_attribute_0(self): |
| 3258 |
class Class: |
| 3259 |
__match_args__ = ("a", "a") |
| 3260 |
a = None |
| 3261 |
x = Class() |
| 3262 |
w = y = z = None |
| 3263 |
with self.assertRaises(TypeError): |
| 3264 |
match x: |
| 3265 |
case Class(y, z): |
| 3266 |
w = 0 |
| 3267 |
self.assertIs(w, None) |
| 3268 |
self.assertIs(y, None) |
| 3269 |
self.assertIs(z, None) |
| 3270 |
|
| 3271 |
def test_got_multiple_subpatterns_for_attribute_1(self): |
| 3272 |
class Class: |
| 3273 |
__match_args__ = ("a",) |
| 3274 |
a = None |
| 3275 |
x = Class() |
| 3276 |
w = y = z = None |
| 3277 |
with self.assertRaises(TypeError): |
| 3278 |
match x: |
| 3279 |
case Class(y, a=z): |
| 3280 |
w = 0 |
| 3281 |
self.assertIs(w, None) |
| 3282 |
self.assertIs(y, None) |
| 3283 |
self.assertIs(z, None) |
| 3284 |
|
| 3285 |
def test_match_args_elements_must_be_strings(self): |
| 3286 |
class Class: |
| 3287 |
__match_args__ = (None,) |
| 3288 |
x = Class() |
| 3289 |
y = z = None |
| 3290 |
with self.assertRaises(TypeError): |
| 3291 |
match x: |
| 3292 |
case Class(y): |
| 3293 |
z = 0 |
| 3294 |
self.assertIs(y, None) |
| 3295 |
self.assertIs(z, None) |
| 3296 |
|
| 3297 |
def test_match_args_must_be_a_tuple_0(self): |
| 3298 |
class Class: |
| 3299 |
__match_args__ = None |
| 3300 |
x = Class() |
| 3301 |
y = z = None |
| 3302 |
with self.assertRaises(TypeError): |
| 3303 |
match x: |
| 3304 |
case Class(y): |
| 3305 |
z = 0 |
| 3306 |
self.assertIs(y, None) |
| 3307 |
self.assertIs(z, None) |
| 3308 |
|
| 3309 |
def test_match_args_must_be_a_tuple_1(self): |
| 3310 |
class Class: |
| 3311 |
__match_args__ = "XYZ" |
| 3312 |
x = Class() |
| 3313 |
y = z = None |
| 3314 |
with self.assertRaises(TypeError): |
| 3315 |
match x: |
| 3316 |
case Class(y): |
| 3317 |
z = 0 |
| 3318 |
self.assertIs(y, None) |
| 3319 |
self.assertIs(z, None) |
| 3320 |
|
| 3321 |
def test_match_args_must_be_a_tuple_2(self): |
| 3322 |
class Class: |
| 3323 |
__match_args__ = ["spam", "eggs"] |
| 3324 |
spam = 0 |
| 3325 |
eggs = 1 |
| 3326 |
x = Class() |
| 3327 |
w = y = z = None |
| 3328 |
with self.assertRaises(TypeError): |
| 3329 |
match x: |
| 3330 |
case Class(y, z): |
| 3331 |
w = 0 |
| 3332 |
self.assertIs(w, None) |
| 3333 |
self.assertIs(y, None) |
| 3334 |
self.assertIs(z, None) |
| 3335 |
|
| 3336 |
def test_class_pattern_not_type(self): |
| 3337 |
w = None |
| 3338 |
with self.assertRaises(TypeError): |
| 3339 |
match 1: |
| 3340 |
case max(0, 1): |
| 3341 |
w = 0 |
| 3342 |
self.assertIsNone(w) |
| 3343 |
|
| 3344 |
def test_regular_protocol(self): |
| 3345 |
from typing import Protocol |
| 3346 |
class P(Protocol): ... |
| 3347 |
msg = ( |
| 3348 |
'Instance and class checks can only be used ' |
| 3349 |
'with @runtime_checkable protocols' |
| 3350 |
) |
| 3351 |
w = None |
| 3352 |
with self.assertRaisesRegex(TypeError, msg): |
| 3353 |
match 1: |
| 3354 |
case P(): |
| 3355 |
w = 0 |
| 3356 |
self.assertIsNone(w) |
| 3357 |
|
| 3358 |
def test_positional_patterns_with_regular_protocol(self): |
| 3359 |
from typing import Protocol |
| 3360 |
class P(Protocol): |
| 3361 |
x: int # no `__match_args__` |
| 3362 |
y: int |
| 3363 |
class A: |
| 3364 |
x = 1 |
| 3365 |
y = 2 |
| 3366 |
w = None |
| 3367 |
with self.assertRaises(TypeError): |
| 3368 |
match A(): |
| 3369 |
case P(x, y): |
| 3370 |
w = 0 |
| 3371 |
self.assertIsNone(w) |
| 3372 |
|
| 3373 |
|
| 3374 |
class TestValueErrors(unittest.TestCase): |
| 3375 |
|
| 3376 |
def test_mapping_pattern_checks_duplicate_key_1(self): |
| 3377 |
class Keys: |
| 3378 |
KEY = "a" |
| 3379 |
x = {"a": 0, "b": 1} |
| 3380 |
w = y = z = None |
| 3381 |
with self.assertRaises(ValueError): |
| 3382 |
match x: |
| 3383 |
case {Keys.KEY: y, "a": z}: |
| 3384 |
w = 0 |
| 3385 |
self.assertIs(w, None) |
| 3386 |
self.assertIs(y, None) |
| 3387 |
self.assertIs(z, None) |
| 3388 |
|
| 3389 |
class TestSourceLocations(unittest.TestCase): |
| 3390 |
def test_jump_threading(self): |
| 3391 |
# See gh-123048 |
| 3392 |
def f(): |
| 3393 |
x = 0 |
| 3394 |
v = 1 |
| 3395 |
match v: |
| 3396 |
case 1: |
| 3397 |
if x < 0: |
| 3398 |
x = 1 |
| 3399 |
case 2: |
| 3400 |
if x < 0: |
| 3401 |
x = 1 |
| 3402 |
x += 1 |
| 3403 |
|
| 3404 |
for inst in dis.get_instructions(f): |
| 3405 |
if inst.opcode in dis.hasjump: |
| 3406 |
self.assertIsNotNone(inst.positions.lineno, "jump without location") |
| 3407 |
|
| 3408 |
class TestTracing(unittest.TestCase): |
| 3409 |
|
| 3410 |
@staticmethod |
| 3411 |
def _trace(func, *args, **kwargs): |
| 3412 |
actual_linenos = [] |
| 3413 |
|
| 3414 |
def trace(frame, event, arg): |
| 3415 |
if event == "line" and frame.f_code.co_name == func.__name__: |
| 3416 |
assert arg is None |
| 3417 |
relative_lineno = frame.f_lineno - func.__code__.co_firstlineno |
| 3418 |
actual_linenos.append(relative_lineno) |
| 3419 |
return trace |
| 3420 |
|
| 3421 |
old_trace = sys.gettrace() |
| 3422 |
sys.settrace(trace) |
| 3423 |
try: |
| 3424 |
func(*args, **kwargs) |
| 3425 |
finally: |
| 3426 |
sys.settrace(old_trace) |
| 3427 |
return actual_linenos |
| 3428 |
|
| 3429 |
def test_default_wildcard(self): |
| 3430 |
def f(command): # 0 |
| 3431 |
match command.split(): # 1 |
| 3432 |
case ["go", direction] if direction in "nesw": # 2 |
| 3433 |
return f"go {direction}" # 3 |
| 3434 |
case ["go", _]: # 4 |
| 3435 |
return "no go" # 5 |
| 3436 |
case _: # 6 |
| 3437 |
return "default" # 7 |
| 3438 |
|
| 3439 |
self.assertListEqual(self._trace(f, "go n"), [1, 2, 3]) |
| 3440 |
self.assertListEqual(self._trace(f, "go x"), [1, 2, 4, 5]) |
| 3441 |
self.assertListEqual(self._trace(f, "spam"), [1, 2, 4, 6, 7]) |
| 3442 |
|
| 3443 |
def test_default_capture(self): |
| 3444 |
def f(command): # 0 |
| 3445 |
match command.split(): # 1 |
| 3446 |
case ["go", direction] if direction in "nesw": # 2 |
| 3447 |
return f"go {direction}" # 3 |
| 3448 |
case ["go", _]: # 4 |
| 3449 |
return "no go" # 5 |
| 3450 |
case x: # 6 |
| 3451 |
return x # 7 |
| 3452 |
|
| 3453 |
self.assertListEqual(self._trace(f, "go n"), [1, 2, 3]) |
| 3454 |
self.assertListEqual(self._trace(f, "go x"), [1, 2, 4, 5]) |
| 3455 |
self.assertListEqual(self._trace(f, "spam"), [1, 2, 4, 6, 7]) |
| 3456 |
|
| 3457 |
def test_no_default(self): |
| 3458 |
def f(command): # 0 |
| 3459 |
match command.split(): # 1 |
| 3460 |
case ["go", direction] if direction in "nesw": # 2 |
| 3461 |
return f"go {direction}" # 3 |
| 3462 |
case ["go", _]: # 4 |
| 3463 |
return "no go" # 5 |
| 3464 |
|
| 3465 |
self.assertListEqual(self._trace(f, "go n"), [1, 2, 3]) |
| 3466 |
self.assertListEqual(self._trace(f, "go x"), [1, 2, 4, 5]) |
| 3467 |
self.assertListEqual(self._trace(f, "spam"), [1, 2, 4]) |
| 3468 |
|
| 3469 |
def test_only_default_wildcard(self): |
| 3470 |
def f(command): # 0 |
| 3471 |
match command.split(): # 1 |
| 3472 |
case _: # 2 |
| 3473 |
return "default" # 3 |
| 3474 |
|
| 3475 |
self.assertListEqual(self._trace(f, "go n"), [1, 2, 3]) |
| 3476 |
self.assertListEqual(self._trace(f, "go x"), [1, 2, 3]) |
| 3477 |
self.assertListEqual(self._trace(f, "spam"), [1, 2, 3]) |
| 3478 |
|
| 3479 |
def test_only_default_capture(self): |
| 3480 |
def f(command): # 0 |
| 3481 |
match command.split(): # 1 |
| 3482 |
case x: # 2 |
| 3483 |
return x # 3 |
| 3484 |
|
| 3485 |
self.assertListEqual(self._trace(f, "go n"), [1, 2, 3]) |
| 3486 |
self.assertListEqual(self._trace(f, "go x"), [1, 2, 3]) |
| 3487 |
self.assertListEqual(self._trace(f, "spam"), [1, 2, 3]) |
| 3488 |
|
| 3489 |
def test_unreachable_code(self): |
| 3490 |
def f(command): # 0 |
| 3491 |
match command: # 1 |
| 3492 |
case 1: # 2 |
| 3493 |
if False: # 3 |
| 3494 |
return 1 # 4 |
| 3495 |
case _: # 5 |
| 3496 |
if False: # 6 |
| 3497 |
return 0 # 7 |
| 3498 |
|
| 3499 |
self.assertListEqual(self._trace(f, 1), [1, 2, 3]) |
| 3500 |
self.assertListEqual(self._trace(f, 0), [1, 2, 5, 6]) |
| 3501 |
|
| 3502 |
@support.skip_wasi_stack_overflow() |
| 3503 |
def test_parser_deeply_nested_patterns(self): |
| 3504 |
# Deeply nested patterns can cause exponential backtracking when parsing. |
| 3505 |
# See gh-93671 for more information. |
| 3506 |
|
| 3507 |
levels = 100 |
| 3508 |
|
| 3509 |
patterns = [ |
| 3510 |
"A" + "(" * levels + ")" * levels, |
| 3511 |
"{1:" * levels + "1" + "}" * levels, |
| 3512 |
"[" * levels + "1" + "]" * levels, |
| 3513 |
] |
| 3514 |
|
| 3515 |
for pattern in patterns: |
| 3516 |
with self.subTest(pattern): |
| 3517 |
code = inspect.cleandoc(""" |
| 3518 |
match None: |
| 3519 |
case {}: |
| 3520 |
pass |
| 3521 |
""".format(pattern)) |
| 3522 |
compile(code, "<string>", "exec") |
| 3523 |
|
| 3524 |
|
| 3525 |
if __name__ == "__main__": |
| 3526 |
""" |
| 3527 |
# From inside environment using this Python, with pyperf installed: |
| 3528 |
sudo $(which pyperf) system tune && \ |
| 3529 |
$(which python) -m test.test_patma --rigorous; \ |
| 3530 |
sudo $(which pyperf) system reset |
| 3531 |
""" |
| 3532 |
import pyperf |
| 3533 |
|
| 3534 |
|
| 3535 |
class PerfPatma(TestPatma): |
| 3536 |
|
| 3537 |
def assertEqual(*_, **__): |
| 3538 |
pass |
| 3539 |
|
| 3540 |
def assertIs(*_, **__): |
| 3541 |
pass |
| 3542 |
|
| 3543 |
def assertRaises(*_, **__): |
| 3544 |
assert False, "this test should be a method of a different class!" |
| 3545 |
|
| 3546 |
def run_perf(self, count): |
| 3547 |
tests = [] |
| 3548 |
for attr in vars(TestPatma): |
| 3549 |
if attr.startswith("test_"): |
| 3550 |
tests.append(getattr(self, attr)) |
| 3551 |
tests *= count |
| 3552 |
start = pyperf.perf_counter() |
| 3553 |
for test in tests: |
| 3554 |
test() |
| 3555 |
return pyperf.perf_counter() - start |
| 3556 |
|
| 3557 |
|
| 3558 |
runner = pyperf.Runner() |
| 3559 |
runner.bench_time_func("patma", PerfPatma().run_perf) |