Skip to content

Fix #2097: Recognize type comments with spaces after # #4652

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

anthonychen000
Copy link

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.

Updated comment recognition functions (is_type_comment, is_type_ignore_comment,
and helpers) to correctly identify type/ignore comments even when extra
whitespace exists between '#' and 'type:'.

This addresses the core recognition bug described in issue psf#2097 without
implementing comment standardization. Includes test case and CHANGELOG entry.
Also includes unrelated mypy fix for action/main.py.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

is_type_comment() is wrong
1 participant