Commit 88368936 authored by Zsolt Dollenstein's avatar Zsolt Dollenstein Committed by Łukasz Langa
Browse files

Support parsing of async generators in non-async functions (#165)

This is a new syntax added in python3.7, so black can't verify that reformatting will not change the ast unless black itself is run with 3.7. We'll need to change the error message black gives in this case. @ambv any ideas?

Fixes #125.
parent b719d85c
Loading
Loading
Loading
Loading
+2 −1
Changes for blib2to3/pgen2/tokenize.py: 2 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -516,11 +516,12 @@ def generate_tokens(readline):
                        stashed = tok
                        continue

                    if token == 'def':
                    if token in ('def', 'for'):
                        if (stashed
                                and stashed[0] == NAME
                                and stashed[1] == 'async'):

                            if token == 'def':
                                async_def = True
                                async_def_indent = indents[-1]

tests/data/python37.py

0 → 100644
+13 −0
Changes for tests/data/python37.py: 13 added lines, 0 removed lines.
Original line number Diff line number Diff line
#!/usr/bin/env python3.7

def f():
    return (i*2 async for i in arange(42))

# output


#!/usr/bin/env python3.7


def f():
    return (i * 2 async for i in arange(42))
+10 −0
Changes for tests/test_black.py: 10 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -411,6 +411,16 @@ def test_stub(self) -> None:
        self.assertFormatEqual(expected, actual)
        black.assert_stable(source, actual, line_length=ll, mode=mode)

    @patch("black.dump_to_file", dump_to_stderr)
    def test_python37(self) -> None:
        source, expected = read_data("python37")
        actual = fs(source)
        self.assertFormatEqual(expected, actual)
        major, minor = sys.version_info[:2]
        if major > 3 or (major == 3 and minor >= 7):
            black.assert_equivalent(source, actual)
        black.assert_stable(source, actual, line_length=ll)

    @patch("black.dump_to_file", dump_to_stderr)
    def test_fmtonoff(self) -> None:
        source, expected = read_data("fmtonoff")