Repository: psf/black
File: tests/test_black.py (Python)

Code before:
        self.assertEqual(black.get_features_used(node), set())
        node = black.lib2to3_parse(expected)
        self.assertEqual(black.get_features_used(node), set())
        node = black.lib2to3_parse("lambda a, /, b: ...")
        self.assertEqual(black.get_features_used(node), {Feature.POS_ONLY_ARGUMENTS})
        node = black.lib2to3_parse("def fn(a, /, b): ...")
        self.assertEqual(black.get_features_used(node), {Feature.POS_ONLY_ARGUMENTS})
        node = black.lib2to3_parse("def fn(): yield a, b")
        self.assertEqual(black.get_features_used(node), set())
        node = black.lib2to3_parse("def fn(): return a, b")
        self.assertEqual(black.get_features_used(node), set())
        node = black.lib2to3_parse("def fn(): yield *b, c")
        self.assertEqual(black.get_features_used(node), {Feature.UNPACKING_ON_FLOW})
        node = black.lib2to3_parse("def fn(): return a, *b, c")
        self.assertEqual(black.get_features_used(node), {Feature.UNPACKING_ON_FLOW})
        node = black.lib2to3_parse("x = a, *b, c")
        self.assertEqual(black.get_features_used(node), set())
        node = black.lib2to3_parse("x: Any = regular")
        self.assertEqual(black.get_features_used(node), set())
        node = black.lib2to3_parse("x: Any = (regular, regular)")
        self.assertEqual(black.get_features_used(node), set())
        node = black.lib2to3_parse("x: Any = Complex(Type(1))[something]")
        self.assertEqual(black.get_features_used(node), set())
        node = black.lib2to3_parse("x: Tuple[int, ...] = a, b, c")
        self.assertEqual(
            black.get_features_used(node), {Feature.ANN_ASSIGN_EXTENDED_RHS}
        )
        node = black.lib2to3_parse("try: pass\nexcept Something: pass")
        self.assertEqual(black.get_features_used(node), set())
        node = black.lib2to3_parse("try: pass\nexcept (*Something,): pass")
        self.assertEqual(black.get_features_used(node), set())
        node = black.lib2to3_parse("try: pass\nexcept *Group: pass")
        self.assertEqual(black.get_features_used(node), {Feature.EXCEPT_STAR})
        node = black.lib2to3_parse("a[*b]")
        self.assertEqual(black.get_features_used(node), {Feature.VARIADIC_GENERICS})
        node = black.lib2to3_parse("a[x, *y(), z] = t")
        self.assertEqual(black.get_features_used(node), {Feature.VARIADIC_GENERICS})
        node = black.lib2to3_parse("def fn(*args: *T): pass")
        self.assertEqual(black.get_features_used(node), {Feature.VARIADIC_GENERICS})

    def test_get_features_used_for_future_flags(self) -> None:
        for src, features in [
            ("from __future__ import annotations", {Feature.FUTURE_ANNOTATIONS}),
            (
                "from __future__ import (other, annotations)",
                {Feature.FUTURE_ANNOTATIONS},
            ),
            ("a = 1 + 2\nfrom something import annotations", set()),
            ("from __future__ import x, y", set()),
        ]:
            with self.subTest(src=src, features=features):

Review comment (bug):
These are the new tests. Several of them would have previously failed. The case that I ran into in one of our tests was

```
with (x := await a, y := await b):
        pass
```


Code after:
        self.check_features_used("def fn(): yield a, b", set())
        self.check_features_used("def fn(): return a, b", set())
        self.check_features_used("def fn(): yield *b, c", {Feature.UNPACKING_ON_FLOW})
        self.check_features_used(
            "def fn(): return a, *b, c", {Feature.UNPACKING_ON_FLOW}
        )
        self.check_features_used("x = a, *b, c", set())

        self.check_features_used("x: Any = regular", set())
        self.check_features_used("x: Any = (regular, regular)", set())
        self.check_features_used("x: Any = Complex(Type(1))[something]", set())
        self.check_features_used(
            "x: Tuple[int, ...] = a, b, c", {Feature.ANN_ASSIGN_EXTENDED_RHS}
        )

        self.check_features_used("try: pass\nexcept Something: pass", set())
        self.check_features_used("try: pass\nexcept (*Something,): pass", set())
        self.check_features_used(
            "try: pass\nexcept *Group: pass", {Feature.EXCEPT_STAR}
        )

        self.check_features_used("a[*b]", {Feature.VARIADIC_GENERICS})
        self.check_features_used("a[x, *y(), z] = t", {Feature.VARIADIC_GENERICS})
        self.check_features_used("def fn(*args: *T): pass", {Feature.VARIADIC_GENERICS})

        self.check_features_used("with a: pass", set())
        self.check_features_used("with a, b: pass", set())
        self.check_features_used("with a as b: pass", set())
        self.check_features_used("with a as b, c as d: pass", set())
        self.check_features_used("with (a): pass", set())
        self.check_features_used("with (a, b): pass", set())
        self.check_features_used("with (a, b) as (c, d): pass", set())
        self.check_features_used(
            "with (a as b): pass", {Feature.PARENTHESIZED_CONTEXT_MANAGERS}
        )
        self.check_features_used(
            "with ((a as b)): pass", {Feature.PARENTHESIZED_CONTEXT_MANAGERS}
        )
        self.check_features_used(
            "with (a, b as c): pass", {Feature.PARENTHESIZED_CONTEXT_MANAGERS}
        )
        self.check_features_used(
            "with (a, (b as c)): pass", {Feature.PARENTHESIZED_CONTEXT_MANAGERS}
        )
        self.check_features_used(
            "with ((a, ((b as c)))): pass", {Feature.PARENTHESIZED_CONTEXT_MANAGERS}
        )

    def check_features_used(self, source: str, expected: Set[Feature]) -> None:
        node = black.lib2to3_parse(source)
        actual = black.get_features_used(node)