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

Long string in return annotation does not get split if there's parameters #3926

Open
jakkdl opened this issue Oct 6, 2023 · 3 comments
Open
Labels
F: linetoolong Black makes our lines too long T: bug Something isn't working

Comments

@jakkdl
Copy link
Contributor

jakkdl commented Oct 6, 2023

Describe the bug
This was first reported in #2699, and thought resolved in #2990, except that only solved the specific case of the function having no parameters.

To Reproduce
Run the test return_annotation_brackets_string:

# Long string example
def frobnicate() -> "ThisIsTrulyUnreasonablyExtremelyLongClassName | list[ThisIsTrulyUnreasonablyExtremelyLongClassName]":
pass
# splitting the string breaks if there's any parameters
def frobnicate(a) -> "ThisIsTrulyUnreasonablyExtremelyLongClassName | list[ThisIsTrulyUnreasonablyExtremelyLongClassName]":
pass
# output
# Long string example
def frobnicate() -> (
"ThisIsTrulyUnreasonablyExtremelyLongClassName |"
" list[ThisIsTrulyUnreasonablyExtremelyLongClassName]"
):
pass
# splitting the string breaks if there's any parameters
def frobnicate(
a,
) -> "ThisIsTrulyUnreasonablyExtremelyLongClassName | list[ThisIsTrulyUnreasonablyExtremelyLongClassName]":
pass

The second function should be formatted exactly the same as the first one.

@jakkdl jakkdl added the T: bug Something isn't working label Oct 6, 2023
@JelleZijlstra JelleZijlstra added the F: linetoolong Black makes our lines too long label Oct 6, 2023
@GOWTHAM2K1
Copy link

hey i am new to open source contribution coming from hacktoberfest , can you help me/assign me this issue so that i could gain some experience

@jakkdl
Copy link
Contributor Author

jakkdl commented Oct 7, 2023

Learning to navigate the different transforms made to the code and what got changed were was a bit tricky for me, but with enough breakpoint debugging and added prints I managed to get a decent handle on it . For this issue you've got the StringSplitter class in src/black/trans.py which is called from transform_line in src/black/linegen.py, which is otherwise responsible for splitting standalone strings. And then there's left_hand_split also in linegen.py which is responsible for splitting function bodies, and in #3916 I added should_split_funcdef_with_rhs to improve return type parsing.
One idea I had would be to expand the logic in StringSplitter to not just trigger on bare strings, but also the case where it's the return value (i.e. following an RARROW). There might definitely be better ways of going about it though.

I'd suggest you just get started on trying to modify shit and see if you manage to make any type of progress, and if so just open a PR where reviewers can give more concrete feedback if your solution has any problems. I'm not going to work on this and suspect it's relatively low priority, so you probably don't need to worry about duplicating work or anything like that.

Make sure you read https://github.com/psf/black#contributing and that also has a link to the Python discord which among other things has a #black channel, you can maybe find more help there if needed. You can also read other PRs that have been merged recently and try to understand them.

Good luck!

@GOWTHAM2K1
Copy link

GOWTHAM2K1 commented Oct 7, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F: linetoolong Black makes our lines too long T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants