Is docstring max line-length different to normal PEP8 standard? - python

So im looking at some code and bringing it up to PEP 8 standard with the help of pylint and i noticed that if i was using triple quotes for a print statement where the text went past 120 chars (we are allowing 120 instead of 79) pylint didn't complain.
Is this a bug in pylint or does it think it might be a comment and is more lenient with the length of lines or does it not care about how far over you go with strings in trippple quotes because you may want to format them that way?
For clarity: yes pylint works normally in every other case of going over the line length.

Having used pylint regularly, I have also noticed this inconsistency. In the Maximum Line Length section of PEP8, it says:
Therefore, please limit all lines to a maximum of 79 characters. For flowing long blocks of text (docstrings or comments), limiting the length to 72 characters is recommended.
I know that pylint does not at all enforce the 79 character or the 72 character line length limits for docstrings and comments, so I would guess that this is a pylint bug since it is non-compliant with PEP8 in this area.

As pylint maintainer, I can tell this is definitly a bug.
#Jacxel : if you've trouble registering on logilab.org, you can still post the pb on the python-projects#logilab.org mailing list
thanks

Related

Is there a limit to the length of a function name in python?

Every length seems to work, as long as I conform to the rules of defining the function name.
What exactly is the limit or is there even one?
https://docs.python.org/3/reference/lexical_analysis.html#identifiers
They are unlimited. However lines longer than 79 characters violate pep style guides.

PEP8 - Contradiction between E129 and E127/E128

According to the PEP standards, indents should come before binary operators. Furthermore, multiline conditions should be enclosed within parentheses to avoid using backslashes before newlines. These two conventions lead to the following situation
if (long_condition_1
or long_condition_2):
do_some_function()
This code in turn breaks E129 visually indented line with same indent as next logical line in PEP8. However, the second line must be indented exactly four spaces, as otherwise it breaks E128 or E127 for under-indented or over-indented lines.
How should one format the above so that it confirms to PEP8 standards?
This should work properly
if (long_condition_1 or
long_condition_2):
do_some_function()
The answer to this question has changed over time. Due to a change in stance from PEP8, W503 is now widely regarded to go against PEP8.
PEP8 now says it's fine to break before OR after, but to keep it consistent locally.
For newer code, Knuth-style is preferred (which I think refers to breaking before the operator).
if (
long_condition_1
or long_condition_2
or (
long_condition_3
and long_condition4
)
):
do_some_function()
if any((long_condition_1,
long_condition_2)):
do_some_function()
it's better to read when both conditions aligned too ...

How to break a line in a function definition in Python according to pep8?

I have the following line of code that is just over the limit of 79 characters:
def ReportResults(self, intTestID, startTime, stopTime, version, serverType):
How do I break the line in the correct way according to pep8?
According to PEP8:
The Python standard library is conservative and requires limiting lines to 79 characters (and docstrings/comments to 72).
This is in my opinion the main rule to observe.
Besides this rule, PEP8 recommends aligning brackets, so I would do something like this:
def report_results(self,
intTestID,
startTime,
stopTime,
version,
serverType):
pass
Note that I renamed your method report_results, following the recommended lower_case_with_underscores. Also, notice that the indentation should be aligned with the first letter of the first parameter and not the parenthesis.
79 characters is more of a guideline than a rule.
Some teams strongly prefer a longer line length. For code maintained exclusively or primarily by a team that can reach agreement on this issue, it is okay to increase the nominal line length from 80 to 100 characters (effectively increasing the maximum length to 99 characters), provided that comments and docstrings are still wrapped at 72 characters.
Additionally, that line is only 77 characters long, so you should be fine anyway. However, if you'd like to break it up, you can use implicit continuation:
def ReportResults(self,
intTestID,
startTime,
stopTime,
version,
serverType):
If you have a function signature that goes far past the character limit you're using, that is an indication that the function has too many parameters.

Why does PyCharm use 120 Character Lines even though PEP8 Specifies 79?

PEP8 clearly specifies 79 characters, however, PyCharm defaults to 120 and gives me the warning "PEP8: line too long (... > 120 characters)".
Did previous versions of PEP8 use 120 and PyCharm not update its PEP8 checker? I couldn't find any previous versions of the PEP8 Guide, however, I can easily find previous version of the PEP8 Python scripts.
I'm starting a new Python project and I'm not sure which to use.
References:
http://legacy.python.org/dev/peps/pep-0008/
PyCharm is built on top of IntelliJ. IntelliJ has a default line length of 120 characters.
This is probably because you can't fit a common Java name like: #annotated public static MyObjectFactoryFactory enterpriseObjectFactoryFactoryBuilderPattern { in a mere 80 character line. (I'm poking fun, but Java names do tend to be longer by convention).
The pep8 checker is configurable, so you can specify a better max line length - like 79 characters.
The error is misleading because the pep8 checker formats the text with something like "PEP8: line too long(... > %s characters)" % max_line_setting. So it's using the pep8 checker, with a specific configuration, not claiming that pep8 specifies a 120 character line.
If you want to remove the limit warning altogether you can take the following steps:
In PyCharm, click File > Settings
In the project settings section, click Editor > Inspections
In the list that appears, expand Python
Under Python, scroll down and click "PEP8 coding style violation"
Click the + button next to "Ignore errors" in the bottom right
Type out E501 and click Apply and/or OK
Sources:
http://iambigblind.blogspot.com/2013/02/configuring-pep8py-support-in-pycharm-27.html
https://intellij-support.jetbrains.com/hc/en-us/community/posts/205816889-Disable-individual-PEP8-style-checking-line-length-
AFAIK, PEP8 has never allowed 120 characters, but not everyone follows PEP8.
To answer your question: stay under 80 characters, both from common courtesy and good sense.

Editing Python in Emacs

I've looked at python-mode and python.el. I'm using python-mode.el. I'm also using rope and ropemacs. I'm looking either for some documentation on these that helps me, or another elisp package or something.
My current problem is that the code I'm given has inconsistent indentation sizes. For some blocks it will be two, for some it will be 4. I want to clean this up, but For some reason, when I tell emacs "fix indentation" it just hits tab on every line basically, which screws up the code. What I want is to keep the same relative indentation, but standardize on 4 spaces. Will anything let me do this easily?
I suppose I could find each instance of bad indentation, block it, and query-replace 2 spaces with 4 spaces. But that relies a bit too much on my precision, noticing where this should be done. Also, it's a lot of code.
Someone told me that bicycle repairman would solve this, but that's been out of developement for several years now... Any other suggestions?
thanks.
Assuming you've used Sven Marnach's comment to clean up the code base, I'm guessing you just need to make python-mode.el use the indentation style that you prefer?
Look at the variables py-indent-offset, and py-smart-indentation (and perhaps also py-continuation-offset and py-honor-comment-indentation). As well as the normal indent-tabs-mode.
You can either Customize them (M-x customize-group RET python RET), or add a custom function to python-mode-hook. e.g.:
(add-hook 'python-mode-hook 'my-python-mode-hook)
(defun my-python-mode-hook ()
(setq indent-tabs-mode nil
py-smart-indentation nil
py-indent-offset 4))
with py-smart-indentation set to `t'
current python-mode.el should do it.
http://launchpad.net/python-mode
In case of a bug, please report there
(custom-set-variables
...
'(indent-tabs-mode nil)
'(tab-stop-list (quote (4 8 16 24 32 40 48 56 64 72 80 88 96 104 112 120)))
'(tab-width 4))
Obviously these are global settings for modes that honor them (which python-mode does). I didn't fudge around with python-mode's indentation settings at all.

Categories