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

Internal error: AST change with bytes docstring #4002

Closed
konstin opened this issue Oct 30, 2023 · 0 comments · Fixed by #4003
Closed

Internal error: AST change with bytes docstring #4002

konstin opened this issue Oct 30, 2023 · 0 comments · Fixed by #4003
Labels
T: bug Something isn't working

Comments

@konstin
Copy link
Contributor

konstin commented Oct 30, 2023

Black reformats the contents of triple quoted byte strings in docstring position, even though those are not docstrings, causing a crash due to AST changes:

def f():
    b""" a"""
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: /tmp/blk_rb9415ox.log

--- src
+++ dst
@@ -18,11 +18,11 @@
           value=
             Constant(
               kind=
                 None,  # NoneType
               value=
-                b' a',  # bytes
+                b'a',  # bytes
             )  # /Constant
         )  # /Expr
       decorator_list=
       name=
         'f',  # str
$ python3.12
Python 3.12.0 (main, Oct  6 2023, 17:57:44) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def f():
...     b""" a"""
... 
>>> print(str(f.__doc__))
None

Surprisingly, the empty line rules for class level docstrings look already correct.

Confirmed to occur on 23.10.1 and main (f7cbe4a) with the playground.

@konstin konstin added the T: bug Something isn't working label Oct 30, 2023
konstin added a commit to astral-sh/ruff that referenced this issue Oct 30, 2023
We previously incorrectly treated byte strings in docstring position as docstrings because black does so (#8283 (comment), psf/black#4002), even CPython doesn't recognize them:

```console
$ python3.12
Python 3.12.0 (main, Oct  6 2023, 17:57:44) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def f():
...     b""" a"""
...
>>> print(str(f.__doc__))
None
```
konstin added a commit to astral-sh/ruff that referenced this issue Oct 30, 2023
We previously incorrectly treated byte strings in docstring position as
docstrings because black does so
(#8283 (comment),
psf/black#4002), even CPython doesn't
recognize them:

```console
$ python3.12
Python 3.12.0 (main, Oct  6 2023, 17:57:44) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def f():
...     b""" a"""
...
>>> print(str(f.__doc__))
None
```

<!--
Thank you for contributing to Ruff! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title?
- Does this pull request include references to any relevant issues?
-->
JelleZijlstra added a commit to JelleZijlstra/black that referenced this issue Oct 30, 2023
Fixes psf#4002

I think this only fixes crashes so it doesn't need to go through the preview style,
but not 100% sure.
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

Successfully merging a pull request may close this issue.

1 participant