Skip to content

Fix 4399 ASTSafetyError on comment in return type annotation#4444

Closed
dankrzeminski32 wants to merge 4 commits into
psf:mainfrom
dankrzeminski32:fix-4399
Closed

Fix 4399 ASTSafetyError on comment in return type annotation#4444
dankrzeminski32 wants to merge 4 commits into
psf:mainfrom
dankrzeminski32:fix-4399

Conversation

@dankrzeminski32
Copy link
Copy Markdown
Contributor

@dankrzeminski32 dankrzeminski32 commented Sep 3, 2024

Description

Resolves #4399

Added an extra check before adding commas to the elements in the body of a bracket split.

The check ensures that there is more than just one non-comment item in the list of return elements. It only performs this check for a bracket split in return annotations as it's non-problematic for parameter annotations.

The extra comment was causing the ASTSafetyError (it led to the return type being interpreted as a tuple).

Checklist - did you ...

  • Add an entry in CHANGES.md if necessary?
  • Add / update tests if necessary?
  • Add new / update outdated documentation?

Copy link
Copy Markdown
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

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

Thank you for the PR!

I have a slightly different fix in mind. We should try and find a leaf that actually has the appropriate parent set. This will fix a similar crash in another case, see:

def foo() -> (
    # comment inside parenthesised new union return type
    int | str | bytes
):
    ...

I opened a PR with what I had in mind at #4453. I'd be grateful if you had time to take a look and see if it makes sense to you!

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.

Black fails with ASTSafetyError on comment line in return type annotation

2 participants