From e89af29928b9d979a597eb7bfa44643e17181f85 Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Mon, 14 Mar 2022 09:52:57 +0000 Subject: [PATCH] dont skip formatting #%% --- CHANGES.md | 2 ++ src/black/comments.py | 5 ++-- tests/data/comments3.py | 56 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 59 insertions(+), 4 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index edca0dcdad4..c971746a0e5 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,6 +10,8 @@ +- Code cell separators `#%%` are now standardised to `# %%` (#2919) + ### Preview style diff --git a/src/black/comments.py b/src/black/comments.py index 28b9117101d..22c18691275 100644 --- a/src/black/comments.py +++ b/src/black/comments.py @@ -105,7 +105,7 @@ def list_comments(prefix: str, *, is_endmarker: bool) -> List[ProtoComment]: def make_comment(content: str) -> str: """Return a consistently formatted comment from the given `content` string. - All comments (except for "##", "#!", "#:", '#'", "#%%") should have a single + All comments (except for "##", "#!", "#:", '#'") should have a single space between the hash sign and the content. If `content` didn't start with a hash sign, one is provided. @@ -113,7 +113,6 @@ def make_comment(content: str) -> str: content = content.rstrip() if not content: return "#" - if content[0] == "#": content = content[1:] NON_BREAKING_SPACE = " " @@ -123,7 +122,7 @@ def make_comment(content: str) -> str: and not content.lstrip().startswith("type:") ): content = " " + content[1:] # Replace NBSP by a simple space - if content and content[0] not in " !:#'%": + if content and content[0] not in " !:#'": content = " " + content return "#" + content diff --git a/tests/data/comments3.py b/tests/data/comments3.py index fbbef6dcc6b..b7f150d3031 100644 --- a/tests/data/comments3.py +++ b/tests/data/comments3.py @@ -1,4 +1,6 @@ # The percent-percent comments are Spyder IDE cells. +# Both `#%%`` and `# %%` are accepted, so `black` standardises +# to the latter. #%% def func(): @@ -44,4 +46,56 @@ def func(): ) -#%% \ No newline at end of file +#%% + +# output + +# The percent-percent comments are Spyder IDE cells. +# Both `#%%`` and `# %%` are accepted, so `black` standardises +# to the latter. + +# %% +def func(): + x = """ + a really long string + """ + lcomp3 = [ + # This one is actually too long to fit in a single line. + element.split("\n", 1)[0] + # yup + for element in collection.select_elements() + # right + if element is not None + ] + # Capture each of the exceptions in the MultiError along with each of their causes and contexts + if isinstance(exc_value, MultiError): + embedded = [] + for exc in exc_value.exceptions: + if exc not in _seen: + embedded.append( + # This should be left alone (before) + traceback.TracebackException.from_exception( + exc, + limit=limit, + lookup_lines=lookup_lines, + capture_locals=capture_locals, + # copy the set of _seen exceptions so that duplicates + # shared between sub-exceptions are not omitted + _seen=set(_seen), + ) + # This should be left alone (after) + ) + + # everything is fine if the expression isn't nested + traceback.TracebackException.from_exception( + exc, + limit=limit, + lookup_lines=lookup_lines, + capture_locals=capture_locals, + # copy the set of _seen exceptions so that duplicates + # shared between sub-exceptions are not omitted + _seen=set(_seen), + ) + + +# %% \ No newline at end of file