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: fix tab-space mix with tab indenation and continuation_align_style=SPACE #970

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

Conversation

spaceone
Copy link

@spaceone spaceone commented Oct 7, 2021

Fixes #967

Let's see what the tests say about this?

@google-cla
Copy link

google-cla bot commented Oct 7, 2021

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@spaceone
Copy link
Author

spaceone commented Oct 7, 2021

@googlebot I signed it!

@bwendling
Copy link
Member

I'm sorry to get to this so late. Could you update the CHANGELOG please? Also, please add a test case derived from the original issue.

@spaceone
Copy link
Author

spaceone commented Nov 3, 2021

I'm sorry to get to this so late. Could you update the CHANGELOG please? Also, please add a test case derived from the original issue.

OK, I created a changelog entry.
Is a new test case necessary? As I adjusted a current one, which already tests this.

@bwendling
Copy link
Member

I would prefer the testcase just so we have a concrete example of what we're fixing, so that if something goes wrong we won't mess it up.

@dhouck
Copy link

dhouck commented Nov 26, 2021

(To clarify: Iʼm a member of the general public, not Google)

I think what you want is best solved with just using CONTINUATION_ALIGN_STYLE=FIXED; changing what CONTINUATION_ALIGN_STYLE=SPACE does will break things.

The point of using spaces for alignment is so that something like

def foo_function(arg1, arg2,
                 arg3):
»   return [
»           'hello',
»           'world',
»   ]

looks good (ie. the arg3 right under the arg1 and the strings right after the [) even when viewed with a different tab width. However, as you see in #967, if it canʼt figure out how to line up continuation lines, itʼll just fall back to four spaces, which isnʼt ideal.

Maybe a better simple option would be to have the default CONTINUATION_ALIGN_STYLE be SPACE when USE_TABS is false, and FIXED when itʼs true? It looks like itʼd take a bigger refactor to add a fourth CONTINUATION_ALIGN_STYLE that uses tabs for generic continuations and spaces if it can actually align things, I think?

@spaceone
Copy link
Author

spaceone commented Nov 27, 2021

Since Python3 mixing tabs with spaces is a SyntaxError - so when USE_TABS=True we must make sure that spaces are never used for indentation.

@dhouck
Copy link

dhouck commented Nov 27, 2021

Putting spaces before a tab is an error. Putting spaces after a tab is not, and even if it were this only uses spaces for continuation lines, not technically indentation, where it definitely isnʼt an error. I just tested with my example code and with

def foo2():
»   print("Unconditional")
»   if foo_function(1,2,3):
»       print("Conditional")

which uses tabs and then spaces for indentation, not just the alignment yapf generates, and both work in python 3.9.7.

And if it were an error, then this PR wouldnʼt help, because not all indentations are a multiple of tab_width; youʼd need one of the other two (already existing) options for CONTINUATION_ALIGN_STYLE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

use_tabs=True causes tabs-space mix
3 participants