Skip to content
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

Normalize type comment formatting and handle extra spaces #4542

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

Conversation

iamaparnaojha
Copy link

Description: This PR addresses an issue with the handling of type comments in Black. Previously, type comments with multiple spaces after # were not recognized correctly, and formatting could lead to unnecessary whitespace.

Changes:
Fixed the handling of type comments by normalizing whitespace after # and : (e.g., converting # type: str to # type: str).
The function now trims extra spaces and ensures consistent formatting for type comments.
Added a check for comments starting with type: and cleaned up the comment text accordingly.
Updated the function to format the comment as # type: if no type annotation is present.
Why:
This change improves the detection and formatting of type comments, especially in cases where users accidentally add extra spaces.
It ensures a standardized format for type comments across the codebase, reducing the likelihood of errors in formatting.
Related Issue:
If this PR addresses a specific issue, reference it here by including the issue number (e.g., Fixes #2097 ).

if not comment_text.startswith("type:"):
return False
type_annotation = comment_text[5:].strip()
leaf.value = f"# type: {type_annotation}" if type_annotation else "# type:"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this function should probably stay pure, maybe put the modification part somewhere else

Also, this will need to go in the preview style

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