-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Coding Style Guidline.rst #34608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Coding Style Guidline.rst #34608
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,285 @@ | ||
pandas Coding Style Guide | ||
========================= | ||
|
||
1 Background | ||
------------ | ||
|
||
Writing good code is not just about what you write. It is also about how | ||
you write it. During Continuous Integration testing, several tools will | ||
be run to check your code for stylistic errors. Generating any warnings | ||
will cause the test to fail. Thus, good style is a requirement for | ||
submitting code to *pandas*. This document serves as a supplement to the | ||
script demonstrating the proper coding style required for contributing | ||
to *pandas*. | ||
|
||
2 Patterns | ||
---------- | ||
|
||
2.1 Line Length | ||
~~~~~~~~~~~~~~~ | ||
|
||
Line length is restricted to 80 characters to promote readability. | ||
|
||
2.2 Indentation | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is enforced by black; no need to detail here |
||
~~~~~~~~~~~~~~~ | ||
|
||
Indentations must be multiples of 4 | ||
|
||
For example: | ||
|
||
**Good:** | ||
|
||
:: | ||
|
||
if i == 0 | ||
if x == 3 | ||
x = 10 | ||
|
||
**Bad:** | ||
|
||
:: | ||
|
||
if i == 0 | ||
if x == 3 | ||
x = 10 | ||
|
||
2.3 Whitespaces Around Arithmetic Operators | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same for comment around black |
||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
There must be whitespaces surrounding arithmetic operators | ||
|
||
**Good:** | ||
|
||
:: | ||
|
||
x = 3 + 5 / 2 | ||
|
||
**Bad:** | ||
|
||
:: | ||
|
||
x=3+5/2 | ||
|
||
2.4 Missing White Spaces After Commas | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same for black |
||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
There must be a whitespace following a comma | ||
|
||
**Good:** | ||
|
||
:: | ||
|
||
myFunctionCall('a', 'b', 'c') | ||
|
||
**Bad:** | ||
|
||
:: | ||
|
||
myFunctionCall('a','b','c') | ||
|
||
2.5 Header File | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a Python reference? |
||
~~~~~~~~~~~~~~~ | ||
|
||
Every header file must include a header guard to avoid name collision if | ||
re-included. | ||
|
||
2.6 foo._class\_ | ||
~~~~~~~~~~~~~~~~ | ||
|
||
**pandas** uses ‘type(foo)’ instead ‘foo.__class__’ as it is making the | ||
code more readable. | ||
|
||
For example: | ||
|
||
**Good:** | ||
|
||
:: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you make these code-block:: python directives? |
||
|
||
foo = "bar" | ||
type(foo) | ||
|
||
**Bad:** | ||
|
||
:: | ||
|
||
foo = "bar" | ||
foo.__class__ | ||
|
||
2.7 Bare Pytest Raises | ||
~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
**Good:** | ||
|
||
:: | ||
|
||
with pytest.raise(ValueError, match="foo"): | ||
# following code that raises ValueError | ||
|
||
**Bad:** | ||
|
||
:: | ||
|
||
with pytest.raise(ValueError): | ||
# following code that raises ValueError | ||
|
||
3 String Formatting | ||
------------------- | ||
|
||
3.1 Concatenated Strings | ||
~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
3.1.1 f-strings | ||
^^^^^^^^^^^^^^^ | ||
|
||
*pandas* uses f-strings formatting instead of ‘%’ and ‘.format()’ string | ||
formatters. | ||
|
||
The convention of using f-strings on a string that is concatenated over | ||
serveral lines, is to prefix only the lines containing the value needs | ||
to be interpeted. | ||
|
||
For example: | ||
|
||
**Good:** | ||
|
||
:: | ||
|
||
foo = "old_function" | ||
bar = "new_function" | ||
|
||
my_warning_message = ( | ||
f"Warning, {foo} is deprecated, " | ||
"please use the new and way better " | ||
f"{bar}" | ||
) | ||
|
||
**Bad:** | ||
|
||
:: | ||
|
||
foo = "old_function" | ||
bar = "new_function" | ||
|
||
my_warning_message = ( | ||
f"Warning, {foo} is deprecated, " | ||
f"please use the new and way better " | ||
f"{bar}" | ||
) | ||
|
||
3.1.2 White Spaces | ||
^^^^^^^^^^^^^^^^^^ | ||
|
||
Putting the white space only at the end of the previous line, so there | ||
is no whitespace at the beggining of the concatenated string. | ||
|
||
For example: | ||
|
||
**Good:** | ||
|
||
:: | ||
|
||
example_string = ( | ||
"Some long concatenated string, " | ||
"with good placement of the " | ||
"whitespaces" | ||
) | ||
|
||
**Bad:** | ||
|
||
:: | ||
|
||
example_string = ( | ||
"Some long concatenated string," | ||
" with bad placement of the" | ||
" whitespaces" | ||
) | ||
|
||
3.2 Representation function (aka ‘repr()’) | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
*pandas* uses ‘repr()’ instead of ‘%r’ and ‘!r’. | ||
|
||
The use of ‘repr()’ will only happend when the value is not an obvious | ||
string. | ||
|
||
For example: | ||
|
||
**Good:** | ||
|
||
:: | ||
|
||
value = str | ||
f"Unknown recived value, got: {repr(value)}" | ||
|
||
**Bad:** | ||
|
||
:: | ||
|
||
value = str | ||
f"Unknown recived type, got: '{type(value).__name__}'" | ||
|
||
4 Types | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK so this is already in the contributing guideline. If we want to move here then should remove from that so as not to duplicate |
||
------- | ||
|
||
**pandas** strongly encourages the use of PEP 484 style type hints. New | ||
development should contain type hints and pull requests to annotate | ||
existing code are accepted as well! | ||
|
||
4.1 Imports | ||
~~~~~~~~~~~ | ||
|
||
Types imports should follow the ``from typing import ...`` convention. | ||
|
||
**Good:** | ||
|
||
:: | ||
|
||
from typing import List, Optional, Union | ||
|
||
primes: List[int] = [] | ||
|
||
**Bad:** | ||
|
||
:: | ||
|
||
import typing | ||
|
||
primes: typing.List[int] = [] | ||
|
||
Optional should be used where applicable | ||
|
||
**Good:** | ||
|
||
:: | ||
|
||
maybe_primes: List[Optional[int]] = [] | ||
|
||
**Bad:** | ||
|
||
:: | ||
|
||
maybe_primes: List[Union[int, None]] = [] | ||
|
||
4.1.1 Redundant Imports | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is strictly for writing documentation, but for general code style it is certainly OK to import these. I think can just remove this from this guide There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @WillAyd I made those changes you requested, but it still seems like it is failing after ever carriage line return |
||
^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
Should not import numpy and pandas | ||
|
||
**Example:** | ||
|
||
:: | ||
|
||
import numpy as np | ||
import pandas as pd | ||
|
||
4.1.2 Unused Imports | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is enforced by flake8 already I don't think need to call out here |
||
^^^^^^^^^^^^^^^^^^^^ | ||
|
||
Unused imports must be removed prior to submission | ||
|
||
**Example:** | ||
|
||
:: | ||
|
||
import pandas as pdf | ||
df = pd.DataFrame(np.ones((3, 3)), columns=('a', 'b', 'c')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use a line length of 88