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

Remove NBSP at the beggining of comments #2092

Merged
merged 3 commits into from
Apr 11, 2021

Conversation

Pierre-Sassoulas
Copy link
Contributor

@Pierre-Sassoulas Pierre-Sassoulas commented Apr 7, 2021

This is a fast attempt to do #2091. I suppose we could remove the while and put a simple if to not remove multiple consecutive NBSP.

Closes #2091

@ichard26
Copy link
Collaborator

ichard26 commented Apr 10, 2021

Looking into this, the reason why Python 3.6 and 3.7 are failing is probably because typed_ast and ast doesn't recognize "#\N{NO-BREAK SPACE}type: ignore" as an actual type ignore comment. The reason why only 3.6 and 3.7 is failing is because typed_ast has a special field for type ignore comments in the AST. The built-in parser (ast) which is used on Python 3.8 and higher has its type ignore comment specific tracking features disabled which means the "apparently invalid type ignore comment turning into valid one" effect goes unnoticed.

@JelleZijlstra
Copy link
Collaborator

Thanks for the diagnosis @ichard26! I think for safety we should just leave any nbsps in type comments alone.

@Pierre-Sassoulas
Copy link
Contributor Author

@JelleZijlstra I believe it's now ready for review. Thanks @ichard26 for dong all the leg work :) !

Comment on lines 2712 to 2713
NON_BREAKING_SPACE = " "
if content and content[0] == NON_BREAKING_SPACE and "type: ignore" not in content:
Copy link
Collaborator

@ichard26 ichard26 Apr 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, you can also use Unicode name escapes, so instead of a defining an aptly named variable, you could just replace it with "\N{NO-BREAK SPACE}"

Now does it matter? Probably not since it's arguable which one is more readable, but I'd like to point it out to you as a language feature you may have not known :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks I did not know ! 😄

Copy link
Collaborator

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! The code looks good now, but I think the tests will be cleaner in a separate file.

@@ -4614,7 +4621,7 @@ def TErr(err_msg: str) -> Err[CannotTransform]:
def contains_pragma_comment(comment_list: List[Leaf]) -> bool:
"""
Returns:
True iff one of the comments in @comment_list is a pragma used by one
True if one of the comments in @comment_list is a pragma used by one
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iff is correct here, it means "if and only if" (https://en.wikipedia.org/wiki/If_and_only_if)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad 😅

@@ -6,7 +6,7 @@
Int,
Path,
# String,
# resolve_to_config_type,
#  resolve_to_config_type,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's going to be hard for readers of this file to recognize what's going on here, because nbsps look the same as spaces pretty much all the time, at least in environments I'm familiar with.

What about undoing the changes to this file and instead adding a new test like nbsp.py just for testing comments with nbsps in them? You'll have to add a new test function in test_black.py, but that's pretty easy.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copy pasted comment7.py with a new name and added NBSP as the first character of each comments. The reflexion is that there was a lot of different test cases in comment7 but let me know if simple test cases (normal comment, commen with a lot of leading spaces, type ignore comment, type: Optional[Squre] comment ?) is preferable.

CHANGES.md Outdated
@@ -4,6 +4,9 @@

#### _Black_

- `Black` now remove leading non-breaking spaces in comments before adding a space
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `Black` now remove leading non-breaking spaces in comments before adding a space
- `Black` now cleans up leading non-breaking spaces in comments

I think this is better; we don't need to be too specific about the exact output.

Pierre-Sassoulas referenced this pull request in Pierre-Sassoulas/black Apr 11, 2021
The switch to unvalid 'type: ignore' to valid 'type: ignore' is a
lot more complicated to do.

See:https://github.com/psf/black/pull/2092\#issuecomment-817178166
Pierre-Sassoulas referenced this pull request in Pierre-Sassoulas/black Apr 11, 2021
The switch to unvalid 'type: ignore' to valid 'type: ignore' is a
lot more complicated to do.

See:https://github.com/psf/black/pull/2092\#issuecomment-817178166
@@ -0,0 +1,271 @@
from .config import (
Any,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove some extraneous code here? I think most of the imports on this line can go for example, and most of the cases in class C below aren't really relevant either. I'd want this test file to have only comments with nbsps and elements that are directly related (e.g., to test behavior around moving comments).

The switch to unvalid 'type: ignore' to valid 'type: ignore' is a
lot more complicated to do.

See:https://github.com/psf/black/pull/2092\#issuecomment-817178166
Also handle notmal comment not starting with 'type:' better.
Copy link
Collaborator

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@JelleZijlstra JelleZijlstra merged commit d960d5d into psf:master Apr 11, 2021
@Pierre-Sassoulas
Copy link
Contributor Author

I made the test a little too nasty and found a bug:

def function(a:int=42):
    """ This docstring start with a NBSP
        There is one column 5 here
        This is 7 NBSP
    """

Error log:

--- src
+++ dst
@@ -162,11 +162,11 @@
       body=
         Expr(
           value=
             Constant(
               value=
-                'This docstring start with a NBSP\n\xa0   There is one column 5 here\n\xa0\xa0\xa0\xa0This is 7 NBSP',  # str
+                'This docstring start with a NBSP\nThere is one column 5 here\nThis is 7 NBSP',  # str
             )  # /Constant
         )  # /Expr
         Pass(
         )  # /Pass
       decorator_list=

Should I open a new issue or address it here ?

@JelleZijlstra
Copy link
Collaborator

Oops, too late. Could you just file a new PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove non-breaking space at the beggining of comments
3 participants