Skip to content

Commit

Permalink
Refactored function to ensure purity and updated preview style
Browse files Browse the repository at this point in the history
  • Loading branch information
iamaparnaojha committed Dec 30, 2024
1 parent 3d64c79 commit f723fc7
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/black/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -935,11 +935,15 @@ def is_type_comment(leaf: Leaf) -> bool:
if leaf.type not in {token.COMMENT, STANDALONE_COMMENT}:
return False
comment_text = leaf.value.lstrip("#").lstrip()
if not comment_text.startswith("type:"):
return False
type_annotation = comment_text[5:].strip()
leaf.value = f"# type: {type_annotation}" if type_annotation else "# type:"
return True
return comment_text.startswith("type:") and not is_type_ignore_comment_string(comment_text)


def normalize_type_comment(leaf: Leaf) -> str:
"""Normalize a type comment by cleaning up whitespace after # and :.
This should only be called after confirming the leaf is a type comment using is_type_comment()."""
comment_text = leaf.value.lstrip("#").lstrip()
type_annotation = comment_text[4:].lstrip(":").strip()
return f"# type: {type_annotation}" if type_annotation else "# type:"


def is_type_ignore_comment(leaf: Leaf) -> bool:
Expand Down

0 comments on commit f723fc7

Please sign in to comment.