Repository: psf/black

Issue:
Remove non-breaking space at the beggining of comments
Black apply a space after a ``#`` in comment like so:
```diff
- #Comment content 
+ # Comment content 
```
The thing is, often we developers fat fingers an insecable space after creating the ``#`` by not releasing the <kbd>Alt Gr</kbd> key fast enough.

Then black had a space before it resulting in visually two spaces:
```diff
- # Comment content 
+ #  Comment content 
```

I'd like black to remove insecable spaces at the beginning of comment and then replaces them with a real space:
```diff
- # Comment content 
+ # Comment content 
```



Discussion:
By "insecable space" do you mean the non-breaking space (nbsp) character https://www.compart.com/en/unicode/U+00A0 ? If so it seems reasonable to me for Black to replace leading nbsps in comments.
Yes, sorry if it was not clear I translated the name from french.
I'm interested to work on this, if a pull request with this change is going to be merged.
I'd merge it (unless someone else comes up with a problem with this suggestion).
I made an attempt, but I have a [crash with type ignore comment for python 3.6 and 3.7](https://github.com/psf/black/pull/2092/checks?check_run_id=2290658688) and I don't quite know what to do with it. Do you have an advise on how to debug it ? 
@ichard26 answered my question : https://github.com/psf/black/pull/2092#issuecomment-817178166

Resolving patch:
diff --git a/src/black/__init__.py b/src/black/__init__.py
--- a/src/black/__init__.py
+++ b/src/black/__init__.py
@@ -2709,6 +2709,13 @@ def make_comment(content: str) -> str:
 
     if content[0] == "#":
         content = content[1:]
+    NON_BREAKING_SPACE = " "
+    if (
+        content
+        and content[0] == NON_BREAKING_SPACE
+        and not content.lstrip().startswith("type:")
+    ):
+        content = " " + content[1:]  # Replace NBSP by a simple space
     if content and content[0] not in " !:#'%":
         content = " " + content
     return "#" + content


Test patch:
diff --git a/tests/data/comments7.py b/tests/data/comments7.py
--- a/tests/data/comments7.py
+++ b/tests/data/comments7.py
@@ -129,6 +129,8 @@ def test_fails_invalid_post_data(
     ):
         ...
 
+square = Square(4) # type: Optional[Square]
+
 # output
 
 from .config import (
@@ -263,4 +265,7 @@ class C:
     def test_fails_invalid_post_data(
         self, pyramid_config, db_request, post_data, message
     ):
-        ...
\ No newline at end of file
+        ...
+
+
+square = Square(4)  # type: Optional[Square]
diff --git a/tests/data/comments_non_breaking_space.py b/tests/data/comments_non_breaking_space.py
new file mode 100644
--- /dev/null
+++ b/tests/data/comments_non_breaking_space.py
@@ -0,0 +1,44 @@
+from .config import (  ConfigTypeAttributes,    Int,    Path,    # String,
+    # DEFAULT_TYPE_ATTRIBUTES,
+)
+
+result = 1  # A simple comment
+result = (    1, ) # Another one
+
+result = 1    # type: ignore
+result = 1# This comment is talking about type: ignore
+square = Square(4) # type: Optional[Square]
+
+def function(a:int=42):
+    """ This docstring is already formatted
+       a
+       b
+    """
+    #    There's a NBSP + 3 spaces before
+    #    And 4 spaces on the next line
+    pass
+
+# output
+from .config import (
+    ConfigTypeAttributes,
+    Int,
+    Path,  # String,
+    # DEFAULT_TYPE_ATTRIBUTES,
+)
+
+result = 1  # A simple comment
+result = (1,)  # Another one
+
+result = 1  #  type: ignore
+result = 1  # This comment is talking about type: ignore
+square = Square(4)  #  type: Optional[Square]
+
+
+def function(a: int = 42):
+    """This docstring is already formatted
+    a
+    b
+    """
+    #    There's a NBSP + 3 spaces before
+    #    And 4 spaces on the next line
+    pass
diff --git a/tests/test_format.py b/tests/test_format.py
--- a/tests/test_format.py
+++ b/tests/test_format.py
@@ -27,6 +27,7 @@
     "comments5",
     "comments6",
     "comments7",
+    "comments_non_breaking_space",
     "comment_after_escaped_newline",
     "composition",
     "composition_no_trailing_comma",