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

Fix 4399 ASTSafetyError on comment in return type annotation #4444

Closed
wants to merge 4 commits into from

Conversation

dankrzeminski32
Copy link
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
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