Skip to content

Apply black style formatting to GenomeDiagram - #2079

Closed
peterjc wants to merge 4 commits into
biopython:masterfrom
peterjc:black_gd
Closed

peterjc wants to merge 4 commits into
biopython:masterfrom
peterjc:black_gd

Conversation

@peterjc

@peterjc peterjc commented May 24, 2019

Copy link
Copy Markdown
Member

This pull request is a test case for issue #2008, chosen since the GenomeDiagram author @widdowquinn is a fan of the black tool and it's output, and we historically have had some variation in style between modules.

Note that I reverted two black edits (white space with slicing) due to a known disagreement between black and flake8 over the PEP8 ruling (looks like a flake8 false positive).

I also manually reviewed and fixed some multi-line comments (previously vertically aligned with spaces), in general making them into one-line comments.

  • I hereby agree to dual licence this and any previous contributions under both
    the Biopython License Agreement AND the BSD 3-Clause License.

  • I have read the CONTRIBUTING.rst file, have run flake8 locally, and
    understand that AppVeyor and TravisCI will be used to confirm the Biopython unit
    tests and style checks pass with these changes.

  • I have added my name to the alphabetical contributors listings in the files
    NEWS.rst and CONTRIB.rst as part of this pull request, am listed
    already, or do not wish to be listed. (This acknowledgement is optional.)

Black and others added 3 commits May 24, 2019 15:44
This is a known false positive in flake8, if and when we
adopt black for all of Biopython we would adopt the black
convention here (and ignore E203 if this problem persists)
@peterjc peterjc added the Style label May 24, 2019
@widdowquinn

Copy link
Copy Markdown
Contributor

One of the things I love about black is that it makes my code look like it's written by a better coder than me.

@peterjc

peterjc commented May 24, 2019

Copy link
Copy Markdown
Member Author

I suppose by discarding any idiosyncratic personal style quirks (in line breaking etc), it makes the code more standard, and thus leaves a better impression? Or maybe you just like the black style more than your style from ten years back ;)

@widdowquinn

Copy link
Copy Markdown
Contributor

😂I like the black style more than my style, now!

@codecov

codecov Bot commented May 24, 2019 •

Copy link
Copy Markdown

Codecov Report

Merging #2079 into master will increase coverage by 1.3%.
The diff coverage is 88.76%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master    #2079     +/-   ##
=========================================
+ Coverage   83.56%   84.87%   +1.3%     
=========================================
  Files         317      318      +1     
  Lines       50834    50884     +50     
=========================================
+ Hits        42480    43187    +707     
+ Misses       8354     7697    -657
Impacted Files Coverage Δ
Bio/Graphics/GenomeDiagram/_Feature.py 91.66% <100%> (ø) ⬆️
Bio/Graphics/GenomeDiagram/_Diagram.py 96.72% <100%> (ø) ⬆️
Bio/Graphics/GenomeDiagram/_CrossLink.py 68.42% <100%> (ø) ⬆️
Bio/Graphics/GenomeDiagram/__init__.py 100% <100%> (ø) ⬆️
Bio/Graphics/GenomeDiagram/_Colors.py 66.66% <14.28%> (ø) ⬆️
Bio/Graphics/GenomeDiagram/_FeatureSet.py 67.64% <43.75%> (ø) ⬆️
Bio/Graphics/GenomeDiagram/_Graph.py 61.03% <76.92%> (ø) ⬆️
Bio/Graphics/GenomeDiagram/_Track.py 81.72% <86.66%> (ø) ⬆️
Bio/Graphics/GenomeDiagram/_CircularDrawer.py 88.95% <87.58%> (ø) ⬆️
Bio/Graphics/GenomeDiagram/_GraphSet.py 60% <88.88%> (ø) ⬆️
... and 32 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 405983f...1912100. Read the comment docs.

@MarkusPiotrowski

Copy link
Copy Markdown
Contributor

Wow, this looks even worse than I have feared... Even if it's not breaking PEP8 literally, I have the feeling it's breaking the spirit of PEP8. It looks so un-pythonic.
Will it really be easier to work with method headers that fill up the whole screen? You will need much more scrolling when working on the code, I guess it will be harder to keep an overview?

In this examples, you probably want to keep the x,y values in the point lists on one line?
Indeed, it takes away much of the freedom to sort/arange things in a meaningful manner.

@peterjc

peterjc commented May 27, 2019 •

Copy link
Copy Markdown
Member Author

I disagree with that it's breaking the spirit of PEP8 or that it is un-pythonic - but it is different and seems to generate polarising views.

Quoting a specific example from Bio/Graphics/GenomeDiagram/_AbstractDrawer.py, before:

    def __init__(self, parent, pagesize='A3', orientation='landscape',
                 x=0.05, y=0.05, xl=None, xr=None, yt=None, yb=None,
                 start=None, end=None, tracklines=0, cross_track_links=None):

after black:

    def __init__(
        self,
        parent,
        pagesize="A3",
        orientation="landscape",
        x=0.05,
        y=0.05,
        xl=None,
        xr=None,
        yt=None,
        yb=None,
        start=None,
        end=None,
        tracklines=0,
        cross_track_links=None,
    ):

The multi-line layout does make it hard to fit on a screen - as you say more scrolling. I would even argue this is a good thing, as functions/classes shouldn't have this many arguments (sorry Leighton!). Adopting black will discourage this in new code.

In hindsight, given there are lots of (x, y) pairs in GenomeDiagram, perhaps passing these points a 2-tuples would have been neater for the API (but we're stuck with this now for backward compatibility).

As an aside, there is probably a flake8 plugin for this kind of complexity check?

(Update: pylint has R0902 "Too many instance attributes" and R0913 "Too many arguments", not sure if there is a flake8 ecosystem equivalent)

As you say, "it takes away much of the freedom to sort/arrange things in a meaningful manner". I highlighted the example or horizontal placement of some multi-line comments in this pull request - do you have another example in GenomeDiagram?

@widdowquinn

Copy link
Copy Markdown
Contributor

"I would even argue this is a good thing, as functions/classes shouldn't have this many arguments (sorry Leighton!). Adopting black will discourage this in new code."

No apology necessary :D It's bad design and I should have used something struct-like, instead.

One of the things I like about black is that where I think it makes my code look unwieldy or ugly, what it's often telling me is that I've made a poor design choice.

@MarkusPiotrowski

MarkusPiotrowski commented May 27, 2019 •

Copy link
Copy Markdown
Contributor

When reading PEP8 and looking at all the examples in the Python documentation, there is a clear preference for using line continuation using parentheses (brackets, etc.), together with an indentation to the parentheses level.
On the other hand, there is no indication that argument lists should preferentially be split to have one argument per line (again, most what I have seen from official Python documents are multiple arguments per line). This is what I mean when saying "the spirit of PEP8". Sorry for starting this argument, this should be discussed (and has been or is being discussed) elsewhere.

One example of "taking away the freedom to sort things in a meaningful way":

# before black
def draw_box(point1, point2,
             color=colors.lightgreen, border=None, colour=None,
             **kwargs):

Here the author has obviously organized the arguments in a meaningful manner; the two necessary and important arguments on the first line, optional formatting parameters on the second, optional other parameters last.
This is easier/faster to read and understand (for me) than the black version:

def draw_box(
    point1, point2, color=colors.lightgreen, border=None, colour=None, **kwargs
):

I don't write much new code, here in Biopython I'm mostly working on existing code. Often, I don't have a clue what a class/method/function is doing exactly after I read the code for the first time, so I need to become acquainted with the code. And this is easier when seeing more of the code at the same time, without too much scrolling up and down. Also, a meaningful and visible sorting of arguments will help.
I fear that black will make the reading of code more difficult (Our newspaper still publishes Charles M Schulz comics. In the last weeks they published a story where Snoopy was reading Tolstoy's War and Peace, but only one word each day. Isn't this strange?).

@peterjc

peterjc commented May 27, 2019

Copy link
Copy Markdown
Member Author

The draw_box example is a good one.

Black will squeeze things down to one line if it fits (as here). It will respect comments though, so black is happy with this:

def draw_box(
    point1,
    point2,
    # Optional color arguments,
    color=colors.lightgreen,
    border=None,
    colour=None,  # historical UK spelling
    **kwargs  # Accept arbitrary arguments to pass to ReportLab Polygon
):

i.e. If there is a real reason for the original grouping (beyond cosmetic line breaking), or other things, you can make it explicit. Granted this is still much longer than the tree line original.

Personally I am OK with the black one line version here. The meaning is still there in the Python mandatory vs optional arguments, but accept it is less visually emphasised.

@widdowquinn

widdowquinn commented May 27, 2019 •

Copy link
Copy Markdown
Contributor

"Here the author has obviously organized the arguments in a meaningful manner; the two necessary and important arguments on the first line, optional formatting parameters on the second, optional other parameters last."

As that original author, my view is that the black formatting is fine, and it highlights that I should have used a struct-like object to group formatting parameters to make the code more readable, rather than the cosmetic linebreaks ;)

I appreciate that there are diverse, strong and heartfelt opinions about code style snd formatting, and preferences for what is most readable. I guess this code sat in the "not brilliantly written, but forced to be readable" category, and after black it sits in the "this code now looks as ugly as it actually is" category. I see why that could be taken as not being an improvement.

@MarkusPiotrowski

Copy link
Copy Markdown
Contributor

I found it interesting, that without any knowledge of the GenomeDiagram code, I immediately was able to understand that there was an intention in sorting these three "categories" of parameters. Really, I didn't search for this example, it sprang to my eye (that's likely not an English idiom, but you understand what I mean) when I went through the diffs.

@peterjc

peterjc commented Jun 13, 2019

Copy link
Copy Markdown
Member Author

I've applied the minor improvements noticed via the black changes in #2127 (which is why there are now loads of merge conflicts).

I don't mind redoing this branch (it should be as simple as re-running black, and cherry-picking the commit to undo the slice changes which would otherwise trigger flake8 E203), but it seems it would not be universally welcome at this time.

I'm going to leave this open until at least the BOSC 2019 CoFest meeting next month, when some of the Biopython team will be able to have an in person discussion, and perhaps experiment further with black.

@sbliven

sbliven commented Jul 27, 2019

Copy link
Copy Markdown
Contributor

After discussing at CoFest I am in favor of using black.

@peterjc

peterjc commented Sep 2, 2019

Copy link
Copy Markdown
Member Author

Closing in favour of #2251 (done as a new pull request to preserve this version and comments as a medium term record of the discussion).

@peterjc peterjc closed this Sep 2, 2019
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.

4 participants