Skip to content

Commit 68cd137

Browse files
is_type_comment now checks for extra or zero spaces (#2097)
1 parent f1a2f92 commit 68cd137

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/black/nodes.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
blib2to3 Node/Leaf transformation-related utility functions.
33
"""
44

5+
import re
56
import sys
67
from typing import Final, Generic, Iterator, Literal, Optional, TypeVar, Union
78

@@ -911,7 +912,10 @@ def is_type_comment(leaf: Leaf) -> bool:
911912
used in modern version of Python, this function may be deprecated in the future."""
912913
t = leaf.type
913914
v = leaf.value
914-
return t in {token.COMMENT, STANDALONE_COMMENT} and v.startswith("# type:")
915+
return (
916+
t in {token.COMMENT, STANDALONE_COMMENT}
917+
and bool(re.match(r"#\s*type:", v))
918+
)
915919

916920

917921
def is_type_ignore_comment(leaf: Leaf) -> bool:

tests/test_nodes.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""Test the black.nodes module."""
2+
3+
import pytest
4+
5+
from black import Leaf, token
6+
from black.nodes import is_type_comment
7+
8+
9+
@pytest.mark.parametrize(
10+
"comment_text, expected",
11+
[
12+
("# type: int", True),
13+
("#type:int", True),
14+
("# type: str", True),
15+
("# type :List[int]", False),
16+
("# type : Dict[str, Any]", False),
17+
("#type :", False),
18+
("#type: ", True),
19+
("#", False),
20+
("# some other comment type: ", False),
21+
("# type", False),
22+
("# type:", True),
23+
("# type :", False),
24+
],
25+
)
26+
def test_is_type_comment(comment_text: str, expected: bool) -> None:
27+
leaf = Leaf(token.COMMENT, comment_text)
28+
assert is_type_comment(leaf) == expected

0 commit comments

Comments
 (0)