public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Simon Marchi <simark@simark.ca>
To: Tom Tromey <tromey@adacore.com>, gdb-patches@sourceware.org
Subject: Re: [PATCH] Add flake8 and isort to .pre-commit-config.yaml
Date: Tue, 2 Apr 2024 16:26:15 -0400	[thread overview]
Message-ID: <d2e827c8-a999-4f87-a30d-c62bf097f75a@simark.ca> (raw)
In-Reply-To: <20240402181121.1948535-1-tromey@adacore.com>



On 2024-04-02 14:11, Tom Tromey wrote:
> This adds flake8 and isort to .pre-commit-config.yaml.  This way, they
> will automatically be run on commit.
> 
> I chose the most recent available versions after verifying that they
> don't cause any reports or changes in the current tree.
> 
> Internally at AdaCore, we also use a few flake8 plugins as well, so
> perhaps that's another avenue for investigation.
> ---
>  .pre-commit-config.yaml | 11 +++++++++++
>  1 file changed, 11 insertions(+)
> 
> diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
> index 7afe60c20be..bca8acc5d02 100644
> --- a/.pre-commit-config.yaml
> +++ b/.pre-commit-config.yaml
> @@ -6,3 +6,14 @@ repos:
>      hooks:
>        - id: black
>          files: 'gdb/.*'
> +  - repo:  https://github.com/pycqa/flake8
> +    rev: 7.0.0
> +    hooks:
> +    - id: flake8
> +      files: gdb/python/.*\.py$
> +      args: [--config, gdb/setup.cfg]
> +  - repo: https://github.com/pycqa/isort
> +    rev: 5.13.2
> +    hooks:
> +    - id: isort
> +      files: gdb/.*\.py$

I only used `gdb/.*.py` for the black hook, because pre-commit does its
own filtering already.  The hooks specify the `python` type, so
pre-commit only runs the hook on what it believes is a Python file.  The
only purpose of the regex was to limit the hooks to the gdb
sub-directory.

However, I realized we have at least one file that is python but isn't
named *.py, gdb-gdb.py.in.  I just tried, and the black hook doesn't run
on gdb-gdb.py-in.  If you run black manually, it considers gdb-gdb.py.in
as a Python file (I don't know by which magic) and re-formats it:

    $ pwd
    /home/simark/src/binutils-gdb
    $ black gdb
    reformatted /home/simark/src/binutils-gdb/gdb/gdb-gdb.py.in

But the filtering done by pre-commit doesn't consider gdb-gdb.py.in a
Python file.  The hooks specify `types: 'python'`, which doesn't match
'*.py.in'.  The doc for `types` is here:

    https://pre-commit.com/#filtering-files-with-types

The mapping between extensions and types is here:

    https://github.com/pre-commit/identify/blob/main/identify/extensions.py

If we want to make pre-commit consider that file for our Python hooks,
the only solution I see currently would be to use something like this,
as described in the doc above:

    hooks:
      - id: black
        types_or: [file]
        files: '^gdb/.*\.(py|py.in)$'

It didn't seem to work for me using `types` (an error in gdb-gdb.py.in
wouldn't be caught), but it works using `types_or`.  So, I would
consider something similar for other hooks as well.

Simon

  reply	other threads:[~2024-04-02 20:26 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-02 18:11 Tom Tromey
2024-04-02 20:26 ` Simon Marchi [this message]
2024-04-03 19:52   ` Tom Tromey
2024-04-03 19:57     ` Simon Marchi
2024-04-09  7:58       ` Tom de Vries
2024-04-09  9:41         ` Kévin Le Gouguec
2024-04-09 12:56           ` Tom de Vries
2024-04-09 13:04             ` Kévin Le Gouguec
2024-04-09 15:22         ` Tom Tromey
2024-04-10  5:38           ` Tom de Vries

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d2e827c8-a999-4f87-a30d-c62bf097f75a@simark.ca \
    --to=simark@simark.ca \
    --cc=gdb-patches@sourceware.org \
    --cc=tromey@adacore.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).