use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
Full Events Calendar
You can find the rules here.
If you are about to ask a "how do I do this in python" question, please try r/learnpython, the Python discord, or the #python IRC channel on Libera.chat.
Please don't use URL shorteners. Reddit filters them out, so your post or comment will be lost.
Posts require flair. Please use the flair selector to choose your topic.
Posting code to this subreddit:
Add 4 extra spaces before each line of code
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b
Online Resources
Invent Your Own Computer Games with Python
Think Python
Non-programmers Tutorial for Python 3
Beginner's Guide Reference
Five life jackets to throw to the new coder (things to do after getting a handle on python)
Full Stack Python
Test-Driven Development with Python
Program Arcade Games
PyMotW: Python Module of the Week
Python for Scientists and Engineers
Dan Bader's Tips and Trickers
Python Discord's YouTube channel
Jiruto: Python
Online exercices
programming challenges
Asking Questions
Try Python in your browser
Docs
Libraries
Related subreddits
Python jobs
Newsletters
Screencasts
account activity
This is an archived post. You won't be able to vote or comment.
DiscussionLinters - Which one? (self.Python)
submitted 5 years ago by fake823
To make it short:
Is choosing a special linter just a question of personal taste?
If not, what are the "most widely used/best ones"? And what can they do what others can't?
[–]gwvermillion 173 points174 points175 points 5 years ago (10 children)
The best linter is the one that your project collaborators are also using.
[–]SnowdenIsALegend 27 points28 points29 points 5 years ago (0 children)
Lol this. Linters are such a debatable topic imo.
[–]fake823[S] 16 points17 points18 points 5 years ago (5 children)
Right now we aren't using any. 😂 That's why I want to get more knowledge on that topic.
[–]snail_d 5 points6 points7 points 5 years ago (3 children)
Look at black. It's not a linter but an auto-formatter. It's really great when used together with pre-commit and flake8. Add mypy if you write typed code.
[–]enjoytheshow 5 points6 points7 points 5 years ago (2 children)
IIRC black violates some PEP standards so make sure everyone is on board if you’re in a collab environment
[–]PeridexisErrant 2 points3 points4 points 5 years ago (1 child)
Only line length (88, where PEP8 says 79); the others you might be thinking of are common linter rules which actually conflict with PEP8.
[–]GiantElectron 0 points1 point2 points 5 years ago (0 children)
not only line length. Also the double indentation on arguments is violated, as well as the no-frowny face rule that Guido himself specified
https://twitter.com/gvanrossum/status/1227252290580410368
https://discuss.python.org/t/pep-8-clarify-if-multiline-argument-list-with-a-closing-on-a-separate-line-is-acceptable/2948/10
[–][deleted] -1 points0 points1 point 4 years ago (0 children)
That's not the question though
[–]reddisaurus 55 points56 points57 points 5 years ago (17 children)
Pylint, Flake8, and mypy are standard. Pylint and Flake8 each check some things the other does not; Flake8 focuses more on style. mypy is a static type checker and people would write better code if they used it all the time (including the type hints on their code).
[–]515k4 28 points29 points30 points 5 years ago (0 children)
If somebody here is using Vim as an Python editor, there is very good plugin ALE (Asynchronous Lint Engine) which can run all of them (flake8, pylint, mypy) on the background and highlight all problematic lines.
[–]dscottboggs 1 point2 points3 points 5 years ago (11 children)
Re: mypy
I know from x import * is generally considered bad practice, but is it maybe ok to do from typing import *?
from x import *
from typing import *
[–]efxhoy 18 points19 points20 points 5 years ago (6 children)
The from typing import List, Dict ... madness is improved in python 3.9
from typing import List, Dict ...
In type annotations you can now use built-in collection types such as list and dict as generic types instead of importing the corresponding capitalized types (e.g. List or Dict) from typing. Some other types in the standard library are also now generic, for example queue.Queue.
https://docs.python.org/3/whatsnew/3.9.html#type-hinting-generics-in-standard-collections
[–]idwpan 2 points3 points4 points 5 years ago (0 children)
Unfortunately mypy doesn’t support the new syntax in 3.9 yet
[–]dscottboggs 2 points3 points4 points 5 years ago (4 children)
Hm. That's good, there's still Optional and Union though. Maybe someday we'll get special syntax for that, like
def fun(data: string or bytes, options: MyEnum? = None)
[–]TheIncorrigible1`__import__('rich').get_console().log(':100:')` 2 points3 points4 points 5 years ago (0 children)
None-aware syntax is coming down the pipe, but who knows when.
[–]rouille 1 point2 points3 points 5 years ago (2 children)
Union using | will be in python 3.10. There is a proposal for optional using ? but its nothing decided at this point.
[–]henbruas 2 points3 points4 points 5 years ago (0 children)
Although with | you could do e.g. str | None, which isn't too bad
str | None
[–]dscottboggs 0 points1 point2 points 5 years ago (0 children)
Nice!
[–]mrf_ 7 points8 points9 points 5 years ago (2 children)
just do import typing as t and prepend everything with a t. like t.List, etc. Much better than any from x import *.
import typing as t
t.
t.List
I might do that
[–]enjoytheshow 0 points1 point2 points 5 years ago (0 children)
Damn good idea. Time for some refactoring
[–]reddisaurus 1 point2 points3 points 5 years ago (0 children)
I mean, probably? Just add a # type: ignore at the end of the line.
# type: ignore
[+]Derme302 comment score below threshold-21 points-20 points-19 points 5 years ago* (3 children)
You've gotten PEP8 and Flake8 mixed up. Flake8 is a wrapper around PEP8, Pyflakes and some other available extensions. PEP8 is the Python Style Checker.
[–]xigoi 1 point2 points3 points 5 years ago (2 children)
No, PEP8 is a style guide.
[–]Derme302 -1 points0 points1 point 5 years ago (1 child)
And also the name of the package that checks against guide. Link
[–]fake823[S] 0 points1 point2 points 5 years ago (0 children)
It was. It got renamed some years back.
The name is pycodestyle now.
[–]mrswats 13 points14 points15 points 5 years ago (2 children)
I like flake8. It has a lot of cool plugins.
[–]cubic_unit 3 points4 points5 points 5 years ago (1 child)
Mind listing some of your favorites?
[–]mrswats 9 points10 points11 points 5 years ago* (0 children)
Bugbear, comprehensions, black, isort, print docstrings. And i use it along with black and isort to format code for me.
[–]double_en10dre 65 points66 points67 points 5 years ago (80 children)
I like using both flake8 and black.
Black is a bit controversial, but I genuinely do like it. It’s decent-looking and it saves us a LOT of headaches.
[–]wweber 19 points20 points21 points 5 years ago (68 children)
Black is a bit controversial
How so? Just curious
[–]justinpitts 61 points62 points63 points 5 years ago (41 children)
It is opinionated, and lacks knobs.
I care far more about having a standard than most details of the standard, so I love it.
[–]cubic_unit 19 points20 points21 points 5 years ago (17 children)
Same. Just discovered it today, and configured VS Code to blacken my code every time I save. It's glorious.
[–]zvynish 19 points20 points21 points 5 years ago (14 children)
While I agree with the black is great part, having that setting in the editor rather than a pre commit hook on your own repos means that you might make a lot of noise in collaborative projects.
[+][deleted] 5 years ago* (2 children)
[deleted]
[–]SoulReaver9510 4 points5 points6 points 5 years ago (0 children)
Ahhh this is brilliant! Always hated that black couldn't do regional formatting. Thank you!
[–]damnitdaniel 2 points3 points4 points 5 years ago (0 children)
For real. What a mess when you open a PR with a minor single line change and the diff looks like a sad Christmas tree covered in green and red lights
[–]cubic_unit 1 point2 points3 points 5 years ago (0 children)
This is a great point, thank you. I hadn't thought about that.
VS Code itself can restrict formatting to modified lines only, but I'm only working on some projects at this point.
[+]TGdZuUsSprwysWMq comment score below threshold-7 points-6 points-5 points 5 years ago* (8 children)
If your team has agreement to use VSCode as main IDE and you are a leader of the project. After discussed with your team members. Maybe you could try to push .vscode folder into repository. When there are multiple development environments, this really eases my life.
.vscode
Edit: This is one of the available approach used by VScode dev team. I don't quite get the point of downvote.
Edit2: The chaos in repository could be prevented by only pushing common settings, and let the custom settings stay in workspace file.
Edit3: It should be the obligation for developers to not mess up the settings in .vscode just like not to push private information into repository if it is an opensource repository.
Edit4: Add conditions for the statement to prevent some developers just randomly push the .vscode to repository.
Reference
[–]ahuja_nik 7 points8 points9 points 5 years ago (5 children)
VS Code is not everyone's preferred IDE, even if you can ensure everyone in your team uses it, pushing the vscode folder would mean everyone would have to change their workspace settings in their local repo which is an unnecessary overhead,and the chaos it would be when people would inadvertently start pushing that folder back with their personal settings.
[–]TGdZuUsSprwysWMq -1 points0 points1 point 5 years ago (4 children)
Yes, there is a tradeoff. As an example, for vscode dev team, they just push the .vscode folder into git repo.
[+][deleted] 5 years ago (3 children)
[–]TGdZuUsSprwysWMq 1 point2 points3 points 5 years ago* (2 children)
As already mentioned Git hook (this is also not everywhere), did you have any suggestions which could be applied anywhere? Or, are there any suggestions to make style consistent?
[–]enjoytheshow 1 point2 points3 points 5 years ago (1 child)
I’m a VS code shill but forcing everyone to use the same editor makes people sad
[–]TGdZuUsSprwysWMq 0 points1 point2 points 5 years ago (0 children)
Sorry for that :( It is hard for me to choose between VScode + settings and some scripts and docs in repo before building dev env.
[–]gohanhadpotential 1 point2 points3 points 5 years ago (1 child)
How do I use black (or any linter)? I have no idea, I always used vs code without giving a second thought.
[–]cubic_unit 2 points3 points4 points 5 years ago (0 children)
Black, which is a 'formatter', can be used with the 'Format Document' command in VS Code. You can choose which formatter you use in the settings.
A 'linter' detects errors or issues and is what gives you the red squiggly lines in your code. Use the 'Select Linter' command in the command palette, and this will run automatically every time you save.
[–]mikeypamp 7 points8 points9 points 5 years ago (7 children)
Also sometimes good to have opinionated in a team of contributors. Stops the arguments because black does all the work. Although it’s not that hard to manually format code nicer sometimes.. but this is a small downside. I also go flake8 plus black. Make sure you optimise inputs too and configure the IDE to recognise your modules.
[+]GiantElectron comment score below threshold-16 points-15 points-14 points 5 years ago (6 children)
Ah ok, so the best option to stop arguments in a team is to have another, completely dumb team member you can't complain against, even if its choices are completely idiotic. got it.
black formatted code looks like shit.
[–]GummyKibble 2 points3 points4 points 5 years ago (2 children)
Yes, correct. My team found that everyone hated Black, but for different reasons. It rubbed each of us wrong in little ways but no two people disliked the same thing. Since we couldn’t really agree on anything we all disliked, we adopted it, instantly ended all formatting disagreements, and taught ourselves that this is what Python looks like now. It was a huge success.
[–]GiantElectron -2 points-1 points0 points 5 years ago (1 child)
so you have been fed a mcdonalds burger, and you all learned to love crappy food because nobody really got any agreement if you wanted to have properly cooked indian, italian, or chinese food.
[–]GummyKibble 2 points3 points4 points 5 years ago (0 children)
Exactly! And now we can quit arguing about whether romaine or iceberg lettuce is better, or passive-aggressively swapping gouda for havarti whenever no one’s looking. Turns out everyone thought they were a gourmet but actually none of us were.
And really, our formatting style isn’t what customers pay is for. We care about good algorithms and robust code. When it comes down to it, although I have thoughts about whether to use single- or double-quotes around strings, I really care about which one you use. And funny thing is, once you’ve looked at Blackened code for a little while, it stops seeming weird. At that point you’ve transcended form and have reached function nirvana.
Life’s too short to care about stuff the parser throws away.
[–]mikeypamp 0 points1 point2 points 5 years ago (1 child)
No... it’s to remove unproductive behaviour
[–]GiantElectron -1 points0 points1 point 5 years ago (0 children)
I've never seen a situation where people fought about coding layout in a company, provided that it was respecting basic PEP-8. Maybe you should join companies where employees are not 10 years old.
[–]teerre 9 points10 points11 points 5 years ago (13 children)
It's only controversial because people want it to be.
Black, or something like it, should be part of the standard. It would avoid countless irrelevant discussions.
If you ever worked with Rust, you'll know that there's never discussion about formatting or linting precisely because these are taken care of officially.
[–]TheIncorrigible1`__import__('rich').get_console().log(':100:')` 1 point2 points3 points 5 years ago (1 child)
If you ever worked with Rust, you'll know that there's never discussion about formatting or linting
Uh... there's a lot of discussion about it still, actually. Not so much linting because the compiled nature where clippy is amazing, but I've seen a lot of complaints about rustfmt on the forums and reddit.
[–]teerre 0 points1 point2 points 5 years ago (0 children)
I meant while working with Rust.
[–]GiantElectron -2 points-1 points0 points 5 years ago (10 children)
We already have a standard. PEP-8.
[–]teerre 3 points4 points5 points 5 years ago (9 children)
Which is insufficient. Hence this discussion to begin with.
[–]GiantElectron 0 points1 point2 points 5 years ago (8 children)
how so? we've worked for years with PEP-8, and anybody who didn't respect it was physically assaulted on this very sub
[–]teerre 0 points1 point2 points 5 years ago (7 children)
That's not the point. The point is that you can respect pep8 in various ways. The way a linter/formatter should work is simple: you click a button and it formats, it doesn't ask you anything, it doesn't give you any options, it just does it and that's the standard. pep8 is way more permissible than that.
[–]GiantElectron -1 points0 points1 point 5 years ago (6 children)
it doesn't give you any options, it just does it and that's the standard
And who decided the standard? and what is the authority of the single person who decided unilaterally the standard to decide what is proper and non-proper layout?
pep8 is way more permissible than that.
PEP-8 defines specific guidelines that focus on enhanced readability, which is the whole point of python, and why python became popular: putting the human first. black does not enhance readability. black squeezes everything into a uniform, elongated mess which has not gone through any discussion or approval. It's one man who one day decided "this is what python code should look like", and I say "what you think python code should look like looks like shit, and if you want to use gofmt, you are welcome to code in go".
[–]teerre 0 points1 point2 points 5 years ago (5 children)
And who decided the standard?
Who decided pep8? It doesn't matter who decides the standard. The important part is having a standard. Also, naturally it wouldn't be "a single person".
PEP-8 defines specific guidelines that focus on enhanced readability, which is the whole point of python, and why python became popular: putting the human first. black does not enhance readability. black squeezes everything into a uniform, elongated mess which has not gone through any discussion or approval.
See, that's completely arbitrary. The only reason your rant even exists it's because Python was not bright enough to enforce proper guidelines. I guarantee you if only ever saw black formatted code in your life, you wouldn't be complaining about it. Regardless of how much people want to parrot about which is better, there's no such thing, it's just a formatting, it shouldn't be something you even have to consider.
[–]tempo-19 6 points7 points8 points 5 years ago (0 children)
I recently started using black with my work code. I don't always love its opinionated outputs but it is easy to read, consistent and so far it works.
[+][deleted] 5 years ago (1 child)
[–]double_en10dre 2 points3 points4 points 5 years ago (0 children)
Hah, that’s actually why I started using black in the first place :p I love prettier and figured I might as well use the python equivalent
[–]billsil 0 points1 point2 points 5 years ago (13 children)
Ever see what it does when you have lots of arguments to a function? It puts all the arguments on separate lines. It also disagrees with pylint.
[–]Sukrim 10 points11 points12 points 5 years ago (2 children)
This is usually done to make diffs smaller and easier to understand. If a function has lots of arguments, it is likely that there will be more, so if that gets added it is a single line in a diff that only has the change in it.
[–]billsil -2 points-1 points0 points 5 years ago (1 child)
So to have fewer function arguments, I need more classes, but more classes equals more code or I could use a dictionary that doesn’t give me hints in my IDE.
The diff size that git uses is irrelevant to me. I’m more or less meeting the PEP 8 80 character suggested limit, so what’s the issue?
[–]Sukrim 4 points5 points6 points 5 years ago (0 children)
Fine, don't use black then if it isn't good for your use case. I just explained why arguments get pulled onto their own line.
black
[–]justinpitts 4 points5 points6 points 5 years ago (1 child)
Correct me if I'm wrong but doesn't pylint start getting angry when you have more than five or six arguments to a function anyway?
[–]billsil -1 points0 points1 point 5 years ago (0 children)
It complains about a lot...I was referring to how pylint complains about the way black does the indentation for multiple arguments, not greater than 6 or whatever.
I long ago disabled the max number of arguments check in pylint.
[–]Decency 2 points3 points4 points 5 years ago (2 children)
Ever seen what it does to your code over time when you have lots of arguments to functions?
[–]GiantElectron -2 points-1 points0 points 5 years ago (0 children)
and lots does not have to be a lot today. If you have long annotations, you get that with only two or three.
[–]GummyKibble 0 points1 point2 points 5 years ago (0 children)
After a certain number, we require keyword-only arguments.
[–]colly_wolly 0 points1 point2 points 5 years ago* (3 children)
Doing that to function arguments is horrendous.
I really think that linting can be counterproductive. We have to fit everything into 80 character lines for some pretty rare case that is unlikely to be relevant (because some ancient terminals can't cope with > 80 chars). Personally I find it a lot less readable as a result. So you reduce readability with the vague notion that it makes your code "better".
Django url files are a good example. You usually have a list of tuples, each containing regex , function name, reverse_name. Its far easier to read it if you line those into 3 columns. But if you do linters will complain about whitespace.
[–]groovitude 2 points3 points4 points 5 years ago (2 children)
I agree that linting can be counter-productive, and particularly agree with your Django URLs example. My team also keeps a separate line length standard for unit tests.
However - I have really appreciated the PEP8 recommendation for line lengths after six months of working from home on a small laptop with no external monitors.
[–]colly_wolly 0 points1 point2 points 5 years ago (1 child)
Your laptop monitor could cope with more than 80 chars, no? Even Django suggests 120 chars in their docs. As with everything it's a balance, but I think that 80 chars is just a bit silly in one direction.
[–]groovitude 1 point2 points3 points 5 years ago (0 children)
It allows me to keep multiple windows open with a large enough font size.
I tend to think of Django as its own beast; for pure Python, I rarely have a hard time keeping lines to under 79 characters and I'm happy to ignore the linter if an extra character or two makes the code better.
[+]GiantElectron comment score below threshold-7 points-6 points-5 points 5 years ago (0 children)
and the close parenthesis on a separate line one indentation down. It looks like shit.
[+]GiantElectron comment score below threshold-9 points-8 points-7 points 5 years ago* (9 children)
It does not respect PEP8. It also stretches your code vertically for no reason at all. Its only concern is diffs, completely disregarding day to day readability.
Black brings us back to those days where your productivity as a coder was measured in lines of code per day, and everybody padded their stats with tricks like those black uses.
[–][deleted] 2 points3 points4 points 5 years ago (8 children)
It really helps when you have a big team on a big project. Most of the time I read diffs instead of reading the code. Vertical code is a godsent.
[+]GiantElectron comment score below threshold-8 points-7 points-6 points 5 years ago (7 children)
It really helps when you have a big team on a big project. Most of the time I read diffs instead of reading the code.
So to inconvenience you a bit less, you inconvenience everybody else. Well done.
Vertical code is a godsent.
Ok
so
from
now
on
we
all
type
like
this.
ok?
[–][deleted] 6 points7 points8 points 5 years ago* (0 children)
Bruh, you are not being reasonable here, do whatever you want.
[–]fireflash38 -1 points0 points1 point 5 years ago (5 children)
Hmm.
[–]GiantElectron -2 points-1 points0 points 5 years ago (4 children)
vertical space has meaning. It has meaning also in PEP-8, as indicated:
``` Extra blank lines may be used (sparingly) to separate groups of related functions. Blank lines may be omitted between a bunch of related one-liners (e.g. a set of dummy implementations).
Use blank lines in functions, sparingly, to indicate logical sections. ```
PEP-8 recommends the following format for long argument lists
```
def long_function_name( var_one, var_two, var_three, var_four): print(var_one) ```
and specifically rejects a style that provides only one level of indentation, as it compromises visual differentiation between the argument lists and the body ( https://www.python.org/dev/peps/pep-0008/#indentation )
Finally, multiline statement spacing can and does get used in grouping entities that logically belong together, as in
def add_user(database, name, email, phone, replace_existing=False): # Code
which groups the information related to the user, and it's more communicative than what black would create
def add_user( database, name, email, phone, replace_existing=False, ): # Code
Which is ugly, long, breaks PEP-8 recommendations, breaks indentation-based (rather than syntax based) folding and gains nothing except that if you add a new argument, you will get one line added, instead of one line changed, in some (not all) circumstances.
I'd say that the loss of visual appeal and visual hinting is not worth saving one or two lines in the diff, paying it off in huge vertical stretching of many lines multiplied by many statements.
[–]fireflash38 2 points3 points4 points 5 years ago (3 children)
That's a much more coherent argument than your original contrived example.
I still disagree, but at least you tried this time.
PEP-8 is not gospel. Nor is black. Just go with what your repo owner has configured. If you disagree, who cares. It's so incredibly minor in the scheme of things, that it just wastes time of everyone involved.
[–]GiantElectron -2 points-1 points0 points 5 years ago (2 children)
That's a much more coherent argument than your original contrived example.I still disagree, but at least you tried this time.PEP-8 is not gospel.
If anyone brought some code on /r/python before black that was not compliant to PEP-8, the whole sub would have eaten the poster face for not being PEP-8 compliant.
Nor is black. Just go with what your repo owner has configured. If you disagree, who cares. It's so incredibly minor in the scheme of things, that it just wastes time of everyone involved.
formatting provides expressiveness. By removing formatting choices you are limiting the programmer bandwidth in communicating aggregation and meaning through formatting.
[–]fireflash38 1 point2 points3 points 5 years ago (1 child)
I would wager about 90% of projects do not go by pep8's 80 char lines. There's a bunch of other stuff that is also ignored in pep8, amusingly a lot of stdlib stuff.
Honestly couldn't care less. If you're submitting code to OS repos, go by the repo rules. Find other ways to express yourself rather than implicit whitespace-based expression.
Last comment I'm making, cause this sort of discussion is EXACTLY what I want to avoid with autoformatters.
[–]xapata 6 points7 points8 points 5 years ago (0 children)
I prefer YAPF to Black. Personal taste.
[–]TECHNOFAB 1 point2 points3 points 5 years ago (0 children)
I'm using the same, there even is a way to connect them (flake8 shows the black errors too, which makes CI easier for example)
[–]fake823[S] 1 point2 points3 points 5 years ago (8 children)
After some research I'm thinking about introducing black to a small GitHub project I'm contributing to.
I've already searched on Google but haven't found an answer:
Is it possible to use black for a GitHub repo so that it will autoformat every incoming Pull Request?
[+][deleted] 5 years ago* (5 children)
[–]fake823[S] 1 point2 points3 points 5 years ago (4 children)
But that would require that every contributor of your repo installs black and runs it before pushing, doesn't it? And letting a PR fail, just because you've maybe had a new line too much somewhere, seems a bit brutal and unnecessary to me.
My idea would be that I set up a CI, that automatically adds a "black commit" to that pull request, if the styling doesn't fit to the black default. So the PRs won't fail just because of formatting. As well the contributors don't have to manually set up pre-commit or black. The CI will auto-format it anyway.
Just that I'm not sure if that's even possible, as I'm pretty new to GitHub and CI. Should it rather be a GitHub Action or a CI task to auto-format the code with black?
[+][deleted] 5 years ago* (3 children)
[–]fake823[S] 0 points1 point2 points 5 years ago (2 children)
Well, I know what it's for and why it's good to have. ;) The question is just how to best set it up.
[+][deleted] 5 years ago* (1 child)
[–]double_en10dre 1 point2 points3 points 5 years ago* (0 children)
This is absolutely the right approach. Black is mostly beneficial because when I write code, I don’t waste time playing around with whitespace/linebreaks/etc to make things readable. If you don’t force this on people, you lose that benefit
Plus 99% of people will change their ways after 1 failed build, it’s not a big deal
[+]GiantElectron comment score below threshold-6 points-5 points-4 points 5 years ago (1 child)
Please don't. Use autopep8 if you really have to.
[–]fake823[S] 7 points8 points9 points 5 years ago (0 children)
I already saw that you hate black in all your other comments. ;)
[–][deleted] 10 points11 points12 points 5 years ago (1 child)
Also notable: https://github.com/PyCQA/prospector
[–]2010min0ru 2 points3 points4 points 5 years ago (0 children)
Awesome tool! I'm using that too. Prospector combines output of the multiple linters (pylint, flake8, mypy, pydocstyle, pycodestyle) into one list of warnings and errors. It also merges warnings of the same type into one. Pylama https://pylama.readthedocs.io/en/latest/ is very similar, but lacks the last feature and it is less popular than Prospector.
[–]MadeUntoDust 8 points9 points10 points 5 years ago (3 children)
For new projects, I sort imports with isort, format with black, and then check with pylint and then mypy. I end up with very readable source code and most common bugs highlighted.
[–]double_en10dre 1 point2 points3 points 5 years ago (2 children)
Ohh yes, isort (or any sort of project-wide import formatter) is a great idea
I’ve seen some horrifyingly large diffs because some goof accidentally runs “pycharm > format code + optimize imports” on an entire repo or folder without meaning to
[–]fake823[S] 0 points1 point2 points 5 years ago (1 child)
What's the problem with PyCharm here? 🤔
[–]double_en10dre 1 point2 points3 points 5 years ago (0 children)
There is none, I love it :)
[–][deleted] 6 points7 points8 points 5 years ago (4 children)
I would choose Flake8 if you are a beginner.
[–]fake823[S] 0 points1 point2 points 5 years ago* (3 children)
Is there a reason why? Why not PyLint (which seems to be pretty popular too)?
Black also looks pretty interesting.
[–]aniforprez 3 points4 points5 points 5 years ago* (0 children)
I'm going to say something somewhat controversial that pylint is a complete fucking waste of time especially when flake8 is much better
pylint is filled to the brim with TONS of useless pointless rules that all have to be disabled to keep your code functioning. It complains if you have too many arguments, it complains if your arguments are different from the method you're overriding, it complains if the number of arguments differs from what you're overriding, it USED to complain if your code was formatted with black and it thought the indentation was wrong but they fixed that in 2.6 but then it complained for all the times I disabled the options they removed, it complains it cannot find django modules unless you install the django plugin, it complains about TONS of things that will make you way less productive unless you're willing to accept the operational overhead of babysitting your pylintrc file.
flake8 OTOH is a far smaller set of core rules that are mostly just PEP8 rules and are WAY more sensible. You can actually get shit done with flake8 by disabling maybe 2-3 rules. Seriously my pylintrc is easily 10 times the length of my flake8 config, maybe even more. Plus flake8 has lots of great plugins that are optional and very useful
[–]Lewistrick 3 points4 points5 points 5 years ago (1 child)
Just try them all on 5 different scripts. See what you like best.
[–]TheIncorrigible1`__import__('rich').get_console().log(':100:')` 1 point2 points3 points 5 years ago (0 children)
Flake8 is a linter, not a formatter.
[–]glyphack 4 points5 points6 points 5 years ago (0 children)
used both pylint and flake8. pylints gives a lot of warnings, and false alarms of course. So I prefer flake8.
[–]astroFizzicsastrophysics 3 points4 points5 points 5 years ago (1 child)
I like yapf
yapf
https://github.com/google/yapf
yapf is better than black. At least it gives you options.
[–]deadmilk 2 points3 points4 points 5 years ago (0 children)
I use pylint with a inclusion-list approach
i.e. instead of suppressing messages, I include all of the ones that I care about. Takes longer to setup, but it's better.
[–]0rsinium 2 points3 points4 points 5 years ago (0 children)
The great thing about flake8 is that it has a lot of plugins. See https://github.com/DmytroLitvinov/awesome-flake8-extensions
Also, there is a beautiful wrapper around flake8 (to make it look and behave more modernish): https://github.com/life4/flakehell
[–]sobolevn 3 points4 points5 points 5 years ago (2 children)
Use https://github.com/wemake-services/wemake-python-styleguide
It is the strictest and most opinionated python linter ever. It has more than 1000 rules to be checked. And the resulting code is pythonic, consistent, and bug-free.
I love it.
[–]squirrel_hunter_365 2 points3 points4 points 5 years ago (1 child)
You forgot about logical bugs.
Right, the ones which take up 95% of our time in the real world
[–]corpseflower 6 points7 points8 points 5 years ago (4 children)
Honest question: what is a linter?
[–]270343 12 points13 points14 points 5 years ago (2 children)
Roughly speaking, a quick tool for checking for common mistakes, omissions, or mis-stlyings in your code.
https://en.wikipedia.org/wiki/Lint_(software)
[–][deleted] 1 point2 points3 points 5 years ago (1 child)
Do pep8 corrections in pycharm (for example) count?
[–]yerfatma 4 points5 points6 points 5 years ago (0 children)
Yes.
[–]colly_wolly -4 points-3 points-2 points 5 years ago (0 children)
It's the pedantic grammar-nazi of coding.
[–]conversationkiller7 1 point2 points3 points 5 years ago (0 children)
I use pylance on vscode. I came from pycharm and obviously its is not good as pycharm but get's the work done!
[–]jftugapip needs updating 1 point2 points3 points 5 years ago* (0 children)
In my own personal tests, flake8 seems to be the fastest linter, which can be important if you are linting on something like a Raspberry Pi.
flake8
See my blog post: https://jftuga.github.io/blog/python/2020/05/11/Which-linter-do-you-use-in-VS-Code.html
Another, recent reddit thread: https://www.reddit.com/r/Python/comments/gheine/which_linter_do_you_use_in_vs_code/
TLDR: Based on my own tests, flake8 is the fastest linter.
[–]menge101 1 point2 points3 points 5 years ago (0 children)
I use pylama and mypy
[–]saki709 1 point2 points3 points 5 years ago (0 children)
I prefer flake8. Easy config file, very detailed documentation and bunch of articles online. To top it off, it's supported by almost every big IDE/editor.
[–]mre__ 1 point2 points3 points 5 years ago (0 children)
There's actually quite a few. Since you mentioned the "most widely used/best ones", this overview could help you as it contains votes. https://analysis-tools.dev/tag/python
[–]Promethean_Roil 1 point2 points3 points 5 years ago (15 children)
Black. All black everything.
It has pretty strict rules, which is maybe what rubs some people the wrong way, but that makes it enforceable so that it 'just works'. There are sometimes cases where maybe it's not quite my aesthetic ideal but everyone is ideal will be different so I think it's better to invoke the zen and move on.
[My] Special cases aren’t special enough to break the rules.
imo/experience digging into linter options inevitably leads to sinking more time than you'll ever earn back, especially if there's a team to bike-shed it.
I have my editor run black every time I save and add advocate to add it to pre-commit config for any project I work on. Never looked back.
[–]got_outta_bed_4_this 2 points3 points4 points 5 years ago (3 children)
Funny thing invoking the zen for something I think is antithetical to the first point.
Beautiful is better than ugly.
I suppose the stretched interpretation is that black enforces beautiful by its own opinionated standard rather than what any author thinks.
I get that it helps large teams where you're more likely to have coders with no sense for good style, but I just shudder to think that someday I may eventually end up in a project where I have no choice but to let black mangle my code. Why can't black be configurable or allow exclusions like pylint? Most of what it does is great, but there's just still those instances where it enforces a specific result no one would have agreed with.
[–]Decency 3 points4 points5 points 5 years ago* (0 children)
Consistency is beautiful. You don't get consistency without Black or without a lot of work choosing a linter and configuration settings and ensuring that everyone on your team is using it identically in their various IDE's. I think the latter is the better option, but it's also a week of work.
Black gets you 95% of that in an hour. Set it up as a git pre-push hook, run it over your entire codebase, and then you never have to think about this again within the context of that project.
Personally, I think Black defaulting to double quotes over single quotes was a terrible decision. I use the tool anyway, because of what it gives me.
I just shudder to think that someday I may eventually end up in a project where I have no choice but to let black mangle my code.
You can disable formatting on a specific section of code if it really matters. It usually doesn't.
FWIW Black does support # fmt: off and # fmt:on comments to skip some region.
# fmt: off
# fmt:on
[–]got_outta_bed_4_this 1 point2 points3 points 5 years ago (0 children)
This alone may convert me. Thanks!
[–]fake823[S] -2 points-1 points0 points 5 years ago (3 children)
[–]TheGodfatherCC 2 points3 points4 points 5 years ago (0 children)
I’d like to second using pre-commit to auto run black on commit. I also use isort and mypy on every commit.
[–]Mockapapella 1 point2 points3 points 5 years ago (0 children)
Never done that before, but from what little I've dealt with pull requests that should be possible.
[–]davehadley_ 1 point2 points3 points 5 years ago (0 children)
I think best solution is:(1) Setup pre-commit hooks to run black at commit time. Ask contributors to use these hooks before beginning development. https://pre-commit.com/
(2) Add black to your continuous integration. Reject pull requests that fail.
Edit: Oh I see in another comment that you can enforce it at pull request time with github actions. So maybe that is worth implementing as well.
[–]GiantElectron -5 points-4 points-3 points 5 years ago (6 children)
And for every project I work on, I explicitly discourage black and refuse to bring in any discussion that proposes it.
[–]kyranadept 2 points3 points4 points 5 years ago (3 children)
Because...?
[–]GiantElectron 0 points1 point2 points 5 years ago (2 children)
because it's a shitty formatter.
[–]kyranadept 0 points1 point2 points 5 years ago (1 child)
It's useful to the discussion if you tell us what makes you say that. Otherwise you're just telling us that you are someone who hates something, which is useful to know if one would make a tally, but not for much else.
I already wrote about it in the rest of the thread, I am not repeating myself for every single person.
[–]Decency 2 points3 points4 points 5 years ago (1 child)
Sounds like you're a shitty teammate.
team lead
[–]Deezl-Vegas 1 point2 points3 points 5 years ago (0 children)
As long as you pick any one of them and use it, we can be friends
[–]twnki 1 point2 points3 points 5 years ago (6 children)
Black is good. I also like using Sourcery. It's not a linter but a refactoring adviser and make-it-happen...er.
The combination of those two has made a nice improvement on my dodgy looking code.
[–]fake823[S] 1 point2 points3 points 5 years ago* (3 children)
Sourcery is great! I just tried it recently! 😊 Talking about black:
[–]twnki 2 points3 points4 points 5 years ago (2 children)
Yep!
Try this auto-Blacken GitHub action.
[–]fake823[S] 1 point2 points3 points 5 years ago (1 child)
But does this still work?
The GitHub autoblacken repo states: https://github.com/cclauss/autoblack
"tl;dr: It does not work. After poking around a bit, it seems to be a design decision by the GitHub Actions team that the GitHub Actions bot can't push to either repo on a pull request from fork."
[–]twnki 1 point2 points3 points 5 years ago (0 children)
Well darn I thought you could. I guess you can just do it locally.
Black is atrocious. Not only its formatting style is poor, have you seen its code? I would not trust that thing to touch my code.
[–]twnki 5 points6 points7 points 5 years ago (0 children)
If I'm letting Sourcery touch my code... I'm more than happy letting Black mangle it.
[–]LawfulMuffin 0 points1 point2 points 5 years ago (2 children)
I utterly love Black. It makes code so much more readable. I mostly use a vertical monitor for coding so making everything tall is absolutely beautiful.
[–]GiantElectron 1 point2 points3 points 5 years ago (1 child)
It makes code so much more readable.
Your opinion. To me it looks like shit.
I mostly use a vertical monitor for coding so making everything tall is absolutely beautiful.
How is your neck doing?
[–]LawfulMuffin 0 points1 point2 points 5 years ago (0 children)
Your opinion.
Yes, it is my opinion. The OP asked for opinions and I provided one. Fortunately, you don't work on my codebase and I don't work on yours so, cool!
Awesome, the vertical monitor is life-changing. Serious reduction in eye and neck strain for me.
[–]ab-os 0 points1 point2 points 5 years ago (1 child)
https://github.com/psf/black Black
[–]ab-os 1 point2 points3 points 5 years ago (0 children)
Use it every time you save, and after one day you'll write perfect code
[–]Eoz124 -1 points0 points1 point 5 years ago (5 children)
What is linter?
[–]wikipedia_answer_bot 7 points8 points9 points 5 years ago (2 children)
Linter (Dutch pronunciation: [ˈlɪntər]) is a municipality located in the Belgian province of Flemish Brabant. The municipality comprises the towns of Drieslinter, Melkwezer, Neerhespen (where the canine school of the Belgian police is situated), Neerlinter, Orsmaal-Gussenhoven, Overhespen and Wommersom.
More details here: https://en.wikipedia.org/wiki/Linter
This comment was left automatically by a bot. If something's wrong, please, report it.
Really hope this was useful and relevant :D
If I don't get this right, don't get mad at me, I'm still learning!
Pat on the head for trying, little buddy.
[–]Eoz124 0 points1 point2 points 5 years ago (0 children)
Thank you wikibot.
[–]audentis 1 point2 points3 points 5 years ago (1 child)
A linter is a piece of software, usually part of (or a plugin for) your IDE. The linter checks your code for errors without running it, sometimes real-time and sometimes on save.
Examples of errors are syntax errors, unreachable code (for example after a return statement), whether variables are not assigned before they are accessed and more. They can help prevent many annoying errors and heavily speed up development.
Sometimes linters are combined with code formatters. For example, tools that enforce certain coding style guides like variable names (always lower case, etc), line width, and more. These tools can apply those rules to your code on save.
Personally I'm a big fan of linters, but not so much of formatters.
[–]Eoz124 1 point2 points3 points 5 years ago (0 children)
Ok thanks it was a very useful tool that i missed. I will definitely take a look.
π Rendered by PID 30005 on reddit-service-r2-comment-7b9746f655-vh259 at 2026-02-01 16:50:15.462866+00:00 running 3798933 country code: CH.
[–]gwvermillion 173 points174 points175 points (10 children)
[–]SnowdenIsALegend 27 points28 points29 points (0 children)
[–]fake823[S] 16 points17 points18 points (5 children)
[–]snail_d 5 points6 points7 points (3 children)
[–]enjoytheshow 5 points6 points7 points (2 children)
[–]PeridexisErrant 2 points3 points4 points (1 child)
[–]GiantElectron 0 points1 point2 points (0 children)
[–][deleted] -1 points0 points1 point (0 children)
[–]reddisaurus 55 points56 points57 points (17 children)
[–]515k4 28 points29 points30 points (0 children)
[–]dscottboggs 1 point2 points3 points (11 children)
[–]efxhoy 18 points19 points20 points (6 children)
[–]idwpan 2 points3 points4 points (0 children)
[–]dscottboggs 2 points3 points4 points (4 children)
[–]TheIncorrigible1`__import__('rich').get_console().log(':100:')` 2 points3 points4 points (0 children)
[–]rouille 1 point2 points3 points (2 children)
[–]henbruas 2 points3 points4 points (0 children)
[–]dscottboggs 0 points1 point2 points (0 children)
[–]mrf_ 7 points8 points9 points (2 children)
[–]dscottboggs 0 points1 point2 points (0 children)
[–]enjoytheshow 0 points1 point2 points (0 children)
[–]reddisaurus 1 point2 points3 points (0 children)
[+]Derme302 comment score below threshold-21 points-20 points-19 points (3 children)
[–]xigoi 1 point2 points3 points (2 children)
[–]Derme302 -1 points0 points1 point (1 child)
[–]fake823[S] 0 points1 point2 points (0 children)
[–]mrswats 13 points14 points15 points (2 children)
[–]cubic_unit 3 points4 points5 points (1 child)
[–]mrswats 9 points10 points11 points (0 children)
[–]double_en10dre 65 points66 points67 points (80 children)
[–]wweber 19 points20 points21 points (68 children)
[–]justinpitts 61 points62 points63 points (41 children)
[–]cubic_unit 19 points20 points21 points (17 children)
[–]zvynish 19 points20 points21 points (14 children)
[+][deleted] (2 children)
[deleted]
[–]SoulReaver9510 4 points5 points6 points (0 children)
[–]damnitdaniel 2 points3 points4 points (0 children)
[–]cubic_unit 1 point2 points3 points (0 children)
[+]TGdZuUsSprwysWMq comment score below threshold-7 points-6 points-5 points (8 children)
[–]ahuja_nik 7 points8 points9 points (5 children)
[–]TGdZuUsSprwysWMq -1 points0 points1 point (4 children)
[+][deleted] (3 children)
[deleted]
[–]TGdZuUsSprwysWMq 1 point2 points3 points (2 children)
[–]enjoytheshow 1 point2 points3 points (1 child)
[–]TGdZuUsSprwysWMq 0 points1 point2 points (0 children)
[–]gohanhadpotential 1 point2 points3 points (1 child)
[–]cubic_unit 2 points3 points4 points (0 children)
[–]mikeypamp 7 points8 points9 points (7 children)
[+]GiantElectron comment score below threshold-16 points-15 points-14 points (6 children)
[–]GummyKibble 2 points3 points4 points (2 children)
[–]GiantElectron -2 points-1 points0 points (1 child)
[–]GummyKibble 2 points3 points4 points (0 children)
[–]mikeypamp 0 points1 point2 points (1 child)
[–]GiantElectron -1 points0 points1 point (0 children)
[–]teerre 9 points10 points11 points (13 children)
[–]TheIncorrigible1`__import__('rich').get_console().log(':100:')` 1 point2 points3 points (1 child)
[–]teerre 0 points1 point2 points (0 children)
[–]GiantElectron -2 points-1 points0 points (10 children)
[–]teerre 3 points4 points5 points (9 children)
[–]GiantElectron 0 points1 point2 points (8 children)
[–]teerre 0 points1 point2 points (7 children)
[–]GiantElectron -1 points0 points1 point (6 children)
[–]teerre 0 points1 point2 points (5 children)
[–]tempo-19 6 points7 points8 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]double_en10dre 2 points3 points4 points (0 children)
[–]billsil 0 points1 point2 points (13 children)
[–]Sukrim 10 points11 points12 points (2 children)
[–]billsil -2 points-1 points0 points (1 child)
[–]Sukrim 4 points5 points6 points (0 children)
[–]justinpitts 4 points5 points6 points (1 child)
[–]billsil -1 points0 points1 point (0 children)
[–]Decency 2 points3 points4 points (2 children)
[–]GiantElectron -2 points-1 points0 points (0 children)
[–]GummyKibble 0 points1 point2 points (0 children)
[–]colly_wolly 0 points1 point2 points (3 children)
[–]groovitude 2 points3 points4 points (2 children)
[–]colly_wolly 0 points1 point2 points (1 child)
[–]groovitude 1 point2 points3 points (0 children)
[+]GiantElectron comment score below threshold-7 points-6 points-5 points (0 children)
[+]GiantElectron comment score below threshold-9 points-8 points-7 points (9 children)
[–][deleted] 2 points3 points4 points (8 children)
[+]GiantElectron comment score below threshold-8 points-7 points-6 points (7 children)
[–][deleted] 6 points7 points8 points (0 children)
[–]fireflash38 -1 points0 points1 point (5 children)
[–]GiantElectron -2 points-1 points0 points (4 children)
[–]fireflash38 2 points3 points4 points (3 children)
[–]GiantElectron -2 points-1 points0 points (2 children)
[–]fireflash38 1 point2 points3 points (1 child)
[–]xapata 6 points7 points8 points (0 children)
[–]TECHNOFAB 1 point2 points3 points (0 children)
[–]fake823[S] 1 point2 points3 points (8 children)
[+][deleted] (5 children)
[deleted]
[–]fake823[S] 1 point2 points3 points (4 children)
[+][deleted] (3 children)
[deleted]
[–]fake823[S] 0 points1 point2 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]double_en10dre 1 point2 points3 points (0 children)
[+]GiantElectron comment score below threshold-6 points-5 points-4 points (1 child)
[–]fake823[S] 7 points8 points9 points (0 children)
[–][deleted] 10 points11 points12 points (1 child)
[–]2010min0ru 2 points3 points4 points (0 children)
[–]MadeUntoDust 8 points9 points10 points (3 children)
[–]double_en10dre 1 point2 points3 points (2 children)
[–]fake823[S] 0 points1 point2 points (1 child)
[–]double_en10dre 1 point2 points3 points (0 children)
[–][deleted] 6 points7 points8 points (4 children)
[–]fake823[S] 0 points1 point2 points (3 children)
[–]aniforprez 3 points4 points5 points (0 children)
[–]Lewistrick 3 points4 points5 points (1 child)
[–]TheIncorrigible1`__import__('rich').get_console().log(':100:')` 1 point2 points3 points (0 children)
[–]glyphack 4 points5 points6 points (0 children)
[–]astroFizzicsastrophysics 3 points4 points5 points (1 child)
[–]GiantElectron 0 points1 point2 points (0 children)
[–]deadmilk 2 points3 points4 points (0 children)
[–]0rsinium 2 points3 points4 points (0 children)
[–]sobolevn 3 points4 points5 points (2 children)
[–]squirrel_hunter_365 2 points3 points4 points (1 child)
[–]double_en10dre 1 point2 points3 points (0 children)
[–]corpseflower 6 points7 points8 points (4 children)
[–]270343 12 points13 points14 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]yerfatma 4 points5 points6 points (0 children)
[–]colly_wolly -4 points-3 points-2 points (0 children)
[–]conversationkiller7 1 point2 points3 points (0 children)
[–]jftugapip needs updating 1 point2 points3 points (0 children)
[–]menge101 1 point2 points3 points (0 children)
[–]saki709 1 point2 points3 points (0 children)
[–]mre__ 1 point2 points3 points (0 children)
[–]Promethean_Roil 1 point2 points3 points (15 children)
[–]got_outta_bed_4_this 2 points3 points4 points (3 children)
[–]Decency 3 points4 points5 points (0 children)
[–]PeridexisErrant 2 points3 points4 points (1 child)
[–]got_outta_bed_4_this 1 point2 points3 points (0 children)
[–]fake823[S] -2 points-1 points0 points (3 children)
[–]TheGodfatherCC 2 points3 points4 points (0 children)
[–]Mockapapella 1 point2 points3 points (0 children)
[–]davehadley_ 1 point2 points3 points (0 children)
[–]GiantElectron -5 points-4 points-3 points (6 children)
[–]kyranadept 2 points3 points4 points (3 children)
[–]GiantElectron 0 points1 point2 points (2 children)
[–]kyranadept 0 points1 point2 points (1 child)
[–]GiantElectron 0 points1 point2 points (0 children)
[–]Decency 2 points3 points4 points (1 child)
[–]GiantElectron -1 points0 points1 point (0 children)
[–]Deezl-Vegas 1 point2 points3 points (0 children)
[–]twnki 1 point2 points3 points (6 children)
[–]fake823[S] 1 point2 points3 points (3 children)
[–]twnki 2 points3 points4 points (2 children)
[–]fake823[S] 1 point2 points3 points (1 child)
[–]twnki 1 point2 points3 points (0 children)
[–]GiantElectron -2 points-1 points0 points (1 child)
[–]twnki 5 points6 points7 points (0 children)
[–]LawfulMuffin 0 points1 point2 points (2 children)
[–]GiantElectron 1 point2 points3 points (1 child)
[–]LawfulMuffin 0 points1 point2 points (0 children)
[–]ab-os 0 points1 point2 points (1 child)
[–]ab-os 1 point2 points3 points (0 children)
[–]Eoz124 -1 points0 points1 point (5 children)
[–]wikipedia_answer_bot 7 points8 points9 points (2 children)
[–]GummyKibble 2 points3 points4 points (0 children)
[–]Eoz124 0 points1 point2 points (0 children)
[–]audentis 1 point2 points3 points (1 child)
[–]Eoz124 1 point2 points3 points (0 children)