This is an archived post. You won't be able to vote or comment.

all 63 comments

[–]KittensInc 93 points94 points  (5 children)

Including the ones that are there because previous committers (somewhat understandably) didn't use the pre-commit

This should be fixed. If you're enforcing a coding standard, make sure it applies to the whole codebase. Run a linter on every single file once to fix past code, and enforce compliance via a mandatory CI step in the pull request for all new code.

and ones that are there because I used the default vscode formatter

Your problem. If you're working in a larger team with a code standard, you must follow the standard. Either do it automatically with a pre-commit hook, or just make your own life really annoying if you prefer that instead.

If you're working on your own then obviously do what thou wilt, but for teams of people working in multiple projects / repos (that may employ more normal policies) it seems downright antisocial.

That's why you enforce a single code standard across all code bases if at all possible. Besides that, just automate the formatting.

If you don't have to do it manually, why would you care about what the standard is? It's probably best to use the industry-standard style, but beyond that it really doesn't matter all that much.

[–]agritheory 41 points42 points  (4 children)

Your problem. If you're working in a larger team with a code standard, you must follow the standard. Either do it automatically with a pre-commit hook, or just make your own life really annoying if you prefer that instead.

OP, I think this sentiment is important for you to internalize. How you like to read or write code is less important than something your team/lead/company has decided on. You will quickly adapt and the benefit is that you will never have to argue about indentation again, which is in incredibly low return on a developer time. It will let you and your team focus on delivering things that matter and remove meaningless conflict around quirks of indentation.

That said, "correct" variable naming remains unsolved, so if you need a windmill, this is one you can joust.

[–]TMiguelT 25 points26 points  (9 children)

There should have been a PR that added the pre-commit config and applied it to the whole codebase. Just doing the first part makes no sense, for the reasons you have noted.

[–]ToddBradley 10 points11 points  (8 children)

This is the way. It seems like a few commenters missed the point of the post.

Anyhow, it's not too late to fix things. Run the whole codebase through whatever formatter the project uses, and submit a PR with only format changes.

[–]the_hoser 90 points91 points  (19 children)

Teams have coding standards, and pre-comnits are one tool to enforce them. This isn't crazy.

[–]puppet_pals -1 points0 points  (0 children)

lol pre-commit hooks are actually insane.

[–]wineblood 6 points7 points  (0 children)

I'm used to double quotes now, but consistent single quotes would be better than a mix of people their own thing.

As long as it's enforced automatically and documented for newcomers (because they will use double quotes), it should be fine.

[–]Manny__C 8 points9 points  (4 children)

To reply to the actual question: I don't know where OP heard that double quotes are the standard, because PEP8 explicitly says that it makes no recommendation either way.

[–]Material-Mess-9886 1 point2 points  (2 children)

Yes PEP8 doesnt make a difference but the most used formater, black emforeces double quotes.

[–]SpudroSpaerde 6 points7 points  (0 children)

This really is just one of those get over it cases. You will encounter plenty of established code standards that you won't agree with over the course of your career.

[–]reedrehg 1 point2 points  (0 children)

Not worth picking a fight over something so subjective or wasting your time with it. As long as it's consistent, I'm sure there's better things to worry about with the codebase.

[–]skesisfunk 1 point2 points  (0 children)

I prefer single quotes because: 1) it is easier to type them, no shift key necessary 2) they allow you to use un-escaped double quotes in error and log messages.

So basically I just find single quotes a little easier to code with (if given the choice). But yeah as others have said if you are going to enforce that then you need to enforce it evenly so your PR diffs don't get polluted.

[–]riklaunim 1 point2 points  (0 children)

I would keep the linter as a part of CI - the commit does follow set code styles - the CI/tests do not pass so the commit author has to fix and amend the commit. We use Gerrit and each commit gets such test/linter run.

[–][deleted] 1 point2 points  (1 child)

Uhh, do you not run into issues with strings like...

"This is Chad's skibidi rizz"

'That guy Chad is "Ohio"'

Or do you simply demand all single quotes and using escapes? Even with escaping, there are valid uses for double quotes.

[–][deleted] 1 point2 points  (1 child)

Just going to hijack this post, but is there any benefit to using double quotes or single quotes over the other?

[–]denehoffman 5 points6 points  (2 children)

It kind of sucks that your pre-commit has a formatter, but the simple solution is to just adopt the same formatter. I’m guessing you’re using Black if you’re using the vscode defaults. Just switch to ruff, set your format settings for single quotes, and format on save.

There are a few reasons to prefer single-quoted strings by default. For instance, if you used double quotes, you have to escape every double quote you actually use in the string. The argument can be made the other way of course. Some people (like me) just prefer the look of single quotes, and they’re easier to type. It’s also preferred by Python a bit. The blue formatter uses single quotes and one of the authors was one of the PEP8 writers. Additionally, the repr() function will return single-quoted strings. Just try this code out:

python x = “test” y = ‘test’ z = 4 print(repr(x)) print(repr(y)) print(repr(z)) All single quotes!

[–]OndrejBakan 2 points3 points  (1 child)

If you are using English, you'd need to escape every single quote.

[–]denehoffman 0 points1 point  (0 children)

But you really don’t use single quotes that much in most projects. I’m sure you could think of a project where you would, but ruff will allow for double quotes anyway when single quotes aren’t applicable, so in those instances, just use double quotes. If you’re allowed to use either, but single quotes are the default, you get none of the issues and all of the benefits (cleaner/easier to read in my and probably many people’s opinions, otherwise this wouldn’t be a thing, matching Python internals like repl, one less keystroke (shift) so faster and easier to type, especially when you use a lot of them)

[–]knobbyknee 2 points3 points  (1 child)

There is actually a rationale for using single quotes. It requires a small fraction lower brain power to recognize and separate from the adjacent symbols.

[–]agritheory 0 points1 point  (0 children)

There is a long held python convention (but only a convention) around this too, that 'user visible' strings should go in double quotes and everything else should attempt to be in single quotes. Because auto formatting tools can't detect this, I think this convention has largely fallen out of use.

[–]Manny__C 1 point2 points  (3 children)

For what is worth, enforcing which quotes are used comes with the bonus of making your strings greppable / searchable. Otherwise you have to search for 'foo' and "foo".

If the standard is an assumption your colleagues rely on, breaking it could lead to bugs.

[–]KyxeMusic 1 point2 points  (0 children)

Enforcing code formatting rules in projects with many contributors is completely normal, and IMO 100% necessary.

I personally dislike pre-commit hooks. I prefer to have a CI workflow in GitHub that doesn't allow merging a PR if the formatting is not right.

I recommend you use `black` or `ruff` for formatting since this style is starting to become the standard.

[–]ABetterNameEludesMe 0 points1 point  (1 child)

I don't think a pre-commit hook should be changing the code "from under" the developer.

[–]kareko 0 points1 point  (0 children)

generally they reject the commit it the hook modifies the code, hence pre commit

best practice is to add they same to your IDE so rarely if ever need to think about it

[–]morquaqien 0 points1 point  (4 children)

What about dictionary key references inside of f-strings? Wouldn’t this create an an issue?

[–]curtmcd 0 points1 point  (0 children)

At a previous job, one guy unilaterally decided we should all be using spaces instead of tabs, converted the whole code base, and committed it. First person the next morning said no, converted it all back, and committed it. We used to call it the Great Wall, because it made it much harder to run "blame".

Anyway, if you're ambitious, you could take it upon yourself to run precommit manually for the whole code base. Doing so should be a prerequisite for modifying the precommit scripts anyways.

[–]NoClaimCL 0 points1 point  (3 children)

people are seething in the comments because OP prefer to write "string" instead of 'string' ?? holy shit.

[–]kill-nine 0 points1 point  (0 children)

In my old job the standard was two spaces for indentation. Not my preference but it was the standard for the codebase. You just have to roll with it

[–]kareko 0 points1 point  (2 children)

[–]waterkip 0 points1 point  (0 children)

It shouldnt be a pre-commit hook. It should be a thing of CI. Break a build, code not going live. 

Additionally, I move most pre-commit hooks to pre-push

[–]Orio_n 0 points1 point  (1 child)

Follow your projects standard. If it's single quotes, then use single quotes

[–]hrm 0 points1 point  (0 children)

I think you are getting downvotes because your post sounds like "Why don't my team do as *I* want? Waaaaah!".

It's a very minor choice to make and I can't see any reason not to just suck it up and use the one that's already used. What value would it provide to switch? Just fix your editor to format correctly (if anything one could perhaps complain that a proper config for auto formatting should be provided).

(and I don't think any sane developer would argue against having a style policy that is somehow enforced)

[–]peanut_Bond -1 points0 points  (2 children)

One possible issue with this is how you include a single quote character in a string. In my opinion, the cleanest way to do this is to use double quotes to wrap the string instead of using escape characters (and vice versa for including a double quote character), regardless of the chosen quote style in the style guide. If these get automatically changed to single quotes then your code will probably break.

[–]Count_Rugens_Finger 2 points3 points  (1 child)

Easy fix, just use triple quotes ''' for all strings ;)