-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
style: clang-format in pre-commit #3683
Conversation
@henryiii I have a readability check that's been waiting on this clang-format. Let me add in it as well. |
std::is_integral<T>::value ? detail::log2(sizeof(T))*2 + std::is_unsigned<T>::value : 8 + ( | ||
std::is_same<T, double>::value ? 1 : std::is_same<T, long double>::value ? 2 : 0)); | ||
static constexpr int index | ||
= std::is_same<T, bool>::value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yikes to some of this formatting...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine to change the format rules, especially if they make it more LLVM like. Autoformatting is not perfectly "automatic", a little fiddling can be done to get it to format nicely (like moving comments).
#include "detail/common.h" | ||
#include "detail/descr.h" | ||
#include "detail/type_caster_base.h" | ||
#include "detail/typeid.h" | ||
#include "pytypes.h" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't right, we need to add special clang-format comments to fix this.
This also broke a lot of clang-tidy comments. |
Looks like |
e248d94
to
63f0695
Compare
I've set the include order to be pybind11 first, detail/* second, other local includes third. The order shouldn't matter, we should IWYU, but for now, this hopefully will work. |
Ahh, NOLINTNEXTLINE should count logical lines. :'( |
$ brew install act
$ act -j clang-tidy Very nice. :) |
template<size_t ...S> struct make_index_sequence_impl <0, S...> { using type = index_sequence<S...>; }; | ||
template<size_t N> using make_index_sequence = typename make_index_sequence_impl<N>::type; | ||
template <size_t...> | ||
struct index_sequence {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These sorts of things would be a lot more readable in the new form with an extra blank line in-between.
Quick question: when I tried this previously I had trouble with
comments getting garbled. I even wrote a small script to massage those, to not get messed up by clang-format. But now it seems to be fine without doing anything special. (?) Mostly curious: Does anyone know what changed? |
.. _PyMemoryView_FromMemory: https://docs.python.org/c-api/memoryview.html#c.PyMemoryView_FromMemory | ||
\endrst */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
clang-format still seems to be unaware of reStructuredText.
Previously the doc generation would fail after I ran clang-format (clang 12).
Are we just getting lucky now?
I wrote an even simpler script to extract the Here is the diff: rwgk/pybind11_scons@fa4ffb4 I think we need to curate the changed blocks, at least some, and protect them from clang-format. I believe it's best to systematically protect all such |
An alternative line of thought:
|
I'm quite okay to turn off comment formatting, I have it off for other areas (including CMake comments, maybe even in this repo). I'd rather have no comment formatting vs. having to watch out for all RST blocks being manually protected. Can we keep the "nice" changes (like reflowing text to the right length) and use your script to recover the rst formatting, then just turn it off? |
(I'm also okay for comments to go over length, but I don't know how to turn that off, including in black) |
Sounds great, I'll try to work on it tomorrow morning.
Yeah, that's a minor loss. After we get this PR merged, we could maybe hook in a small script that flags long comments. I bet a page worth of Python will do most of the job, with some simple approach to allowing exceptions. |
I'm hoping to avoid too much customization, we are getting rid of our custom formatting script. :) I think we may still get line length warnings - and I'm really not too worried about comments. The thing I don't like is this: AVeryLongLongLongClass a_very_long_long_long_varaible_or_other_expression(); // NOLINT Turning into this: AVeryLongLongLongClass a_very_long_long_long_varaible_or_other_expression();
// NOLINT or this: AVeryLongLongLongClass
a_very_long_long_long_varaible_or_other_expression(); // NOLINT But I think we are stuck with that. For new code, you usually can rework it to make it shorter, us a |
This has an interesting side-effect on comments for pragmas. Manually tweaked to something that looks ok and is not modified by clang-format anymore.
Done. My tweaks are in commit 60b7eb4. Setting |
I spent a few minutes glancing through the net diffs. It sure does odd things with comments: I manually changed that block to this:
But then rerunning the pre-commit changed it straight back. What I'd really want: enforce one space (or two?) exactly before The rest looks acceptable, although I cannot say that I like how it deals with |
Okay, I'll see if we can handle those things better. |
Looks like it’s hard to improve WRT comments. What should be done is long trailing comments need to be placed above the line, not on the same one. We need some new blank lines separating sequences of definitions, too. Or we could turn up the line length a lot. |
} | ||
else return false; | ||
msecs = microseconds(PyDateTime_TIME_GET_MICROSECOND(src.ptr())); | ||
} else |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stuff like this is why I woould vote always endorcing braces.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stuff like this is why I woould vote always endorcing braces.
Absolutely. (Missing braces have tripped me up many times.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you have code automatically formatted, then whitespace is always in sync with braces, so it doesn't bother me at all. I just completely ignore braces and pretend it's Python. :) Though I'm totally fine with requiring the braces, as I said, I ignore them anyway. ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so, too. With this in my mind: when developing pybind11 in the future, clang-format will always do one thing or another that doesn't look great. Then we will look and tweak to make it look ok before sending PRs for review. Related but separate note: when I worked on this previously I was organizing the work to have all manual changes before running clang-format. It's extra work, but it looks much better in the git history: it will be easy to see what was manually changed. If we notice problems later, we will more easily understand how they came about, and therefore feel more certain how to properly fix. We will also have complete certainty which issues were introduced by clang-format, and again be more certain about how to fix; and we have something to point to for filing clang-format bugs. Once we agree here, I could work on splitting into two PRs (part 1: manual, part 2: exclusively automatic). |
I'd recommend always working on things like this in two commits. The first is the manual tweaking, and then the second is after the auto run. Then you can just keep force-pushing changes to the first comment & the followup auto-run. So we need to add the always-braces rule, and maybe move or add whitespace around a few of the really egregious comments and bunches of previously one-line statements? |
Clang-format can't do this, actually, but |
I'm surprised! |
Informed by work under pybind#3683: pybind@60b7eb4 pybind@59572e6
Informed by work under pybind#3683: pybind@60b7eb4 pybind@59572e6
Informed by work under pybind#3683: pybind@60b7eb4 pybind@59572e6
Informed by work under pybind#3683: pybind@60b7eb4 pybind@59572e6
) * Manual line breaks to pre-empt undesired `clang-format`ing. Informed by work under #3683: 60b7eb4 59572e6 * Manual curation of clang-format diffs involving source code comments. Very labor-intensive and dull. * Pulling .clang-format change from @henryiii's 9057962 * Adding commonly used .clang-format `CommentPragmas:` * Ensure short lambdas are allowed Co-authored-by: Aaron Gokaslan <[email protected]>
Description
Full clang-format, in pre-commit.
Suggested changelog entry: