|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""The macros pass through Python 3.15's new comprehension unpacking (PEP 798). |
| 3 | +
|
| 4 | +These tests require Python 3.15+ because the unpacking syntax won't parse on |
| 5 | +earlier versions. |
| 6 | +
|
| 7 | +`lazify`, `autocurry` and `tco` have no comprehension-specific handling, so the |
| 8 | +new forms reach their generic paths. Two shapes are new there: a `Starred` in the |
| 9 | +element position of a list/set/generator comprehension, and a `DictComp` whose |
| 10 | +`value` is `None` because the mapping expression sits in `key` instead. |
| 11 | +
|
| 12 | +The hazard worth guarding against is specific to `lazify`: if it wrapped the |
| 13 | +starred value in a promise, `*promise` would fail at unpacking. It does not, and |
| 14 | +these tests are here to keep it that way — the existing suite cannot catch a |
| 15 | +regression here, since nothing else in it contains this syntax. |
| 16 | +
|
| 17 | +TODO: Merge into the per-macro test modules when the floor bumps to Python 3.15+. |
| 18 | +""" |
| 19 | + |
| 20 | +from ...syntax import macros, test, the # noqa: F401 |
| 21 | +from ...test.fixtures import session, testset |
| 22 | + |
| 23 | +from ...syntax import macros, lazify, autocurry, tco # noqa: F401, F811 |
| 24 | + |
| 25 | + |
| 26 | +def _items(k): |
| 27 | + return [k, k] |
| 28 | + |
| 29 | + |
| 30 | +def _mapping(k): |
| 31 | + return {k: k} |
| 32 | + |
| 33 | + |
| 34 | +with lazify: |
| 35 | + def lazy_starred_list(ks): |
| 36 | + return [*_items(k) for k in ks] |
| 37 | + |
| 38 | + def lazy_starred_set(ks): |
| 39 | + return {*_items(k) for k in ks} |
| 40 | + |
| 41 | + def lazy_starred_genexpr(ks): |
| 42 | + return list((*_items(k) for k in ks)) |
| 43 | + |
| 44 | + def lazy_dict_unpacking(ks): |
| 45 | + return {**_mapping(k) for k in ks} |
| 46 | + |
| 47 | + def lazy_ordinary_dictcomp(ks): |
| 48 | + return {k: _items(k) for k in ks} |
| 49 | + |
| 50 | + |
| 51 | +with autocurry: |
| 52 | + def curried_starred_list(ks): |
| 53 | + return [*_items(k) for k in ks] |
| 54 | + |
| 55 | + def curried_dict_unpacking(ks): |
| 56 | + return {**_mapping(k) for k in ks} |
| 57 | + |
| 58 | + |
| 59 | +with tco: |
| 60 | + def tco_starred_list(ks): |
| 61 | + return [*_items(k) for k in ks] |
| 62 | + |
| 63 | + def tco_dict_unpacking(ks): |
| 64 | + return {**_mapping(k) for k in ks} |
| 65 | + |
| 66 | + |
| 67 | +def runtests(): |
| 68 | + with testset("lazify: starred comprehension elements"): |
| 69 | + test[lazy_starred_list([1, 2]) == [1, 1, 2, 2]] |
| 70 | + test[lazy_starred_set([1, 2]) == {1, 2}] |
| 71 | + test[lazy_starred_genexpr([1, 2]) == [1, 1, 2, 2]] |
| 72 | + |
| 73 | + with testset("lazify: dict-unpacking comprehension"): |
| 74 | + test[lazy_dict_unpacking([1, 2]) == {1: 1, 2: 2}] |
| 75 | + # Control: the `k: v` form goes through the `value` field the unpacking |
| 76 | + # form leaves empty, so this catches a fix that skipped that field. |
| 77 | + test[lazy_ordinary_dictcomp([1, 2]) == {1: [1, 1], 2: [2, 2]}] |
| 78 | + |
| 79 | + with testset("autocurry: new comprehension forms"): |
| 80 | + test[curried_starred_list([1, 2]) == [1, 1, 2, 2]] |
| 81 | + test[curried_dict_unpacking([1, 2]) == {1: 1, 2: 2}] |
| 82 | + |
| 83 | + with testset("tco: new comprehension forms"): |
| 84 | + test[tco_starred_list([1, 2]) == [1, 1, 2, 2]] |
| 85 | + test[tco_dict_unpacking([1, 2]) == {1: 1, 2: 2}] |
| 86 | + |
| 87 | + |
| 88 | +if __name__ == '__main__': # pragma: no cover |
| 89 | + with session(__file__): |
| 90 | + runtests() |
0 commit comments