grantjenks / blue

The slightly less uncompromising Python code formatter.
https://blue.readthedocs.io/
Other
393 stars 21 forks source link

Formatting around "and" #40

Open grantjenks opened 3 years ago

grantjenks commented 3 years ago

I'm not thrilled with this diff:

     def __eq__(self, that):
         if not isinstance(that, type(self)):
             return NotImplemented
-        return (self.__slots__ == that.__slots__
-                and all(item == iota for item, iota in zip(self, that)))
+        return self.__slots__ == that.__slots__ and all(
+            item == iota for item, iota in zip(self, that)
+        )

     def __repr__(self):
grantjenks commented 3 years ago

I think the ideal here is:

     def __eq__(self, that):
         if not isinstance(that, type(self)):
             return NotImplemented
         return (
            self.__slots__ == that.__slots__
            and all(item == iota for item, iota in zip(self, that))
        )
wbolster commented 3 years ago

oh hai,

imo, the ‘ideal’ is far from ideal.

that said, here's a quick attempt that would be closer to ‘ideal’, and lo and behold, it doesn't suffer from perceived formatting ‘problems’ either:

class C:
    def __eq__(self, other):
        if not isinstance(other, type(self)):
            return NotImplemented

        if self.__slots__ != other.__slots__:
            return False

        for x in self.__slots__:
            if getattr(self, x) != getattr(other, x):
                return False

        return True

the ‘ideal’ solution would be to not write any code at all, and use attrs instead to declaratively define attributes and whether they should be included in an an equality comparison.

merwok commented 2 years ago

This ticket is not a discussion of that __eq__ method, but of the formatting of specific lines by the black tool.

wbolster commented 2 years ago

which is way more subtle and complex than this ticket seems to imply; see https://github.com/psf/black/issues/2156#issuecomment-1027175601