Zulip Chat Archive

Stream: mathlib4

Topic: Linting space around variables


Damiano Testa (Mar 10 2025 at 10:37):

In the context of one of the items discussed in #general > Mathlib community meetings, #22760 is an attempt to lint "spacing" issues, such as example (a: Nat).

Damiano Testa (Mar 10 2025 at 10:38):

The linter checks that

  • each command starts at the beginning of a line;
  • the "hypotheses segment" of each declaration coincides with its pretty-printed version.

Damiano Testa (Mar 10 2025 at 10:38):

I was somewhat surprised that mathlib is already compliant and the only issues raised by the linter were in a single test file!

Damiano Testa (Mar 10 2025 at 10:38):

#22766 "fixes" the test file.

Michael Rothgang (Mar 10 2025 at 13:32):

Damiano Testa said:

I was somewhat surprised that mathlib is already compliant and the only issues raised by the linter were in a single test file!

For those following at home: the truth is sadly more complicated; there are actually a number of violations. (But I would love for them to get fixed.)

Damiano Testa (Mar 10 2025 at 13:32):

Yes, the reason that I thought that mathlib was complying was that I had forgotten to import the linter... :man_facepalming:

Damiano Testa (Mar 10 2025 at 13:33):

It looks like a lot of issues come from the fact that the pretty-printer erases comments, so I am hoping that there will be many fewer false positives once I get the linter to erase comments from the source code as well.

Damiano Testa (Mar 10 2025 at 19:21):

What should the linter do with

instance instPartialOrder : PartialOrder (Fin n) := inferInstance
instance instLattice      : Lattice (Fin n)      := inferInstance

It is hard to make the linter aware of the joint formatting.

Damiano Testa (Mar 10 2025 at 19:22):

Should these be wrapped inside a set_option linter.commandStart false region?

Damiano Testa (Mar 10 2025 at 19:22):

Should formatting comply with the linter anyway?

Michael Rothgang (Mar 10 2025 at 20:54):

Damiano Testa said:

Should formatting comply with the linter anyway?

Personal opinion: consistent formatting and not having to argue about style are huge benefits, so I think we should move towards this goal if we can.

In case there are bikeshedding questions, we decide to make compliance optional (until the bikeshed is somewhat resolved).
If we discover the pretty-printer is really bad in some cases, consciously deactivating the linter seems fine for me.

Michael Rothgang (Mar 10 2025 at 20:55):

Damiano Testa said:

What should the linter do with

instance instPartialOrder : PartialOrder (Fin n) := inferInstance
instance instLattice      : Lattice (Fin n)      := inferInstance

It is hard to make the linter aware of the joint formatting.

(In a text-based linter, I was thinking of checking these things: if there is a colon which lines up, treat it as conscious alignment, otherwise format normally.)

I believe that "do not align manually" (because this tends to go stale) is the best trade-off in the long term, especially for a diverse project as ours.

Eric Wieser (Mar 10 2025 at 21:17):

I frequently see column alignment going stale within the evolution of PR, and so the nonstale version never even makes it into mathlib

Ruben Van de Velde (Mar 10 2025 at 22:14):

I would also prefer not allowing the alignment. Especially when you add a new line where the column would be further to the right, you either get an inconsistency or you needlessly mess up the blame for the other lines

Damiano Testa (Mar 11 2025 at 08:53):

I am happy to "lint all the way"! I personally have used "whitespace formatting" in my code, but I agree that it is simpler to have a "local" rule for formatting.

Damiano Testa (Mar 11 2025 at 08:55):

Btw, I'm also testing out a "relative linter", namely one that only lints the code that has been modified, using the git diff as a basis for what counts as "modified".

Damiano Testa (Mar 11 2025 at 08:56):

If that works, it could be useful to avoid the need of nolints for certain linters, since it would flag an exception during the review process (in a workflow step outside the main CI) and then be silent, if the non-lint-compliant code is accepted nevertheless.

Damiano Testa (Mar 11 2025 at 08:57):

This can be useful for this specific linter, where there isn't a full set of rules for what is applied.

Damiano Testa (Mar 11 2025 at 08:57):

So the linter could make suggestions, some of which are "mandatory" others are just optional/up to each contributor's style.

Damiano Testa (Mar 11 2025 at 16:03):

#22838 is another batch of "fixes", including some "de-formatting" of whitespace alignment.

Michael Rothgang (Mar 11 2025 at 17:47):

I just maintainer merged. (That doesn't get an emoji, right? - I don't think this is necessarily wrong, but take this as a testament that the emojibot is great :-).)

Damiano Testa (Mar 11 2025 at 19:27):

That's an interesting idea about emoji reactions for maintainer merges: it would be very easy to add it to the current workflows and makes sense to me.

Feel free to open a PR and/or a discussion about it!

Michael Rothgang (Apr 30 2025 at 10:46):

#24467 fixes some more whitespace issues in mathlib

Michael Rothgang (Apr 30 2025 at 12:43):

#24472 fixes some more


Last updated: May 02 2025 at 03:31 UTC