black icon indicating copy to clipboard operation
black copied to clipboard

Fix #2097: Recognize type comments with spaces after #

Open anthonychen000 opened this issue 1 year ago • 0 comments

Fixes #2097

Description

Currently, Black fails to recognize # type: and # type: ignore comments if there's extra whitespace after the # (e.g., # type:), as described in issue #2097. This can lead to these comments being treated as regular comments, potentially causing incorrect formatting or instability.

This PR updates the comment recognition logic (is_type_comment, is_type_ignore_comment, and helper functions like is_type_comment_string / is_type_ignore_comment_string) to correctly handle this leading whitespace after the #. It allows comments like # type: value and # type: ignore to be correctly identified.

As discussed in the issue thread, this change focuses only on fixing the recognition part of the bug and does not implement comment standardization (i.e., it doesn't reformat # type: to # type:).

Additionally, this PR includes a small fix for unrelated mypy errors ([import-untyped, unreachable]) encountered in action/main.py which were required to pass local pre-commit checks.

anthonychen000 avatar Apr 22 '25 06:04 anthonychen000