yapf icon indicating copy to clipboard operation
yapf copied to clipboard

incorrect format with assert according to pep8

Open akshit-sharma opened this issue 4 years ago • 2 comments

If there is a long statement having assert statement then, yapf is changing into format which is not consistent with pep8 guidelines.

sample.py

def someConditionTrue():
  return True


class Foo():
  def bar(self, longVar2):
    longVar1 = 2
    for _ in range(5):
      if not True:
        assert someConditionTrue(), "The condition if not true will print this {} and {}".format(longVar1, longVar2)

Command being run:

yapf --style='{based_on_style: pep8, indent_width: 2}' sample.py

Output begin generated:

def someConditionTrue():
  return True


class Foo():
  def bar(self, longVar2):
    longVar1 = 2
    for _ in range(5):
      if not True:
        assert someConditionTrue(
        ), "The condition if not true will print this {} and {}".format(
            longVar1, longVar2)

Expected Output:

def someConditionTrue():
  return True


class Foo():
  def bar(self, longVar2):
    longVar1 = 2
    for _ in range(5):
      if not True:
        assert someConditionTrue(), \
               "The condition if not true will print this {} and {}".format(
               longVar1, longVar2)

akshit-sharma avatar May 16 '21 20:05 akshit-sharma

We at YAPF refuse to add or remove tokens from the program. It just leads to bugs and headaches and, worse, could change the semantics of the program.

bwendling avatar May 30 '21 06:05 bwendling

I modified file to

def someConditionTrue():
  return True


class Foo():
  def bar(self, longVar2):
    longVar1 = 2
    for _ in range(5):
      if not True:
        assert someConditionTrue(), \
                "The condition if not true will print this {} and {}".format(longVar1, longVar2)

I ran it with:

yapf --style='{based_on_style: pep8, indent_width: 2, column_limit: 79}' sample.py

Output being generated:

def someConditionTrue():
  return True


class Foo():
  def bar(self, longVar2):
    longVar1 = 2
    for _ in range(5):
      if not True:
        assert someConditionTrue(), \
                "The condition if not true will print this {} and {}".format(longVar1, longVar2)

The last line is not being formatted correctly. Correct me if I am wrong, but I believe, longVar1 should start from next line. Also, shouldn't double quotes start directly below someConditionTrue().

akshit-sharma avatar Jun 02 '21 22:06 akshit-sharma