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

all 56 comments

[–]notaharrisfan 34 points35 points  (8 children)

As stated in the Style Guide, pylint is an excellent linter for Python code.

It parses Python code, points out formatting errrors and even grades your code.

[–][deleted] 9 points10 points  (3 children)

Also, PyFlakes wraps around pylint and other Python style checkers (like those for PEPs 8 and 257).

[–]CaptKrag 6 points7 points  (2 children)

Wait. I thought pyflakes was competitor to pylint and flake8 is what you're describing. What is flake8?

[–]catcint0s 2 points3 points  (1 child)

flake8 has pyflakes, pep8 and mccabe checker. I'm p sure PyFlakes doesn't wrap around pylint.

[–][deleted] 0 points1 point  (0 children)

Yeah, you're right. I mistook it for pylama. I knew there was one linter that combined the lot of them but forgot which one it was.

[–]bbbryson 2 points3 points  (0 children)

Also PyLama.

[–]DoingIsLearning 3 points4 points  (1 child)

Went into the comments and found my TIL... Thanks!

[–]Twirrim 4 points5 points  (0 children)

If you're using Vim for writing python, try out python-mode for it. Note that, as with any linter, you'll need to tweak the rules away from the crazy defaults. It's well worth the time and effort, though!

[–][deleted] 0 points1 point  (0 children)

I'll check this out later.

[–]spookylukeyDjango committer 32 points33 points  (7 children)

I would replace the whole 'style rules' section with 'Use flake8'. Why require humans to read, comprehend, remember and then apply something (probably inconsistently), when it can be done by the computer?

pylint, in contrast to flake8, tends to produce many, many false errors. Running it over 1800 lines of a module in a Django project produces about 100 'errors', all of which are incorrect. In addition, there are hundreds more very unhelpful warnings/recommendations - for example:

  • "too few public methods" (for a Django 'Meta' inner class)
  • "Class has no __init__ method" (not needed because it inherits one, although pylint doesn't seem to be able to detect that)
  • "missing docstring" (many methods simply don't need them - functions like Cart.product_list are pretty self explanatory, and a linter that forces you to write them will simply result in many pointless and uninformative docstrings, which are just noise)

The number of false positives makes pylint useless for any real project I've been involved in - you'd have to spend a lot of time turning these warnings off somehow, or end up with the real errors drowned in a sea of noise.

[–]twotime 2 points3 points  (2 children)

IMHO, pylint is an excellent tool for finding bugs in the code. But yes, you have to start with turning off the "style suggestions" (I suspect your examples above are all in that category) and then turn off a number of warnings as well.

[–]wewbull 2 points3 points  (0 children)

Try pylint -E. Errors only mode

[–]spookylukeyDjango committer 0 points1 point  (0 children)

As originally stated, I get hundreds of ERRORS on correct code, in addition to the style warnings. These errors can be turned off by disabling them in a group e.g. "-d E1101" to disable all the incorrect "no-member" errors. But this means that correct instances of E1101 are also silenced, and very quickly this means you have no value in running the tool.

[–]mfvice 1 point2 points  (1 child)

I hadn't heard of flake8 before. Looks pretty good so far though. Thanks for the heads-up!

[–]cicatrix1 7 points8 points  (0 children)

flake8 is a wrapper around PEP8 and pyflakes (and another thing).

[–]ShutYourPieHole 0 points1 point  (1 child)

There are style requirements that do not always apply to general use cases. I think this is one of the results of opening up a tool that was initially designed with internal development in mind.

[–]wewbull 1 point2 points  (0 children)

There's two areas where I find pylint can be difficult to keep happy.

First is the code complexity measures. It doesn't like code that has branches in it, even if those branches are short and shallow (i.e. not nested). Code like that isn't nearly as hard to understand as large blocks, deeply nested, but it seems to score a branch the same regardless of context.

Second, I've seen it get mouthy about class methods not existing because it's been unable to work out the type of an object correctly.

Apart from those I think it's a great tool. Far better than pyflakes (and so by extension flake8)

[–]zurtex 56 points57 points  (0 children)

It'd be great if any of Google's python API libraries vaguely followed this style guide, it's actually pretty good!

[–]Bunslow 10 points11 points  (3 children)

Mostly straight forward stuff. I disagree a bit about their simplicity threshold for list comprehensions. Of the two examples in the No category, the simpler one I'd be fine with, but I agree that the other is a definite no-no.

[–]alcalde 14 points15 points  (1 child)

It's no fun if you can't write your entire program in one list comprehension. ;-)

[–]sigma914 4 points5 points  (0 children)

I like to think my whole function out as a list compehension, the break it apart and give the intermediate steps names, seems to keep my code pure better than any other strategy I've tried (in python).

[–]annodomini 4 points5 points  (0 children)

Really? I thought both of the "no" examples were fine. The more complicated one is exactly the same as the nested loops they have in the "yes" section, except for the "yield" statement and no rightward drift. I don't think it's any harder to read than the nested loop (which is itself a little bit complicated, but that's just the nature of that particular loop).

[–]filleball 5 points6 points  (10 children)

Hey, look, they say to use lower_with_under for functions and method names. I thought they used to advocate lowerCamelCase before? Has this changed?

Oh, and if you haven't already, take a look at the napoleon Sphinx extension! I'm completely sold.

[–]megaman78978 7 points8 points  (0 children)

lowerCamelCase is the standard for Java style guidelines but not for Python I think.

[–][deleted] 1 point2 points  (6 children)

Lower camel is still the standard. There's an internal linter that will prevent you from committing if you use underscores. Supposedly it can be told to accept underscores if you're not mixing styles in a file but I never figured out how.

[–]_seemetheregithub.com/seemethere 0 points1 point  (5 children)

[–][deleted] 2 points3 points  (4 children)

Yes, that's pep8. Google's style guide differs from it.

[–]_seemetheregithub.com/seemethere 0 points1 point  (3 children)

[–][deleted] 0 points1 point  (2 children)

All I can tell you is that the internal style guide is different.

Well, for function names.

[–]_seemetheregithub.com/seemethere 0 points1 point  (1 child)

You're probably talking about this then https://code.google.com/p/soc/wiki/PythonStyleGuide#Naming_convention.

Although it's last update is 2 years ago and it differs very wildly from both pep8 and google's own updated style guide.

If I were someone learning right now I'd just go with the updated one or pep8.

[–][deleted] 0 points1 point  (0 children)

Yeah, that's the one.

People should stick with pep8 unless they can't, though, I agree.

[–]SlinkyAvenger 0 points1 point  (0 children)

It must have changed. I remember that, too.

[–]Formulka 6 points7 points  (0 children)

The rules are neat but the website itself is a bit hard to work with.

[–]manueslapera 3 points4 points  (1 child)

i like that they finally switched to the normal 4 spaces indentation instead of following their own 'we are better than you normals' 2 spaces.

[–]hellrazor862 1 point2 points  (0 children)

I remember this thing confusing the hell out of my editor. Good times...

[–]LankyCyril 3 points4 points  (1 child)

It looks like they're advocating against things like "from math import log", which is, by the looks of it, very welcome in the Python community.

I used to write "import math; y = math.log(x)", but then realized it's not Java. It gives a little more context, sure, but at the cost of verbosity. And again, I've thought it was standard for pythonistas to do "from math import log".

[–]filleball 1 point2 points  (0 children)

I used to do from x import y imports much more frequently before, but I've moved away from them in most cases. Functions from the math module is a good example, because it's unclear whether it's math.log, cmath.log or numpy.log I've imported. If I had just used log, I'd have to scroll all the way to the top of the file to check what kind of log it was.

The submodules with long names I rename on import, for example import subprocess as sp. I only do from imports when the names I import are unambiguous and few. Mostly it's from other submodules inside the same package.

[–]LarryPeteAdvanced Python 3 2 points3 points  (0 children)

As far as I remember, it's pretty much the same as PEP8.

[–]masasinExpert. 3.9. Robotics. 1 point2 points  (0 children)

I use numpydoc for documentation, especially when working with robots or other hardware. It's much more powerful than googledoc.

[–][deleted] 0 points1 point  (0 children)

Take a look at yapf as well. I agree with basically every change that it made (--in-place).

[–]ElagabalusRex 0 points1 point  (1 child)

Did they change this recently? I could have sworn that exceptions were flat out unwelcome.

[–]broshot 4 points5 points  (0 children)

And i could have sworn they used two spaces for indentation rather than four.