-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Conversation
ee28570
to
145d46e
Compare
Looking into this, the reason why Python 3.6 and 3.7 are failing is probably because |
Thanks for the diagnosis @ichard26! I think for safety we should just leave any nbsps in type comments alone. |
4732453
to
3b7b0b7
Compare
3b7b0b7
to
8e55788
Compare
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
8e55788
to
f66a033
Compare
@JelleZijlstra I believe it's now ready for review. Thanks @ichard26 for dong all the leg work :) ! |
src/black/__init__.py
Outdated
NON_BREAKING_SPACE = " " | ||
if content and content[0] == NON_BREAKING_SPACE and "type: ignore" not in content: |
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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 ! 😄
There was a problem hiding this 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.
src/black/__init__.py
Outdated
@@ -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 |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad 😅
tests/data/comments7.py
Outdated
@@ -6,7 +6,7 @@ | |||
Int, | |||
Path, | |||
# String, | |||
# resolve_to_config_type, | |||
# resolve_to_config_type, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- `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.
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
d3288db
to
41d50de
Compare
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
41d50de
to
d7a633c
Compare
@@ -0,0 +1,271 @@ | |||
from .config import ( | |||
Any, |
There was a problem hiding this comment.
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.
d7a633c
to
08fe7f1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
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:
Should I open a new issue or address it here ? |
Oops, too late. Could you just file a new PR? |
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