From 945c6f2fd654696e96344b3f9a3d6616cb9017b8 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Mon, 27 Apr 2026 18:23:48 -0700 Subject: [PATCH] Add test for new walrus semantics allowed in Python 3.12 --- test-data/unit/check-python312.test | 38 ++++++++++++++++++++++++++ test-data/unit/fixtures/primitives.pyi | 2 ++ 2 files changed, 40 insertions(+) diff --git a/test-data/unit/check-python312.test b/test-data/unit/check-python312.test index 2e1f4f863464..b51186796b8f 100644 --- a/test-data/unit/check-python312.test +++ b/test-data/unit/check-python312.test @@ -1818,6 +1818,44 @@ type YNested = (1 + (yield from [])) # E: Yield expression cannot be used within type ZNested = (1 + (a := 1)) # E: Named expression cannot be used within a type alias type KNested = (1 + (await 1)) # E: Await expression cannot be used within a type alias +[case testWalrusRebindsNamesLoadedInComprehensionTargets] +# Newly legal in https://github.com/python/cpython/pull/100581 +from typing import Any + +def f(c: Any, d: Any, e: Any, f: Any, g: Any, h: Any, j: Any) -> None: + ((c := 1) for a, (*b, c[d+e::f(g)], h.i) in j) + [(c := 1) for a, (*b, c[d+e::f(g)], h.i) in j] + {(c := 1) for a, (*b, c[d+e::f(g)], h.i) in j} + + ((d := 1) for a, (*b, c[d+e::f(g)], h.i) in j) + [(d := 1) for a, (*b, c[d+e::f(g)], h.i) in j] + {(d := 1) for a, (*b, c[d+e::f(g)], h.i) in j} + + ((e := 1) for a, (*b, c[d+e::f(g)], h.i) in j) + [(e := 1) for a, (*b, c[d+e::f(g)], h.i) in j] + {(e := 1) for a, (*b, c[d+e::f(g)], h.i) in j} + + ((f := 1) for a, (*b, c[d+e::f(g)], h.i) in j) + [(f := 1) for a, (*b, c[d+e::f(g)], h.i) in j] + {(f := 1) for a, (*b, c[d+e::f(g)], h.i) in j} + + ((g := 1) for a, (*b, c[d+e::f(g)], h.i) in j) + [(g := 1) for a, (*b, c[d+e::f(g)], h.i) in j] + {(g := 1) for a, (*b, c[d+e::f(g)], h.i) in j} + + ((h := 1) for a, (*b, c[d+e::f(g)], h.i) in j) + [(h := 1) for a, (*b, c[d+e::f(g)], h.i) in j] + {(h := 1) for a, (*b, c[d+e::f(g)], h.i) in j} + + ((i := 1) for a, (*b, c[d+e::f(g)], h.i) in j) + [(i := 1) for a, (*b, c[d+e::f(g)], h.i) in j] + {(i := 1) for a, (*b, c[d+e::f(g)], h.i) in j} + + ((j := 1) for a, (*b, c[d+e::f(g)], h.i) in j) + [(j := 1) for a, (*b, c[d+e::f(g)], h.i) in j] + {(j := 1) for a, (*b, c[d+e::f(g)], h.i) in j} +[builtins fixtures/primitives.pyi] + [case testPEP695TypeAliasAndAnnotated] from typing_extensions import Annotated, Annotated as _Annotated import typing_extensions as t diff --git a/test-data/unit/fixtures/primitives.pyi b/test-data/unit/fixtures/primitives.pyi index 83d82ae1ca1d..88d698ecb962 100644 --- a/test-data/unit/fixtures/primitives.pyi +++ b/test-data/unit/fixtures/primitives.pyi @@ -81,3 +81,5 @@ class range(Sequence[int]): def isinstance(x: object, t: Union[type, Tuple]) -> bool: pass class BaseException: pass + +class slice: pass