From ee285704542a00bc44522f433f86385656195294 Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Wed, 7 Apr 2021 20:52:37 +0200 Subject: [PATCH] Remove NBSP at the beggining of comments Closes #2091 --- CHANGES.md | 2 ++ src/black/__init__.py | 2 ++ tests/data/comments7.py | 18 +++++++++--------- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 97a3be33c93..1db24aac8d3 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,6 +4,8 @@ #### _Black_ +- `Black` now remove leading non-breaking spaces in comments before adding a space (#2092) + - `Black` now respects `--skip-string-normalization` when normalizing multiline docstring quotes (#1637) diff --git a/src/black/__init__.py b/src/black/__init__.py index 431ee027270..0dd15d42743 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -2709,6 +2709,8 @@ def make_comment(content: str) -> str: if content[0] == "#": content = content[1:] + if content and content[0] == " ": # This is a non-breaking space + content = " " + content[1:] # And this is a simple space if content and content[0] not in " !:#'%": content = " " + content return "#" + content diff --git a/tests/data/comments7.py b/tests/data/comments7.py index 0e2bd35bf55..bd81e700765 100644 --- a/tests/data/comments7.py +++ b/tests/data/comments7.py @@ -6,7 +6,7 @@ Int, Path, # String, - # resolve_to_config_type, + #  resolve_to_config_type, # DEFAULT_TYPE_ATTRIBUTES, ) @@ -18,12 +18,12 @@ ConfigTypeAttributes, Int, no_comma_here_yet - # and some comments, + #  and some comments, # resolve_to_config_type, # DEFAULT_TYPE_ATTRIBUTES, ) from com.my_lovely_company.my_lovely_team.my_lovely_project.my_lovely_component import ( - MyLovelyCompanyTeamProjectComponent # NOT DRY + MyLovelyCompanyTeamProjectComponent # NOT DRY ) from com.my_lovely_company.my_lovely_team.my_lovely_project.my_lovely_component import ( MyLovelyCompanyTeamProjectComponent as component # DRY @@ -33,7 +33,7 @@ result = 1 # look ma, no comment migration xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx result = ( - 1 # look ma, no comment migration xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + 1 # look ma, no comment migration xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ) result = ( @@ -50,7 +50,7 @@ def func(): 0.0789, 0.0123, 0.0789, - a[-1], # type: ignore + a[-1], # type: ignore ) c = call( 0.0123, @@ -73,7 +73,7 @@ def func(): a[-1] # type: ignore ) - # The type: ignore exception only applies to line length, not + # The type: ignore exception only applies to line length, not # other types of formatting. c = call( "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", # type: ignore @@ -85,7 +85,7 @@ class C: @pytest.mark.parametrize( ("post_data", "message"), [ - # metadata_version errors. + # metadata_version errors. ( {}, "None is an invalid value for Metadata-Version. Error: This field is" @@ -98,7 +98,7 @@ class C: " Version see" " https://packaging.python.org/specifications/core-metadata" ), - # name errors. + # name errors. ( {"metadata_version": "1.2"}, "'' is an invalid value for Name. Error: This field is required. see" @@ -110,7 +110,7 @@ class C: " letter or numeral and contain only ascii numeric and '.', '_' and" " '-'. see https://packaging.python.org/specifications/core-metadata" ), - # version errors. + # version errors. ( {"metadata_version": "1.2", "name": "example"}, "'' is an invalid value for Version. Error: This field is required. see"