black icon indicating copy to clipboard operation
black copied to clipboard

is_type_comment() is wrong

Open JelleZijlstra opened this issue 4 years ago • 15 comments

Currently, the implementation of is_type_comment() looks like this:

def is_type_comment(leaf: Leaf, suffix: str = "") -> bool:
    """Return True if the given leaf is a special comment.
    Only returns true for type comments for now."""
    t = leaf.type
    v = leaf.value
    return t in {token.COMMENT, STANDALONE_COMMENT} and v.startswith("# type:" + suffix)

But this is wrong, because typed-ast also recognizes a comment with multiple spaces after the # as a type comment. Concretely this could lead Black to skip some of the checks we have to avoid moving type comments to the wrong line.

Ideally, Black should also standardize the formatting of type comments by cleaning up excessive whitespace after the # and after the :.

JelleZijlstra avatar Apr 11 '21 13:04 JelleZijlstra

I think there is also the pragmas in contains_pragma_comment at: comment.value.startswith(("# type:", "# noqa", "# pylint:")

Pierre-Sassoulas avatar Apr 11 '21 19:04 Pierre-Sassoulas

Should we also standardise spaces before the colon? Meaning we would accept (and fix) e.g. # type : Type or even # \t type : (some unexpected whitespace type) Type?

felix-hilden avatar Dec 15 '21 14:12 felix-hilden

Hey everyone! Can I get some maintainer reivews on the PR?

itxasos23 avatar Oct 24 '22 17:10 itxasos23

@Pierre-Sassoulas Hello... I am new here and I would like to start contributing in the repository. So can you please assign this issue to me Thank you

xantric avatar Dec 07 '23 08:12 xantric

Hey @IshanKamboj, sorry I'm not a black maintainer. I think you can start implementing right away and open a PR though :)

Pierre-Sassoulas avatar Dec 07 '23 10:12 Pierre-Sassoulas

hello i am new here and would like to contribute to the project can you help me out

debapriyakumar avatar May 25 '24 13:05 debapriyakumar