|
14 | 14 | get_lexical_variables, |
15 | 15 | scoped_transform) |
16 | 16 |
|
17 | | -# TODO: Add tests for `match`/`case` once we bump minimum language version to Python 3.10. |
18 | | -# TODO: Add tests for `try`/`except*` once we bump minimum language version to Python 3.11. |
19 | | - |
20 | 17 | def runtests(): |
21 | 18 | # test data |
22 | 19 | with q as getnames_load: |
@@ -272,6 +269,82 @@ def f(): # noqa: F811 |
272 | 269 | n["_apply_test_here_"] |
273 | 270 | scoped_transform(scoped_localvar3, callback=make_checker(["f"])) # x already deleted |
274 | 271 |
|
| 272 | + # Python 3.10+: `match`/`case` |
| 273 | + with testset("match/case: get_names_in_store_context"): |
| 274 | + # Simple capture |
| 275 | + with q as matchcase_simple: |
| 276 | + match x: # noqa: F821, it's only quoted. |
| 277 | + case y: # noqa: F841, it's only quoted. |
| 278 | + pass |
| 279 | + test[get_names_in_store_context(matchcase_simple) == ["y"]] |
| 280 | + |
| 281 | + # Wildcard `_` — does NOT capture |
| 282 | + with q as matchcase_wildcard: |
| 283 | + match x: # noqa: F821, it's only quoted. |
| 284 | + case _: |
| 285 | + pass |
| 286 | + test[get_names_in_store_context(matchcase_wildcard) == []] |
| 287 | + |
| 288 | + # Sequence pattern with star capture |
| 289 | + with q as matchcase_sequence: |
| 290 | + match x: # noqa: F821, it's only quoted. |
| 291 | + case [a, b, *rest]: # noqa: F841, it's only quoted. |
| 292 | + pass |
| 293 | + test[get_names_in_store_context(matchcase_sequence) == ["a", "b", "rest"]] |
| 294 | + |
| 295 | + # Class pattern — captures `x` and `y`, but NOT the class reference `Point` |
| 296 | + with q as matchcase_class: |
| 297 | + match x: # noqa: F821, it's only quoted. |
| 298 | + case Point(x, y): # noqa: F821, F841, it's only quoted. |
| 299 | + pass |
| 300 | + names = get_names_in_store_context(matchcase_class) |
| 301 | + test["x" in names] |
| 302 | + test["y" in names] |
| 303 | + test["Point" not in names] # class reference, not a capture |
| 304 | + |
| 305 | + # Class pattern with keyword captures |
| 306 | + with q as matchcase_class_kw: |
| 307 | + match x: # noqa: F821, it's only quoted. |
| 308 | + case Point(x=px, y=py): # noqa: F821, F841, it's only quoted. |
| 309 | + pass |
| 310 | + names = get_names_in_store_context(matchcase_class_kw) |
| 311 | + test["px" in names] |
| 312 | + test["py" in names] |
| 313 | + test["Point" not in names] |
| 314 | + |
| 315 | + # Mapping pattern with `**rest` |
| 316 | + with q as matchcase_mapping: |
| 317 | + match x: # noqa: F821, it's only quoted. |
| 318 | + case {"key": value, **rest}: # noqa: F841, it's only quoted. |
| 319 | + pass |
| 320 | + names = get_names_in_store_context(matchcase_mapping) |
| 321 | + test["value" in names] |
| 322 | + test["rest" in names] |
| 323 | + |
| 324 | + # Nested: mapping containing a class pattern |
| 325 | + with q as matchcase_nested: |
| 326 | + match x: # noqa: F821, it's only quoted. |
| 327 | + case {"key": Point(px, py)}: # noqa: F821, F841, it's only quoted. |
| 328 | + pass |
| 329 | + names = get_names_in_store_context(matchcase_nested) |
| 330 | + test["px" in names] |
| 331 | + test["py" in names] |
| 332 | + test["Point" not in names] # class reference, not a capture |
| 333 | + |
| 334 | + # OR pattern |
| 335 | + with q as matchcase_or: |
| 336 | + match x: # noqa: F821, it's only quoted. |
| 337 | + case 1 | 2 | 3: |
| 338 | + pass |
| 339 | + test[get_names_in_store_context(matchcase_or) == []] |
| 340 | + |
| 341 | + # `as` pattern with guard |
| 342 | + with q as matchcase_as: |
| 343 | + match x: # noqa: F821, it's only quoted. |
| 344 | + case (1 | 2) as num: # noqa: F841, it's only quoted. |
| 345 | + pass |
| 346 | + test[get_names_in_store_context(matchcase_as) == ["num"]] |
| 347 | + |
275 | 348 | if __name__ == '__main__': # pragma: no cover |
276 | 349 | with session(__file__): |
277 | 350 | runtests() |
0 commit comments