-
Notifications
You must be signed in to change notification settings - Fork 221
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
ci: add black formatter with pre-commit hooks #1435
ci: add black formatter with pre-commit hooks #1435
Conversation
Hi @aleksejleonov Many apologies for the terribly late reply, and many thanks for the contribution 🙏 . I've been thinking about this for a while and agree that we should have an automatic code formatting solution in place, but I am still not quite sure what the best solution is.
# currently
g_Na = 100*nS/cm**2
V_r = -70*mV
V_th = V_r + 20*mV # black-formatted
g_Na = 100 * nS / cm ** 2
V_r = -70 * mV
V_th = V_r + 20 * mV
I still need to think about this a bit more, and try black on some example code. Point 1 is mostly an issue for examples/tutorials/etc., so maybe we could simply exclude everything outside of the
Even though it seems radical, I think the second option would actually be better, since I don't want to put any additional burden on contributors. This would also be consistent with the massive string-formatting commit we did earlier, and we already have a |
Oh, and maybe we should also use something like |
Hi @mstimberg,
Yes, I agree with you. Brian's unit system is a kind of DSL and is excellent for readability.
That's an interesting question. From an aesthetic and semantic point of view, I agree with you. Black (or any other formatter) can't reliably identify a string as "data" or "human-readable." So, the responsibility and burden for keeping the code style for string are moved to contributors and maintainers. Black's approach (everything auto-formatted to double quotes) gives the following pros:
I am taking part in a large codebase project with tens of actively contributing developers. For some time, I tried to keep some aesthetical code style rules different from the community standard formatter. But at some point, I gave up and switched to the community standard configuration. And it was a good decision — code reviews become faster and easier. |
Yes, I totally agree with using This PRs covers |
I am totally fine with
|
Thanks again @aleksejleonov for your comments and valuable insights. As you mentioned, there is a tension between what I like best, and what is the easiest for contributors. Since I write most of the code that gets committed these days, I feel that my taste in style matters somewhat, but of course the whole idea is that my preferences should matter less and less as more and more contributors join :) So I think from my side, a good solution/compromise would be:
Any thoughts on all this by others before this PR changes every single file in this repository 😆 ? |
I don't really see the advantage of a standardised code style. 🤷♂️ I'm happy for Marcel to make the call on this. Certainly, we don't want spaces enforced on examples because it would make them look horrible (particularly equation strings). But, for internal code it doesn't matter so much. Personally I don't like standardised spaces around operators, but I don't feel like my preferences about that should count for much any more. 😉 |
A couple of general comments. I actually prefer the spacing between numbers and their units - it makes it easier to parse for my eyes but I suppose it really amounts to what you're used to. Having said that, when it comes down to aesthetic preferences, I would break the tie by going with the Pythonic option... Also as you point out, the more community contributions the project receives, the more it makes sense to adopt such widely applied principles.
Thanks indeed - this is a good initiative and it's nice to see the
I agree. I have no strong view on which auto formatter to use but using one is a good idea. 😄
This seems like a reasonable compromise to me.
Agreed. Better to rip the plaster off and move on with our lives! 😉
#TODO...
Yes, I think these would be a good idea in the same spirit as using
Do it! 😝 |
I know what you mean, but the main advantage I am seeing is that there are some style-related things I do care about (say, I wouldn't want to have a
Equation strings wouldn't be affected in either case – the content of strings will never be affected.
I think we already did this with #1364 😊 |
To make it a bit more concrete what
For 2, here's a little example from our code: Lines 921 to 930 in cfd5da8
would be reformatted as: def _nextclocks(self):
clocks_times_dt = [
(c, self._clock_variables[c][1][0], self._clock_variables[c][2][0])
for c in self._clocks
]
minclock, min_time, minclock_dt = min(clocks_times_dt, key=lambda k: k[1])
curclocks = {
clock
for clock, time, dt in clocks_times_dt
if (
time == min_time
or abs(time - min_time) / min(minclock_dt, dt) < Clock.epsilon_dt
)
}
return minclock, curclocks There's one thing we still need to figure out: how to deal with the indentation in multi-line strings. For example: Lines 37 to 51 in cfd5da8
would be reformatted as: prefs.register_preferences(
"core.network",
"Network preferences",
default_schedule=BrianPreference(
default=[
"start",
"groups",
"thresholds",
"synapses",
"resets",
"end",
],
docs="""
Default schedule used for networks that
don't specify a schedule.
""",
),
) which is clearly sub-optimal for the |
Bit late to the party, but here are my 2 cents:
|
@mstimberg, I will slightly update this PR according to the discussion:
|
fef98e8
to
2b1ea41
Compare
@mstimberg, I have added [tool.black]
target-version = ['py37']
include = '^/brian2/.*\.pyi?$'
preview = true The main idea is that the contributor could run either Both I don't add |
2b1ea41
to
60b7b3d
Compare
@aleksejleonov Thanks for the update! I will have a closer look tomorrow, update the docs and if everything is working smoothly on my end, I'll merge the PR. |
Some manual re-alignment of mult-line strings Co-authored-by: Marcel Stimberg <marcel.stimberg@inserm.fr>
60b7b3d
to
d6a5dbd
Compare
@aleksejleonov I hope it is not considered rude, but I just force-pushed into your branch. I wanted to split the commit into the part that updates the Github workflow, adds There are only two things open, before I can go ahead with the merge: 1) the developer documentation needs to be updated and 2) the CI to build our packages seems to be broken, I need to look into why (maybe because we now have a |
Instead of installing them manually with CIBW_BEFORE_BUILD
Also add black badge to README, and fix an unrelated typo in release notes [ci skip]
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.
Alright, that's a wrap. Thanks again @aleksejleonov 🙏 and everyone else who commented. I fixed the CI issues, updated the development docs, and verified that everything works on my side, including the VS Code devcontainter.
If no one has any last-minute comment/complaint, I'll go ahead with the merge.
Proposal for #1295.
pre-commit
on changed files for PRs. The action does not correct formatting automatically — it simply fails with a description of the reason.This approach enforces formatting only on changed files and is gentle. But it will require contributors to fix the formatting of any existing files their further PRs will touch (so PRs would be a bit harder to review with multiple format-related changes).
Another option — with
pre-commit run --all-files
I could fix formatting for the project at once and add all those changes to this PR (~350 changed files, tests are green). This approach is a radical one, but it will simplify all further contributions.