Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Closed
anthonychen000 wants to merge 1 commit intopsf:mainpsf/black:mainfrom
anthonychen000:fix-2097-type-comment-recognitionanthonychen000/black:fix-2097-type-comment-recognitionCopy head branch name to clipboard
Closed

Fix #2097: Recognize type comments with spaces after ##4652
anthonychen000 wants to merge 1 commit intopsf:mainpsf/black:mainfrom
anthonychen000:fix-2097-type-comment-recognitionanthonychen000/black:fix-2097-type-comment-recognitionCopy head branch name to clipboard

Conversation

@anthonychen000
Copy link
Copy Markdown

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 #2097 without
implementing comment standardization. Includes test case and CHANGELOG entry.
Also includes unrelated mypy fix for action/main.py.
@cobaltt7
Copy link
Copy Markdown
Collaborator

I'm closing this PR since #4645 seems more comprehensive. Additionally, this PR seems to have some unrelated changes included - please make sure to only include the relevant changes in the future.

@cobaltt7 cobaltt7 closed this Jul 22, 2025
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

2 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.