1111 WHITESPACE ,
1212 container_of ,
1313 first_leaf_of ,
14+ is_type_comment_string ,
1415 make_simple_prefix ,
1516 preceding_leaf ,
1617 syms ,
@@ -50,7 +51,7 @@ class ProtoComment:
5051 leading_whitespace : str # leading whitespace before the comment, if any
5152
5253
53- def generate_comments (leaf : LN ) -> Iterator [Leaf ]:
54+ def generate_comments (leaf : LN , mode : Mode ) -> Iterator [Leaf ]:
5455 """Clean the prefix of the `leaf` and generate comments from it, if any.
5556
5657 Comments in lib2to3 are shoved into the whitespace prefix. This happens
@@ -70,15 +71,17 @@ def generate_comments(leaf: LN) -> Iterator[Leaf]:
7071 are emitted with a fake STANDALONE_COMMENT token identifier.
7172 """
7273 total_consumed = 0
73- for pc in list_comments (leaf .prefix , is_endmarker = leaf .type == token .ENDMARKER ):
74+ for pc in list_comments (
75+ leaf .prefix , is_endmarker = leaf .type == token .ENDMARKER , mode = mode
76+ ):
7477 total_consumed = pc .consumed
7578 prefix = make_simple_prefix (pc .newlines , pc .form_feed )
7679 yield Leaf (pc .type , pc .value , prefix = prefix )
7780 normalize_trailing_prefix (leaf , total_consumed )
7881
7982
8083@lru_cache (maxsize = 4096 )
81- def list_comments (prefix : str , * , is_endmarker : bool ) -> list [ProtoComment ]:
84+ def list_comments (prefix : str , * , is_endmarker : bool , mode : Mode ) -> list [ProtoComment ]:
8285 """Return a list of :class:`ProtoComment` objects parsed from the given `prefix`."""
8386 result : list [ProtoComment ] = []
8487 if not prefix or "#" not in prefix :
@@ -109,7 +112,7 @@ def list_comments(prefix: str, *, is_endmarker: bool) -> list[ProtoComment]:
109112 comment_type = token .COMMENT # simple trailing comment
110113 else :
111114 comment_type = STANDALONE_COMMENT
112- comment = make_comment (line )
115+ comment = make_comment (line , mode = mode )
113116 result .append (
114117 ProtoComment (
115118 type = comment_type ,
@@ -140,7 +143,7 @@ def normalize_trailing_prefix(leaf: LN, total_consumed: int) -> None:
140143 leaf .prefix = ""
141144
142145
143- def make_comment (content : str ) -> str :
146+ def make_comment (content : str , mode : Mode ) -> str :
144147 """Return a consistently formatted comment from the given `content` string.
145148
146149 All comments (except for "##", "#!", "#:", '#'") should have a single
@@ -157,9 +160,18 @@ def make_comment(content: str) -> str:
157160 if (
158161 content
159162 and content [0 ] == "\N{NO-BREAK SPACE} "
160- and not content .lstrip (). startswith ( "type:" )
163+ and not is_type_comment_string ( "# " + content .lstrip (), mode = mode )
161164 ):
162165 content = " " + content [1 :] # Replace NBSP by a simple space
166+ if (
167+ Preview .standardize_type_comments in mode
168+ and content
169+ and "\N{NO-BREAK SPACE} " not in content
170+ and is_type_comment_string ("#" + content , mode = mode )
171+ ):
172+ type_part , value_part = content .split (":" , 1 )
173+ content = type_part .strip () + ": " + value_part .strip ()
174+
163175 if content and content [0 ] not in COMMENT_EXCEPTIONS :
164176 content = " " + content
165177 return "#" + content
@@ -183,7 +195,7 @@ def convert_one_fmt_off_pair(
183195 """
184196 for leaf in node .leaves ():
185197 previous_consumed = 0
186- for comment in list_comments (leaf .prefix , is_endmarker = False ):
198+ for comment in list_comments (leaf .prefix , is_endmarker = False , mode = mode ):
187199 is_fmt_off = comment .value in FMT_OFF
188200 is_fmt_skip = _contains_fmt_skip_comment (comment .value , mode )
189201 if (not is_fmt_off and not is_fmt_skip ) or (
@@ -273,13 +285,13 @@ def generate_ignored_nodes(
273285 return
274286 container : Optional [LN ] = container_of (leaf )
275287 while container is not None and container .type != token .ENDMARKER :
276- if is_fmt_on (container ):
288+ if is_fmt_on (container , mode = mode ):
277289 return
278290
279291 # fix for fmt: on in children
280- if children_contains_fmt_on (container ):
292+ if children_contains_fmt_on (container , mode = mode ):
281293 for index , child in enumerate (container .children ):
282- if isinstance (child , Leaf ) and is_fmt_on (child ):
294+ if isinstance (child , Leaf ) and is_fmt_on (child , mode = mode ):
283295 if child .type in CLOSING_BRACKETS :
284296 # This means `# fmt: on` is placed at a different bracket level
285297 # than `# fmt: off`. This is an invalid use, but as a courtesy,
@@ -290,12 +302,14 @@ def generate_ignored_nodes(
290302 if (
291303 child .type == token .INDENT
292304 and index < len (container .children ) - 1
293- and children_contains_fmt_on (container .children [index + 1 ])
305+ and children_contains_fmt_on (
306+ container .children [index + 1 ], mode = mode
307+ )
294308 ):
295309 # This means `# fmt: on` is placed right after an indentation
296310 # level, and we shouldn't swallow the previous INDENT token.
297311 return
298- if children_contains_fmt_on (child ):
312+ if children_contains_fmt_on (child , mode = mode ):
299313 return
300314 yield child
301315 else :
@@ -316,7 +330,7 @@ def _generate_ignored_nodes_from_fmt_skip(
316330 ignored_nodes : list [LN ] = []
317331 # Need to properly format the leaf prefix to compare it to comment.value,
318332 # which is also formatted
319- comments = list_comments (leaf .prefix , is_endmarker = False )
333+ comments = list_comments (leaf .prefix , is_endmarker = False , mode = mode )
320334 if not comments or comment .value != comments [0 ].value :
321335 return
322336 if prev_sibling is not None :
@@ -392,24 +406,24 @@ def _generate_ignored_nodes_from_fmt_skip(
392406 yield from iter (ignored_nodes )
393407
394408
395- def is_fmt_on (container : LN ) -> bool :
409+ def is_fmt_on (container : LN , mode : Mode ) -> bool :
396410 """Determine whether formatting is switched on within a container.
397411 Determined by whether the last `# fmt:` comment is `on` or `off`.
398412 """
399413 fmt_on = False
400- for comment in list_comments (container .prefix , is_endmarker = False ):
414+ for comment in list_comments (container .prefix , is_endmarker = False , mode = mode ):
401415 if comment .value in FMT_ON :
402416 fmt_on = True
403417 elif comment .value in FMT_OFF :
404418 fmt_on = False
405419 return fmt_on
406420
407421
408- def children_contains_fmt_on (container : LN ) -> bool :
422+ def children_contains_fmt_on (container : LN , mode : Mode ) -> bool :
409423 """Determine if children have formatting switched on."""
410424 for child in container .children :
411425 leaf = first_leaf_of (child )
412- if leaf is not None and is_fmt_on (leaf ):
426+ if leaf is not None and is_fmt_on (leaf , mode = mode ):
413427 return True
414428
415429 return False
0 commit comments