psf / black

The uncompromising Python code formatter
https://black.readthedocs.io/en/stable/
MIT License
39.15k stars 2.47k forks source link

Inconsistent line breaking and parenthesis with pow and floor-div #1735

Open Cielquan opened 4 years ago

Cielquan commented 4 years ago

Describe the bug I have two lines which are same except for one line using ** and one line using // as operator. When I black the file both lines are broken differently. And for the ** unnecessary parenthesis are added on the mathematical expression on the right side of the assert statement. When I remove them manually afterwards and run black again, they are not added again.

To Reproduce Steps to reproduce the behavior:

  1. Write this to a file:
    def test_this():
    if True:
        assert StrCalc.calculate_string("+{num_l}**+{num_r}".format(num_l=num_l, num_r=num_r)) == +num_l ** +num_r
        assert StrCalc.calculate_string("+{num_l}//+{num_r}".format(num_l=num_l, num_r=num_r)) == +num_l // +num_r
  2. Run black on it with line-length set to 88
  3. See bugs:
    def test_this():
    if True:
        assert StrCalc.calculate_string(
            "+{num_l}**+{num_r}".format(num_l=num_l, num_r=num_r)
        ) == +(num_l ** +num_r)
        assert (
            StrCalc.calculate_string(
                "+{num_l}//+{num_r}".format(num_l=num_l, num_r=num_r)
            )
            == +num_l // +num_r
        )

Expected behavior A clear and concise description of what you expected to happen. I expected both lines to be broken the same and the unnecessary parenthesis are not added.

Environment (please complete the following information):

Does this bug also happen on master? I used the online formatter and yes.

JelleZijlstra commented 4 years ago

This presumably has something to do with the relative precedences of unary +, //, and ** (https://docs.python.org/3/reference/expressions.html#operator-precedence).

JelleZijlstra commented 2 years ago

Still formats the same on current main.