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

Enforce alignment of and/or in if statements #527

Closed
kaste opened this issue Sep 21, 2018 · 2 comments
Closed

Enforce alignment of and/or in if statements #527

kaste opened this issue Sep 21, 2018 · 2 comments

Comments

@kaste
Copy link

kaste commented Sep 21, 2018

Operating system: Windows 10
Python version: 3.6
Black version: 18.6b4

Thanks for the great tool. I had the following today:

IN

                if (
                    virtual_view.select_line(lines - 1).strip() == '' and
                    (lines < 2 or virtual_view.select_line(lines - 2).strip() != '')
                ):
                    continue

OUT

                if virtual_view.select_line(lines - 1).strip() == '' and (
                    lines < 2
                    or virtual_view.select_line(lines - 2).strip() != ''
                ):
                    ...

That seems an odd decision. I would rather like to see black doing this

                if (
                    virtual_view.select_line(lines - 1).strip() == ''
                    and (
                        lines < 2 
                        or virtual_view.select_line(lines - 2).strip() != ''
                    )
                ):
                    ...

so that the logical operators get visually promoted.

@kaste
Copy link
Author

kaste commented Sep 21, 2018

I also see this

-        if (not original_method or not inspect.isclass(self.mocked_obj) and
-                inspect.ismethod(original_method)):
+        if (
+            not original_method
+            or not inspect.isclass(self.mocked_obj)
+            and inspect.ismethod(original_method)
+        ):

which is expected, but then

-        elif (inspect.isclass(self.mocked_obj) and
-                inspect.isclass(original_method)):
+        elif inspect.isclass(self.mocked_obj) and inspect.isclass(
+            original_method
+        ):

🤷‍♂️

@ambv
Copy link
Collaborator

ambv commented Sep 25, 2018

Black only keeps extra parentheses when there is more than one operator involved. For single operators, additional parentheses are just visual noise.

What Black is not seeing is a form of symmetry that you do perceive. The simple rule to only keep optional parentheses on more than one operator (of the same priority) works predictably for all cases. Trying to introduce "symmetry" would be bound to fail here and there and would slow things significantly down.

Thanks for your thoughtful report but I'm afraid we can't do much about this unless there is some breakthrough in how to inform Black of symmetry in expressions without killing performance.

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

No branches or pull requests

2 participants