Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test failures with Python 3.9.0b1 #1441

Closed
hroncok opened this issue May 21, 2020 · 16 comments
Closed

Test failures with Python 3.9.0b1 #1441

hroncok opened this issue May 21, 2020 · 16 comments
Labels
T: bug Something isn't working

Comments

@hroncok
Copy link
Contributor

hroncok commented May 21, 2020

Describe the bug

With Python 3.9.0b1, there are test failures:

======================================================================
FAIL: test_beginning_backslash (tests.test_black.BlackTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib64/python3.9/unittest/mock.py", line 1337, in patched
    return func(*newargs, **newkeywargs)
  File ".../black/tests/test_black.py", line 768, in test_beginning_backslash
    black.assert_equivalent(source, actual)
  File ".../black/src/black/__init__.py", line 6050, in assert_equivalent
    raise AssertionError(
AssertionError: INTERNAL ERROR: Black produced code that is not equivalent to the source.  Please report a bug on https://github.com/psf/black/issues.  This diff might be helpful: 
--- src
+++ dst
@@ -1,17 +1,25 @@
 Module(
   body=
-    Print(
-      dest=
-        None,  # NoneType
-      nl=
-        True,  # bool
-      values=
-        Constant(
-          kind=
-            None,  # NoneType
-          value=
-            b'hello, world',  # bytes
-        )  # /Constant
-    )  # /Print
+    Expr(
+      value=
+        Call(
+          args=
+            Constant(
+              kind=
+                None,  # NoneType
+              value=
+                'hello, world',  # str
+            )  # /Constant
+          func=
+            Name(
+              ctx=
+                Load(
+                )  # /Load
+              id=
+                'print',  # str
+            )  # /Name
+          keywords=
+        )  # /Call
+    )  # /Expr
   type_ignores=
 )  # /Module



======================================================================
FAIL: test_expression (tests.test_black.BlackTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ".../black/src/black/__init__.py", line 6029, in assert_equivalent
    src_ast = parse_ast(src)
  File ".../black/src/black/__init__.py", line 5948, in parse_ast
    return ast27.parse(src)
  File ".../black/__venv39__/lib64/python3.9/site-packages/typed_ast/ast27.py", line 50, in parse
    return _ast27.parse(source, filename, mode)
  File "<unknown>", line 1
    ...
    ^
SyntaxError: invalid syntax

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib64/python3.9/unittest/mock.py", line 1337, in patched
    return func(*newargs, **newkeywargs)
  File ".../black/tests/test_black.py", line 331, in test_expression
    black.assert_equivalent(source, actual)
  File ".../black/src/black/__init__.py", line 6031, in assert_equivalent
    raise AssertionError(
AssertionError: cannot use --safe with this file; failed to parse source file.  AST error message: invalid syntax (<unknown>, line 1)

======================================================================
FAIL: test_expression_diff (tests.test_black.BlackTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ".../black/tests/test_black.py", line 375, in test_expression_diff
    self.assertEqual(result.exit_code, 0)
AssertionError: 123 != 0

======================================================================
FAIL: test_expression_diff_with_color (tests.test_black.BlackTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ".../black/tests/test_black.py", line 403, in test_expression_diff_with_color
    self.assertIn("\033[1;37m", actual)
AssertionError: '\x1b[1;37m' not found in ''

======================================================================
FAIL: test_expression_ff (tests.test_black.BlackTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ".../black/src/black/__init__.py", line 6029, in assert_equivalent
    src_ast = parse_ast(src)
  File ".../black/src/black/__init__.py", line 5948, in parse_ast
    return ast27.parse(src)
  File "<unknown>", line 1
    ...
    ^
SyntaxError: invalid syntax

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File ".../black/tests/test_black.py", line 362, in test_expression_ff
    black.assert_equivalent(source, actual)
  File ".../black/src/black/__init__.py", line 6031, in assert_equivalent
    raise AssertionError(
AssertionError: cannot use --safe with this file; failed to parse source file.  AST error message: invalid syntax (<unknown>, line 1)

----------------------------------------------------------------------
Ran 140 tests in 35.840s

FAILED (failures=5)

To Reproduce Steps to reproduce the behavior:

  1. git clone psf/black && cd black
  2. python3.9 -m venv __venv39__
  3. . __venv39__/bin/activate
  4. pip install -e '.[d]'
  5. python -m unittest

Expected behavior is that tests should pass.

Environment (please complete the following information):

  • Version: master (now a2408b3)
  • OS and Python version: Linux (Fedora 32 x86_64) / Python 3.9.0b1

Does this bug also happen on master? yes (but also on the latest version)

@hroncok hroncok added the T: bug Something isn't working label May 21, 2020
@hugovk
Copy link
Contributor

hugovk commented May 21, 2020

Also visible on the CI:

https://travis-ci.com/github/psf/black/jobs/338315058

3.9-dev is allowed to fail, that should be removed once this is fixed now 3.9 is in beta.

@AdamWill
Copy link
Contributor

AdamWill commented Jun 2, 2020

So the problem with expression.py is that ast on Python 3.9 doesn't like (*starred) any more. This is what breaks test_expression, test_expression_ff, test_expression_diff and test_expression_diff_with_color, I think.

Python 3.8:

Python 3.8.3 (default, May 15 2020, 00:00:00) 
[GCC 10.1.1 20200507 (Red Hat 10.1.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ast
>>> ast.parse("(*starred)", feature_version=(3, 8))
<_ast.Module object at 0x7ff7c90a2d00>
>>> ast.parse("(*starred)", feature_version=(3, 9))
<_ast.Module object at 0x7ff7c8fa4d60>
>>> 

Python 3.9:

Python 3.9.0b1 (default, May 29 2020, 00:00:00) 
[GCC 10.1.1 20200507 (Red Hat 10.1.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ast
>>> ast.parse("(*starred)", feature_version=(3, 9))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python3.9/ast.py", line 50, in parse
    return compile(source, filename, mode, flags,
  File "<unknown>", line 1
    (*starred)
              ^
SyntaxError: invalid syntax
>>> ast.parse("(*starred)", feature_version=(3, 8))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python3.9/ast.py", line 50, in parse
    return compile(source, filename, mode, flags,
  File "<unknown>", line 1
    (*starred)
              ^
SyntaxError: invalid syntax

Edit: this is caused by the new parser. Check this:

<mock-chroot> sh-5.0# PYTHONOLDPARSER=1 python3
Python 3.9.0b1 (default, May 29 2020, 00:00:00) 
[GCC 10.1.1 20200507 (Red Hat 10.1.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ast
>>> ast.parse("(*starred)", feature_version=(3, 9))
<ast.Module object at 0x7f25aded52e0>

@JelleZijlstra
Copy link
Collaborator

Even on 3.8 this raises a SyntaxError when you run the code though:

>>> (*x)
  File "<stdin>", line 1
SyntaxError: can't use starred expression here

In what context does Black generate this code?

@AdamWill
Copy link
Contributor

AdamWill commented Jun 2, 2020

Yeah, I noticed that too. You get a different SyntaxError on 3.8 and 3.9, though. On 3.9 it just says "invalid syntax", not "can't use starred expression here".

edit: the PyCF_ONLY_AST flag that ast.parse() uses seems to have an effect here. Check this:

Python 3.8.3 (default, May 15 2020, 00:00:00) 
[GCC 10.1.1 20200507 (Red Hat 10.1.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from _ast import *
>>> compile("(*starred)", "<unknown>", "exec", flags=PyCF_ONLY_AST)
<_ast.Module object at 0x7f129e2cfd00>
>>> compile("(*starred)", "<unknown>", "exec")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<unknown>", line 1
SyntaxError: can't use starred expression here

but:

Python 3.9.0b1 (default, May 29 2020, 00:00:00) 
[GCC 10.1.1 20200507 (Red Hat 10.1.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from _ast import *
>>> compile("(*starred)", "<unknown>", "exec", flags=PyCF_ONLY_AST)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<unknown>", line 1
    (*starred)
              ^
SyntaxError: invalid syntax

i.e. you can compile a simple "(*starred)" with PyCF_ONLY_AST with the old parser, but not the new one.

@AdamWill
Copy link
Contributor

AdamWill commented Jun 2, 2020

OK, so every Python 3.9 problem is caused by the new parser, it seems - if I run PYTHONOLDPARSER=1 python3 setup.py test, all tests pass.

@AdamWill
Copy link
Contributor

AdamWill commented Jun 2, 2020

The 'beginning_backslash' fail seems to be because the new parser considers an empty line after a backslash to be a syntax error:

<mock-chroot> sh-5.0# cat test.py
print("hello, world")
\

print("hello, world 2")
<mock-chroot> sh-5.0# python3 test.py
  File "/builddir/build/BUILD/black-19.10b0/test.py", line 3
    
    ^
SyntaxError: invalid syntax
<mock-chroot> sh-5.0# PYTHONOLDPARSER=1 python3 test.py
hello, world
hello, world 2

@AdamWill
Copy link
Contributor

AdamWill commented Jun 2, 2020

Just for the record, the reason the failures here are a bit obfuscated is because of the parse_ast behaviour where it falls through to trying to parse with ast3 then ast27. In these tests we're expecting the first attempt to parse with ast to work, but when it doesn't, we wind up with errors from the final attempt to parse with ast27, so we get the 'wrong' errors. To find the "real" problems I simply short-circuited that fallthrough behaviour by changing the continue in the first parse block to a raise and re-ran the affected tests.

@JelleZijlstra
Copy link
Collaborator

The issue in #1441 (comment) would seem to be a CPython bug, would you mind reporting it to CPython?

@AdamWill
Copy link
Contributor

AdamWill commented Jun 2, 2020

sure, can do that.

@AdamWill
Copy link
Contributor

AdamWill commented Jun 2, 2020

Filed. I actually also filed an issue for the other problem too, because it seems at least plausible to me it might be considered a bug upstream. Let's see what they say.

@AdamWill
Copy link
Contributor

AdamWill commented Jun 3, 2020

Upstream calls the starred expression thing not a bug, so that will need to be addressed here somehow.

@AdamWill
Copy link
Contributor

AdamWill commented Jun 3, 2020

So, I'd like to suggest a patch for the starred expression thing, but not really sure what to suggest. There's already all of these elsewhere in the file:

call(*gidgets[:2])
[*a]
[*range(10)]
f = 1, *range(10)
g = 1, *"ten"
print(*[] or [1])
print(*lambda x: x)

the two (*starred) lines have existed since the very first commit in the repo, so there's no commit message to helpfully explain exactly what it is they're there to test. Does anyone know? Any ideas for a replacement? Thanks!

@JelleZijlstra
Copy link
Collaborator

Oh, I didn't realize this appeared directly in tests/data/expression.py. Can you just change it to (*starred,)?

@AdamWill
Copy link
Contributor

AdamWill commented Jun 3, 2020

yeah, that seems to work. I'll send a PR for that, I guess.

AdamWill added a commit to AdamWill/black that referenced this issue Jun 3, 2020
As discussed in psf#1441, Python 3.9's new parser will not parse
`(*starred)` even using `compile()` with the `PyCF_ONLY_AST`
flag (as `ast.parse()` does), it raises a `SyntaxError`. This
breaks the four tests that use this file with Python 3.9.
Upstream does not consider this to be a bug - see
https://bugs.python.org/issue40848#msg370643) - so we must
adjust the expression. As suggested by @JelleZijlstra, this just
adds a comma, which makes the new parser happy with it (the old
parser is fine with this form also).

Signed-off-by: Adam Williamson <awilliam@redhat.com>
AdamWill added a commit to AdamWill/black that referenced this issue Jun 3, 2020
As discussed in psf#1441, Python 3.9's new parser will not parse
`(*starred)` even using `compile()` with the `PyCF_ONLY_AST`
flag (as `ast.parse()` does), it raises a `SyntaxError`. This
breaks the four tests that use this file with Python 3.9.
Upstream does not consider this to be a bug - see
https://bugs.python.org/issue40848#msg370643 - so we must
adjust the expression. As suggested by @JelleZijlstra, this just
adds a comma, which makes the new parser happy with it (the old
parser is fine with this form also).

Signed-off-by: Adam Williamson <awilliam@redhat.com>
JelleZijlstra pushed a commit that referenced this issue Jun 3, 2020
…1477)

As discussed in #1441, Python 3.9's new parser will not parse
`(*starred)` even using `compile()` with the `PyCF_ONLY_AST`
flag (as `ast.parse()` does), it raises a `SyntaxError`. This
breaks the four tests that use this file with Python 3.9.
Upstream does not consider this to be a bug - see
https://bugs.python.org/issue40848#msg370643 - so we must
adjust the expression. As suggested by @JelleZijlstra, this just
adds a comma, which makes the new parser happy with it (the old
parser is fine with this form also).

Signed-off-by: Adam Williamson <awilliam@redhat.com>
harenbrs referenced this issue in harenbrs/bleck Jun 5, 2020
expression tests: adjust starred expression for Python 3.9 (#1441) (#1477)
@ichard26
Copy link
Collaborator

CPython 3.9 has been released and it's in our testing matrix and is passing. Unless there are Fedora specific test failures (which seems highly unlikely) and someone speaks up, I'll be closing this issue soon.

@AdamWill
Copy link
Contributor

AdamWill commented Nov 2, 2020

yeah, the blank line issue was fixed upstream and the starred set thing with my PR, so this is all good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants