Skip to content

Emphasize more strongly that contributors should use pre-commit hooks #2014

Closed

Description

@MarkusPiotrowski

While Biopython is enforcing many/most coding-style conventions described in PEP-8 and PEP-257, many pull requests get delayed, because contributors (often unintentionally) violate these style conventions.

A discussion about a Biopython specific pre-commit git hook to enforce PEP-8 style has already started some time ago (e.g. #493, #1188).

Using a pre-commit hook in the git environment is an easy way to make contributors aware of style-violations and give them the chance to address these before submitting a pull request.

However, it seems that many contributors are not using pre-commit hooks, and one reason for this may be that they are not aware of them and/because we are not explicit enough about that they should be used.

There are some places, where we could emphasize this more strongly:

Activity

  1. peterjc commented on Apr 18, 2019

    @peterjc
    Member

    This is a very good idea in principle, but the complexities of our different .flake8 settings in different folders is an annoyance requiring the bespoke git pre-commit script.

    I would very much like to address that first, which bring the enormous advantage of letting us use an off-the shelf widely used hook, e.g. that provided by flake8 itself:

    $ flake8 --install-hook git
    $ git config --bool flake8.strict true
    

    Currently (leaving aside optional extensions we don't currently require):

    https://github.com/biopython/biopython/blob/biopython-173/BioSQL/.flake8 - quite strict

    • ignores W504,D101,D102,D103,D105,

    https://github.com/biopython/biopython/blob/biopython-173/Doc/examples/.flake8 - quite strict

    • ignores D100,D101,D102,D103

    https://github.com/biopython/biopython/blob/biopython-173/Bio/.flake8 - fairly strict

    • ignores E501,E731,F401,F841,D100,D101,D102,D103,D105,D203,

    https://github.com/biopython/biopython/blob/biopython-173/Tests/.flake8 - least strict

    • ignores E402,E501,E731,F401,F405,F841,D100,D101,D102,D103,D105,D200,D202,D203,D204, D300,D400,D401,D412

    This looks doable (and is part of my motivation with adopting the black formatting tool, #2008, which would take care of some of these). [Update 22 April: Actually black does not help much, other than E241, which autopep8 can do, we've already fixed all the flake8 style issues black would have handled]

    Basically we need to fix a few things in the tests and/or relax the other tests a little.

  2. peterjc commented on Apr 18, 2019

    @peterjc
    Member

    I'm looking at D300 right now...

  3. peterjc commented on Apr 18, 2019

    @peterjc
    Member

    Pull requests opened to fix D300, D200 and D204 in Tests - a bit more effort on this and we can have a single .flake8 configuration, and use the default flake8 git pre-commit hook - which will be simpler to document and maintain 👍

  4. peterjc commented on Apr 19, 2019

    @peterjc
    Member

    I propose to solve E731 the pragmatic way - whitelist most of them with #noqa lines, see #2019.

    I can fix D100 mostly automatically with the following script (missing docstrings can trigger false edits, and it leaves question marks and exclamation marks to a human to edit), will do this once the current pull requests are merged:

    import sys
    
    
    def fix(lines):
        answer = []
        post_def = False
        for line in lines:
            if line.lstrip().startswith("def "):
                # print(line.rstrip())
                post_def = True
            if post_def and line.lstrip().startswith('"""'):
                post_def = False
                # Looks like line one of a docstring
                if line.rstrip().endswith('"""'):
                    # a one-liner
                    if not line.rstrip().endswith(('."""', '?"""', '!"""')):
                        print(line.rstrip())
                        line = line.rstrip()[:-3] + '."""\n'
                else:
                    # multi-liner
                    if not line.rstrip().endswith(('.', '?', '!')):
                        print(line.rstrip())
                        line = line.rstrip() + ".\n"
            answer.append(line)
        return answer
    
    
    for f in sys.argv[1:]:
        print(f)
        with open(f) as handle:
            old = list(handle)
        new = fix(old)
        with open(f, "w") as handle:
            for line in new:
                handle.write(line)
    print("Done")

    Next would be D401 First line should be in imperative mood and there are now few enough of these to do my hand later (again, waiting on the open pull requests as I already fixed some).

  5. MarkusPiotrowski commented on Apr 19, 2019

    @MarkusPiotrowski
    ContributorAuthor

    @peterjc Do you know that newer flake8 versions (since 3.7) have a per-file-ignores option? This allows to set up only one .flake8 file and still have folder specific ignores, e.g.:

    [flake8]
    doctests = True
    ignore =
        E122,E123,E126,W503,W504,
        E501,E731,F401,F841,
        D101,D102,D103,D105,
        D203,
        B007,B009,B010,B011,
        C812,C815,
        BLK100,
    per-file-ignores =
        /Bio/*:W303
    

    This example doesn't make too much sense, I just used it for testing: I put too much empty lines in one file in Bio and one file in Tests, and indeed, only the file from the Tests folder was flagged.

  6. peterjc commented on Apr 19, 2019

    @peterjc
    Member

    @MarkusPiotrowski I did not know that - if as in your example it works nicely with folder level settings, that would do nicely (not that fixing the issues in Tests/ isn't a good idea anyway).

    Once we've merged #2019 and #2020, do you want to try a single strict top-level .flake8 instead of #2018 - taking care of E731 etc as extra ignores for Bio/, and D202 etc as extra ignores on the Tests/ folder?

  7. MarkusPiotrowski commented on Apr 19, 2019

    @MarkusPiotrowski
    ContributorAuthor

    I found this just today. Didn't want that you spend your whole Easter Holidays with these issues...
    Yes, I would like to give it a try. Do you need to update flake8?

  8. peterjc commented on Apr 19, 2019

    @peterjc
    Member

    One of the interesting things about my employer is we don't get public holidays (although the institute does close over Christmas into New Year), so if we take them off they come out of (reasonably generous) annual leave. I'll be back at work on Monday.

    --

    By all means, please have a play with the combined settings - I'm not planning on doing any more flake8 work today or the weekend (aside perhaps from merging pull requests).

  9. peterjc commented on Apr 22, 2019

    @peterjc
    Member

    I think we're now very close to having Bio/ and Tests/ having the same .flake8 settings, with BioSQL/ and Doc/examples/ even stricter. So will be soon be able to use a single top level .flake8 file.

    The quick solution will be to adopt the Bio/.flake8 as the sole top level settings - but ideally using Markus' idea lets us have the stricter settings with additional ignores under Bio/ and Tests/ as needed 👍

  10. MarkusPiotrowski commented on Apr 22, 2019

    @MarkusPiotrowski
    ContributorAuthor

    I'm actually working on style issues in Scripts.
    I already have made a 'top-level' .flake8 and I'm waiting now for some of the ongoing style-related PR's to be merged to test it.

  11. peterjc commented on Apr 22, 2019

    @peterjc
    Member

    This time round, let's put each ignore code on its own line (sensibly sorted) as it will make tracking changes much easier?

  12. MarkusPiotrowski commented on Apr 22, 2019

    @MarkusPiotrowski
    ContributorAuthor

    Unfortunately, it seems that the per-file-ignores must be all on the same line per file pattern. I tried to put them one on each line with following comments, but that didn't work.

  13. MarkusPiotrowski commented on Apr 22, 2019

    @MarkusPiotrowski
    ContributorAuthor

    With this .flake8 file (not up-to-date regarding the running PRs):

    [flake8]
    doctests = True
    ignore =
        # =============================================================
        # Biopython's 'standard' ignores we can agree to always accept:
        # =============================================================
        E501, # line too long TODO: Maybe we find a sensible limit, e.g. 88 (black) and enforce it
        W503, # line-break before binary operator, deliberately ignore in favour of W504
        D203, # 1 blank line required before class docstring
              # We ignore D203 deliberately in favour of passing D211 No blank lines allowed before
              # class docstring
        # ===========================================
        # Ignores that we have to accept for a while:
        # ===========================================
        E123, # closing bracket does not match indentation of opening bracket's line (top, Bio, Tests, 132 occurrences)
        W504, # line break after binary operator TODO?
        B007, # Loop control variable not used within the loop body.
              # If this is intended, start the name with an underscore.
        #==========================================
        # Optional ignores for local installations:
        # =========================================
        BLK100, # Black would make changes, only on local installations (so far)
    per-file-ignores =
        Bio/*:E122,E126,E731,F401,F841,D101,D102,D103,D105,B009,B010,B011,C812,C815
        Tests/*:E122,E126,E241,E401,E731,F401,F405,F841,D100,D101,D102,D103,D202,D400,D401,D412,B009,B010,B011,C812
    
    # This format is not allowed:
    # per-file-ignores =
    #    /Bio/*:E122, # continuation line missing indentation or outdented TODO (271 occurrences)
    #           E126, # continuation line over-indented for hanging indent TODO  (53 occurrences)
    #           E731, # do not assign a lambda expression, use a def TODO (28 occurrences)
    #           F401, # module imported but unused TODO (108 occurrences)
    #           F841, # local variable is assigned to but never used TODO (55 occurrences)
    #           D101, # missing docstring in public class TODO (43 occurrences)
    #           D102, # missing docstring in public method TODO (81 occurrences)
    #           D103, # missing docstring in public function TODO (17 occurrences)
    #           D105, # missing docstring magic method TODO (116 occurrences)
    #           B009, # do not call getattr with a constant attribute value,
    #                 # it is not any safer than normal property access
    #           B010, # do not call setattr with a constant attribute value,
    #                 # it is not any safer than normal property access
    #           B011, # Do not call assert False since python -O removes these calls;
    #                 # instead callers should raise AssertionError().
    #           C812, # missing trailing comma
    #          C815  # missing trailing comma in Python 3.5+
    #    /Tests/*:E122, # continuation line missing indentation or outdented TODO (48 occurrences)
    #             E126, # continuation line over-indented for hanging indent TODO (41 occurrences)
    #             E241, # multiple spaces after ',' TODO (54 occurrences)
    #             E401, # module level import not at top of file TODO? (and noqa?) (49 occurrences)
    #             E731, # do not assign a lambda expression, use a def, we want deliberately ignore this in tests
    #             F401, # module imported but unused TODO (76 occurrences)
    #             F405, # name may be undefined, or defined from star imports: module TODO or noqa?,only 21 in tests
    #             F841, # local variable is assigned to but never used TODO? (68 occurrences)
    #             D100, # missing docstring in public module, TODO (75 occurrences)
    #             D101, # missing docstring in public class (220 occurrences)
    #             D102, # missing docstring in public method (976 occurrences)
    #             D103, # missing docstring in public functions TODO (50 occurrences)
    #             D202, # no blank lines allowed after function docstring (88 occurrences)
    #             D400, # first line should end with a period (742 occurrences)
    #             D401, # first line should be in imperative mood TODO(31 occurrences)
    #             D412, # no blank lines allowed between a section header and its content TODO(1 occurrence)
    #             B009, # do not call getattr with a constant attribute value,
    #                   # it is not any safer than normal property access
    #             B010, # do not call setattr with a constant attribute value,
    #                   # it is not any safer than normal property access
    #             B011, # Do not call assert False since python -O removes these calls;
    #                   # instead callers should raise AssertionError()
    #             C812  # missing trailing comma
    

    we get the following result:

    G:\git_32bit\biopython>flake8 --statistics --quiet
    .\setup.py
    .\Doc\examples\ACT_example.py
    .\Doc\examples\fasta_iterator.py
    .\Doc\examples\fasta_dictionary.py
    .\Doc\examples\getgene.py
    .\Scripts\query_pubmed.py
    .\Scripts\scop_pdb.py
    .\Scripts\debug\debug_blast_parser.py
    .\Scripts\GenBank\check_output.py
    .\Scripts\Restriction\ranacompiler.py
    .\Scripts\Restriction\rebase_update.py
    .\Scripts\xbbtools\nextorf.py
    .\Scripts\xbbtools\xbb_blast.py
    .\Scripts\xbbtools\xbb_blastbg.py
    .\Scripts\xbbtools\xbb_help.py
    .\Scripts\xbbtools\xbb_search.py
    .\Scripts\xbbtools\xbb_translations.py
    .\Scripts\xbbtools\xbb_utils.py
    .\Scripts\xbbtools\xbb_widget.py
    .\Tests\test_NaiveBayes.py
    .\Tests\test_seq.py
    .\Tests\test_PDB_ResidueDepth.py
    .\Tests\test_ExPASy.py
    .\Tests\test_KGML_graphics_online.py
    .\Tests\test_SeqIO_online.py
    .\Tests\test_PDBList.py
    .\Tests\test_TogoWS.py
    .\Tests\test_phyml_tool.py
    .\Tests\test_Uniprot.py
    .\Tests\test_psw.py
    .\Tests\test_SCOP_online.py
    .\Tests\requires_wise.py
    .\Tests\test_Wise.py
    .\Tests\test_ClustalOmega_tool.py
    .\Tests\test_Entrez_parser.py
    .\Tests\test_HMMCasino.py
    1     D100 Missing docstring in public module
    18    D101 Missing docstring in public class
    95    D102 Missing docstring in public method
    14    D103 Missing docstring in public function
    2     D105 Missing docstring in magic method
    15    D107 Missing docstring in __init__
    1     E126 continuation line over-indented for hanging indent
    49    E402 module level import not at top of file
    

    This does (of course) not include the running PRs, e.g. all the issues in /Scripts and /Tests are probably already solved and there may be only some single files that need further attention.

  14. peterjc commented on Apr 23, 2019

    @peterjc
    Member

    Do you know if this style would work? If so could opt for even more path repetition to get one code per line?

    per-file-ignores =
        Bio/*:E122,E126,E731,
        Bio/*:F401,F841,
        Bio/*:D101,D102,D103,D105,
        Bio/*:B009,B010,B011,
        Bio/*:C812,C815
        Tests/*:E122,E126,E241,E401,E731,
        Tests/*:F401,F405,F841,
        Tests/*:D100,D101,D102,D103,D202,D400,D401,D412,
        Tests/*:B009,B010,B011,C812
    

    (If you don't known, I might try this later today)

  15. 13 remaining items

  16. peterjc commented on May 16, 2019

    @peterjc
    Member

    Added 21 Nov 2018, I think I'd missed that.

    I agree that moving to pre-commit would allow including the misc checks currently done on TravisCI via the Tox configuration too - so that would be a practical improvement.

    I suspect we could even use our current multiple .flake8 file setup with pre-commit, but I would rather move to a single top level .flake8 instead.

  17. peterjc commented on May 23, 2019

    @peterjc
    Member

    With #2074 merged, @MarkusPiotrowski are you happy for us to recommend the flake8 pre-commit hook in the CONTRIBUTING.rst file (etc)?

  18. peterjc commented on May 24, 2019

    @peterjc
    Member

    Good progress, I've closed #493 as of #2075.

    Still need to mention the hook on https://biopython.org/wiki/GitUsage

    Also consider if and how to mention it in the PULL_REQUEST_TEMPLATE.md too.

  19. peterjc commented on May 24, 2019

    @peterjc
    Member

    How about this:

    - [ ] 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.
    

    The wording aims to cover the fact that TravisCI/Tox includes some extra style checks not currently done via flake8 (e.g. the RST linting)

  20. MarkusPiotrowski commented on May 24, 2019

    @MarkusPiotrowski
    ContributorAuthor

    Fine!

  21. peterjc commented on May 24, 2019

    @peterjc
    Member

    Change applied - are we OK to close this issue?

  22. MarkusPiotrowski commented on May 24, 2019

    @MarkusPiotrowski
    ContributorAuthor

    Yes, I'm doing so now. Thank you!

  23. peterjc commented on May 24, 2019

    @peterjc
    Member

    Excellent work, thank you too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions