| 1 |
import pytest |
| 2 |
|
| 3 |
def test_match_sequence_string_bug(): |
| 4 |
x = "x" |
| 5 |
match x: |
| 6 |
case ['x']: |
| 7 |
y = 2 |
| 8 |
case 'x': |
| 9 |
y = 5 |
| 10 |
assert y == 5 |
| 11 |
|
| 12 |
def test_sequence_missing_not_used(): |
| 13 |
class defaultdict(dict): |
| 14 |
def __missing__(self, key): |
| 15 |
return 0 |
| 16 |
x = defaultdict() |
| 17 |
x[1] = 1 |
| 18 |
match x: |
| 19 |
case {0: 0}: |
| 20 |
y = 0 |
| 21 |
case {**z}: |
| 22 |
y = 1 |
| 23 |
assert x == {1: 1} |
| 24 |
assert y == 1 |
| 25 |
assert z == {1: 1} |
| 26 |
|
| 27 |
def test_error_name_bindings_duplicate(): |
| 28 |
with pytest.raises(SyntaxError) as info: |
| 29 |
exec(""" |
| 30 |
match x: |
| 31 |
case [a, a]: |
| 32 |
pass |
| 33 |
""") |
| 34 |
|
| 35 |
def test_error_name_bindings_duplicate_or(): |
| 36 |
with pytest.raises(SyntaxError) as info: |
| 37 |
exec(""" |
| 38 |
def f(x): |
| 39 |
match x: |
| 40 |
case [1 as a, |
| 41 |
((2 as a) | (3 as a))]: return 17 |
| 42 |
""") |
| 43 |
assert info.value.msg == "multiple assignments to name 'a' in pattern, previous one was on line 4" |
| 44 |
assert info.value.lineno == 5 |
| 45 |
|
| 46 |
|
| 47 |
def test_error_name_bindings_or(): |
| 48 |
with pytest.raises(SyntaxError) as info: |
| 49 |
exec(""" |
| 50 |
match x: |
| 51 |
case "a" | a: |
| 52 |
pass |
| 53 |
""") |
| 54 |
|
| 55 |
def test_error_allow_always_passing_or(): |
| 56 |
with pytest.raises(SyntaxError) as info: |
| 57 |
exec(""" |
| 58 |
match x: |
| 59 |
case a | "a": |
| 60 |
pass |
| 61 |
""") |
| 62 |
assert info.value.msg == "name capture 'a' makes remaining patterns unreachable" |
| 63 |
|
| 64 |
def test_error_forbidden_name(): |
| 65 |
with pytest.raises(SyntaxError) as info: |
| 66 |
exec(""" |
| 67 |
match x: |
| 68 |
case 1 as True: |
| 69 |
pass |
| 70 |
""") |
| 71 |
|
| 72 |
def test_match_list(): |
| 73 |
def match_list(x): |
| 74 |
match x: |
| 75 |
case [True]: return "[True]" |
| 76 |
case [1,2,3]: return "list[1,2,3]" |
| 77 |
case [1]: return "list[1]" |
| 78 |
case []: return "emptylist" |
| 79 |
case [_]: return "list" |
| 80 |
assert match_list(['']) == "list" |
| 81 |
assert match_list([1]) == "list[1]" |
| 82 |
assert match_list([1,2,3]) == "list[1,2,3]" |
| 83 |
assert match_list([1,2,4]) is None |
| 84 |
assert match_list([2,3,4]) is None |
| 85 |
assert match_list([1, 2]) is None |
| 86 |
assert match_list([]) == "emptylist" |
| 87 |
|
| 88 |
def test_match_with_if_bug(): |
| 89 |
def match_truthy(x): |
| 90 |
match x: |
| 91 |
case a if a: return a |
| 92 |
assert match_truthy(1) == 1 |
| 93 |
assert match_truthy(True) is True |
| 94 |
assert match_truthy([]) is None |
| 95 |
assert match_truthy('') is None |
| 96 |
|
| 97 |
|
| 98 |
def test_or(): |
| 99 |
x = 2 |
| 100 |
match x: |
| 101 |
case 1 | 2: |
| 102 |
a = 2 |
| 103 |
case _: |
| 104 |
a = 3 |
| 105 |
assert a == 2 |
| 106 |
|
| 107 |
def test_dont_use_is(): |
| 108 |
match [1.0]: |
| 109 |
case [1]: pass |
| 110 |
case _: assert False |
| 111 |
|
| 112 |
def test_only_bind_at_end(): |
| 113 |
a = 5 |
| 114 |
match [1, 2, 3]: |
| 115 |
case [a, 1, b]: |
| 116 |
pass |
| 117 |
assert a == 5 |
| 118 |
|
| 119 |
def test_or_reorder(): |
| 120 |
def or_orders(x): |
| 121 |
match x: |
| 122 |
case [a, b, 1] | [b, a, 2]: |
| 123 |
return a, b |
| 124 |
return 12 |
| 125 |
assert or_orders([1, 2, 1]) == (1, 2) |
| 126 |
assert or_orders([1, 2, 2]) == (2, 1) |
| 127 |
assert or_orders([1, 3, 4]) == 12 |
| 128 |
|
| 129 |
def test_bug_repeated_names_not_reset_between_cases(): |
| 130 |
def as_bug(x): |
| 131 |
match x: |
| 132 |
case 1 as y: return y |
| 133 |
case 2 as y: return y |
| 134 |
assert as_bug(1) == 1 |
| 135 |
assert as_bug(2) == 2 |
| 136 |
assert as_bug(3) is None |
| 137 |
|
| 138 |
def test_bug_match_sequence_star(): |
| 139 |
def sequence_star_bug(x): |
| 140 |
match x: |
| 141 |
case [1, a, *rest, x, 3]: |
| 142 |
return a, rest, x |
| 143 |
assert sequence_star_bug([1, 2, 3, 4, 5, 6, 3]) == (2, [3, 4, 5], 6) |
| 144 |
|
| 145 |
assert "rest" not in globals() |
| 146 |
|
| 147 |
def test_bug_match_class_builtin(): |
| 148 |
def match_class_bool(x): |
| 149 |
match x: |
| 150 |
case bool(b) if b: return "True" |
| 151 |
case bool(): return "False" |
| 152 |
assert match_class_bool(True) == "True" |
| 153 |
assert match_class_bool(False) == "False" |
| 154 |
|
| 155 |
def test_error_repeated_class_keyword(): |
| 156 |
with pytest.raises(SyntaxError) as info: |
| 157 |
exec(""" |
| 158 |
match x: |
| 159 |
case A(a=_, a=_): |
| 160 |
pass |
| 161 |
""") |
| 162 |
|
| 163 |
def test_error_duplicate_key(): |
| 164 |
with pytest.raises(SyntaxError) as info: |
| 165 |
exec(""" |
| 166 |
match x: |
| 167 |
case {"a": 1, "a": 2}: |
| 168 |
pass |
| 169 |
""") |
| 170 |
assert info.value.msg == "mapping pattern checks duplicate key ('a')" |
| 171 |
assert info.value.lineno == 3 |
| 172 |
|
| 173 |
def test_error_key_wrong_kind(): |
| 174 |
with pytest.raises(SyntaxError) as info: |
| 175 |
exec(""" |
| 176 |
match x: |
| 177 |
case {f"{a}": 1}: |
| 178 |
pass |
| 179 |
""") |
| 180 |
assert info.value.msg == "mapping pattern keys may only match literals and attribute lookups" |
| 181 |
assert info.value.lineno == 3 |
| 182 |
|
| 183 |
def test_error_key_wrong_kind(): |
| 184 |
with pytest.raises(SyntaxError) as info: |
| 185 |
exec(""" |
| 186 |
match x: |
| 187 |
case [a, *b, c, *d]: |
| 188 |
pass |
| 189 |
""") |
| 190 |
assert info.value.msg == "multiple starred names in sequence pattern" |
| 191 |
assert info.value.lineno == 3 |
| 192 |
|
| 193 |
def test_match_args_tuple(): |
| 194 |
class C: |
| 195 |
__match_args__ = ["a", "b"] |
| 196 |
a = 0 |
| 197 |
b = 1 |
| 198 |
x = C() |
| 199 |
w = y = z = None |
| 200 |
with pytest.raises(TypeError): |
| 201 |
match x: |
| 202 |
case C(y, z): |
| 203 |
w = 12 |
| 204 |
assert w is y is z is None |
| 205 |
|
| 206 |
def test_match_keys_duplicate_runtime(): |
| 207 |
class K: |
| 208 |
k = "a" |
| 209 |
w = y = z = None |
| 210 |
with pytest.raises(ValueError): |
| 211 |
match {"a": 1, "b": 2}: |
| 212 |
case {K.k: y, "a": z}: w = 12 |
| 213 |
assert w is y is z is None |
| 214 |
|
| 215 |
def test_optimize_unpack_sequence_star_no_capture(): |
| 216 |
class Sequence: |
| 217 |
def __getitem__(self, index): |
| 218 |
return index |
| 219 |
def __len__(self): |
| 220 |
return 42 |
| 221 |
def __iter__(self): |
| 222 |
return self |
| 223 |
def __next__(self): |
| 224 |
return 1 |
| 225 |
a = 0 |
| 226 |
b = 0 |
| 227 |
match Sequence(): |
| 228 |
case [0, *_, b, 41]: |
| 229 |
a = 1 |
| 230 |
assert a == 1 |
| 231 |
assert b == 40 |
| 232 |
|
| 233 |
def test_unpack_sequence_bug(): |
| 234 |
def f(w): |
| 235 |
match w: |
| 236 |
case (p, q) as x: |
| 237 |
locals() |
| 238 |
return p, q, x |
| 239 |
assert f((1, 2)) == (1, 2, (1, 2)) |
| 240 |
assert "p" not in globals() |
| 241 |
assert "q" not in globals() |
| 242 |
|
| 243 |
def test_or_reordering_bug(): |
| 244 |
def annoying_or(x): |
| 245 |
match x: |
| 246 |
case ((a, b, c, d, e, 7) | |
| 247 |
(a, b, d, e, c, 8)): |
| 248 |
pass |
| 249 |
out = locals() |
| 250 |
del out["x"] |
| 251 |
return out |
| 252 |
|
| 253 |
res = annoying_or(range(3, 9)) |
| 254 |
exp = dict(a=3, b=4, d=5, e=6, c=7) |
| 255 |
assert res == exp |
| 256 |
|
| 257 |
def test_bytearray_does_not_match_sequence(): |
| 258 |
def sequence_match(x): |
| 259 |
match x: |
| 260 |
case [120]: |
| 261 |
return 1 |
| 262 |
case 120: |
| 263 |
return 2 |
| 264 |
return 3 |
| 265 |
assert sequence_match(bytearray(b"x")) == 3 |
| 266 |
|
| 267 |
|
| 268 |
def test_error_fstring(): |
| 269 |
with pytest.raises(SyntaxError) as info: |
| 270 |
exec(""" |
| 271 |
def fstringbug(): |
| 272 |
match x: |
| 273 |
case f"{x}": |
| 274 |
pass |
| 275 |
""") |
| 276 |
assert info.value.msg == "patterns may only match literals and attribute lookups" |
| 277 |
|
| 278 |
|
| 279 |
def test_collections_abcs(): |
| 280 |
import collections.abc |
| 281 |
class Seq(collections.abc.Sequence): |
| 282 |
__getitem__ = None |
| 283 |
|
| 284 |
def __init__(self, l): |
| 285 |
self.l = l |
| 286 |
|
| 287 |
def __len__(self): |
| 288 |
return self.l |
| 289 |
match Seq(0): |
| 290 |
case []: |
| 291 |
y = 0 |
| 292 |
assert y == 0 |
| 293 |
|
| 294 |
match Seq(34): |
| 295 |
case [*_]: |
| 296 |
y = 10 |
| 297 |
assert y == 10 |
| 298 |
|
| 299 |
def test_sequence_doesnt_need_length(): |
| 300 |
class A: |
| 301 |
def __getitem__(self, x): |
| 302 |
return 1 |
| 303 |
match A(): |
| 304 |
case [*_]: |
| 305 |
y = 10 |
| 306 |
assert y == 10 |
| 307 |
|
| 308 |
def test_collections_abc_mapping(): |
| 309 |
import collections.abc |
| 310 |
class A: |
| 311 |
pass |
| 312 |
collections.abc.Mapping.register(A) |
| 313 |
match A(): |
| 314 |
case [*_]: assert 0, "unreachable" |
| 315 |
case {}: x = 11121 |
| 316 |
assert x == 11121 |
| 317 |
|
| 318 |
class B(A): |
| 319 |
pass |
| 320 |
|
| 321 |
match B(): |
| 322 |
case [*_]: assert 0, "unreachable" |
| 323 |
case {}: x = 111213434 |
| 324 |
assert x == 111213434 |
| 325 |
|
| 326 |
def test_abstract_isinstance_check(): |
| 327 |
class ABC(type): |
| 328 |
|
| 329 |
def __instancecheck__(cls, inst): |
| 330 |
"""Implement isinstance(inst, cls).""" |
| 331 |
return any(cls.__subclasscheck__(c) |
| 332 |
for c in set([type(inst), inst.__class__])) |
| 333 |
|
| 334 |
def __subclasscheck__(cls, sub): |
| 335 |
"""Implement issubclass(sub, cls).""" |
| 336 |
candidates = cls.__dict__.get("__subclass__", set()) | set([cls]) |
| 337 |
return any(c in candidates for c in sub.mro()) |
| 338 |
|
| 339 |
class Integer(metaclass=ABC): |
| 340 |
__subclass__ = set([int]) |
| 341 |
|
| 342 |
assert isinstance(12, Integer) |
| 343 |
|
| 344 |
match 12: |
| 345 |
case Integer(): pass |
| 346 |
case _: assert 0 |
| 347 |
|
| 348 |
|
| 349 |
def test_dict_pattern_none_value_bug(): |
| 350 |
d = {"a": None} |
| 351 |
res = 0 |
| 352 |
match d: |
| 353 |
case {"a": None}: |
| 354 |
res = 1 |
| 355 |
case _: |
| 356 |
res = 2 |
| 357 |
assert res == 1 |
| 358 |
|
| 359 |
|
| 360 |
def test_match_mapping_codegen_uses_unpack_sequence(): |
| 361 |
"""MATCH_KEYS result should be unpacked with UNPACK_SEQUENCE, not |
| 362 |
with DUP_TOP+BINARY_SUBSCR+rotation. CPython emits: |
| 363 |
MATCH_KEYS / COPY 1 / POP_JUMP_FORWARD_IF_NONE / UNPACK_SEQUENCE n |
| 364 |
PyPy must match that pattern to avoid emitting SWAP instructions.""" |
| 365 |
import dis |
| 366 |
|
| 367 |
def f(x): |
| 368 |
match x: |
| 369 |
case {'a': a, 'b': b, 'c': _}: |
| 370 |
return a, b |
| 371 |
|
| 372 |
instrs = list(dis.get_instructions(f)) |
| 373 |
opnames = [i.opname for i in instrs] |
| 374 |
|
| 375 |
assert 'UNPACK_SEQUENCE' in opnames, \ |
| 376 |
"expected UNPACK_SEQUENCE for mapping pattern, got: %s" % opnames |
| 377 |
assert 'BINARY_SUBSCR' not in opnames, \ |
| 378 |
"BINARY_SUBSCR should not appear in mapping pattern" |
| 379 |
assert 'SWAP' not in opnames, \ |
| 380 |
"SWAP should not appear in mapping pattern" |
| 381 |
assert 'ROT_FOUR' not in opnames, \ |
| 382 |
"ROT_FOUR should not appear in mapping pattern" |
| 383 |
|
| 384 |
|
| 385 |
def _check_match_class_no_swap(pattern): |
| 386 |
"""Check that MatchClass codegen for pattern emits no SWAP or BINARY_SUBSCR. |
| 387 |
CPython emits MATCH_CLASS / POP_JUMP_IF_FALSE / UNPACK_SEQUENCE / stores. |
| 388 |
PyPy currently uses DUP_TOP+BINARY_SUBSCR+SWAP rotation which the peephole |
| 389 |
optimizer cannot always eliminate.""" |
| 390 |
import dis |
| 391 |
|
| 392 |
src = ( |
| 393 |
"def f(x):\n" |
| 394 |
" match x:\n" |
| 395 |
" case %s:\n" |
| 396 |
" pass\n" |
| 397 |
) % pattern |
| 398 |
ns = {} |
| 399 |
exec(compile(src, "<test>", "exec"), ns) |
| 400 |
f = ns["f"] |
| 401 |
instrs = list(dis.get_instructions(f)) |
| 402 |
opnames = [i.opname for i in instrs] |
| 403 |
print("pattern %s opcodes: %s" % (pattern, opnames)) |
| 404 |
assert 'SWAP' not in opnames, \ |
| 405 |
"SWAP should not appear in class pattern %s, got: %s" % (pattern, opnames) |
| 406 |
assert 'BINARY_SUBSCR' not in opnames, \ |
| 407 |
"BINARY_SUBSCR should not appear in class pattern %s, got: %s" % (pattern, opnames) |
| 408 |
|
| 409 |
|
| 410 |
def test_match_class_no_swap_wildcard_b_wildcard(): |
| 411 |
_check_match_class_no_swap("C(_, b, _)") |
| 412 |
|
| 413 |
def test_match_class_no_swap_a_wildcard_wildcard(): |
| 414 |
_check_match_class_no_swap("C(a, _, _)") |
| 415 |
|
| 416 |
def test_match_class_no_swap_a_b_wildcard(): |
| 417 |
_check_match_class_no_swap("C(a, b, _)") |
| 418 |
|
| 419 |
def test_match_class_no_swap_a_wildcard_c(): |
| 420 |
_check_match_class_no_swap("C(a, _, c)") |
| 421 |
|
| 422 |
def test_match_class_no_swap_wildcard_b_c(): |
| 423 |
_check_match_class_no_swap("C(_, b, c)") |
| 424 |
|
| 425 |
def test_match_class_no_swap_a_b_c(): |
| 426 |
_check_match_class_no_swap("C(a, b, c)") |