lervag / apy

CLI script for interacting with local Anki collection
MIT License
228 stars 17 forks source link

chore: add black as code formatter and Github Action #56

Closed denismaciel closed 1 year ago

denismaciel commented 1 year ago

This PR does three things:

  1. As discussed in #52, it adds Black as a code formatter.

    Black doesn't allow much customization, which I think is a good thing. The only option I changed from the default is to tell Black not to change single-quote ' to double-quote ". This is what the --skip-string-normalization does.

    To enforce single quotes wherever possible, I have added an extra pre-commit hook (double-quote-string-fixer).

    Preferring single-quotes over double ones is just me. Let me know if prefer the other way around and I will change it.

  2. The PR adds pre-commit to the development tool set. I really like pre-commit. It makes it easy to lint the code on CI and enforce the coding standards.

  3. The PR adds a Github Action to lint the code and run the tests on different platforms.

For a sample run of the CI workflow, see here.

I realize the PR might be a bit too opinionated on the choices, so do let me know if would like to have something different and I will gladly adapt.

Cheers!

lervag commented 1 year ago

Thanks! I've been very busy the last days, but I will be sure to look at this as fast as I possibly can!

lervag commented 1 year ago
  1. As discussed in improvement ideas #52, it adds Black as a code formatter.

Great! I notice that the configuration for Black is "implicit" in the sense that it is provided as options for pre-commit. Would it be possible to instead add this to something like pyproject.toml or .blackrc or similar so that it would be applied e.g. if I use black from my editor?

Would it make sense to add a dev-dependency for black as well?

The only option I changed from the default is to tell Black not to change single-quote ' to double-quote ". This is what the --skip-string-normalization does.

It's been a while since I did much Python programming. If I remember correctly, there is no real difference between single-quote and double-quote strings. And there is not definite rule about what to prefer. I assume that black will not change quotes unless it is really possible; e.g., double-quote strings that include single-quotes should not be changed. And probably not f-strings and similar?

  1. The PR adds pre-commit to the development tool set. I really like pre-commit. It makes it easy to lint the code on CI and enforce the coding standards.

I'm actually not a fan of commit hooks. I want to run my linting and formatters manually on my own terms (e.g. in my editor). I'm curious, is this stuff opt-in or opt-out?

Also, would it make sense to add a dev-dependency for pre-commit?

  1. The PR adds a Github Action to lint the code and run the tests on different platforms.

Cool, thanks! That looks very good to me!

I realize the PR might be a bit too opinionated on the choices, so do let me know if would like to have something different and I will gladly adapt.

I think it is mostly good, thanks!


I have two more comments to the changes:

ckp95 commented 1 year ago

I prefer double quotes over singles, but I can't really give a reason other than "black does it, I believe it, that settles it"

imo the point of black is that you don't have to configure it; it provides a Schelling Point so that devs don't have to argue about formatting style. If you start making amendments to black's style then you open the road to re-litigating everything and the benefit is lost.

I also agree with @lervag about not liking pre-commit hooks, imo they're too fussy and add needless friction to local development, and you can get the same benefit from CI checks. It's better to make the merge conditional on the various linter checks passing in CI, but otherwise make no restriction on what can be committed to a branch.

lervag commented 1 year ago

Cool; then it seems a good course of action is:

What do you think, @denismaciel?

denismaciel commented 1 year ago

That sounds good! I will adapt/simplify the PR tomorrow or the day after!

lervag commented 1 year ago

Great; looking forward to it :)

denismaciel commented 1 year ago

@lervag @ckp95

I have adapted to the suggestions above:

The commit history is messy right now. At work, we squash before merging, so I don't have too much practice editing commits to make them look nice.

Is it fine if I just squash them into one thing and fore-push it?

Let me know if you'd like to have something else addressed in this PR.

Cheers!

lervag commented 1 year ago

Thanks! I took the liberty of cleaning the commits by changing the order, squashing some of them and rewording slightly. I hope you don't mind.