Skip to content

Commit f937843

Browse files
ShaharNavehWillAyd
authored andcommitted
DOC: Code style documentation (#30129)
1 parent 6eabca3 commit f937843

File tree

4 files changed

+132
-2
lines changed

4 files changed

+132
-2
lines changed

doc/source/development/code_style.rst

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
.. _code_style:
2+
3+
{{ header }}
4+
5+
=======================
6+
pandas code style guide
7+
=======================
8+
9+
.. contents:: Table of contents:
10+
:local:
11+
12+
Patterns
13+
========
14+
15+
foo.__class__
16+
-------------
17+
18+
*pandas* uses 'type(foo)' instead 'foo.__class__' as it is making the code more
19+
readable.
20+
21+
For example:
22+
23+
**Good:**
24+
25+
.. code-block:: python
26+
27+
foo = "bar"
28+
type(foo)
29+
30+
**Bad:**
31+
32+
.. code-block:: python
33+
34+
foo = "bar"
35+
foo.__class__
36+
37+
38+
String formatting
39+
=================
40+
41+
Concatenated strings
42+
--------------------
43+
44+
f-strings
45+
~~~~~~~~~
46+
47+
*pandas* uses f-strings formatting instead of '%' and '.format()' string formatters.
48+
49+
The convention of using f-strings on a string that is concatenated over serveral lines,
50+
is to prefix only the lines containing the value needs to be interpeted.
51+
52+
For example:
53+
54+
**Good:**
55+
56+
.. code-block:: python
57+
58+
foo = "old_function"
59+
bar = "new_function"
60+
61+
my_warning_message = (
62+
f"Warning, {foo} is deprecated, "
63+
"please use the new and way better "
64+
f"{bar}"
65+
)
66+
67+
**Bad:**
68+
69+
.. code-block:: python
70+
71+
foo = "old_function"
72+
bar = "new_function"
73+
74+
my_warning_message = (
75+
f"Warning, {foo} is deprecated, "
76+
f"please use the new and way better "
77+
f"{bar}"
78+
)
79+
80+
White spaces
81+
~~~~~~~~~~~~
82+
83+
Putting the white space only at the end of the previous line, so
84+
there is no whitespace at the beggining of the concatenated string.
85+
86+
For example:
87+
88+
**Good:**
89+
90+
.. code-block:: python
91+
92+
example_string = (
93+
"Some long concatenated string, "
94+
"with good placement of the "
95+
"whitespaces"
96+
)
97+
98+
**Bad:**
99+
100+
.. code-block:: python
101+
102+
example_string = (
103+
"Some long concatenated string,"
104+
" with bad placement of the"
105+
" whitespaces"
106+
)
107+
108+
Representation function (aka 'repr()')
109+
--------------------------------------
110+
111+
*pandas* uses 'repr()' instead of '%r' and '!r'.
112+
113+
The use of 'repr()' will only happend when the value is not an obvious string.
114+
115+
For example:
116+
117+
**Good:**
118+
119+
.. code-block:: python
120+
121+
value = str
122+
f"Unknown recived value, got: {repr(value)}"
123+
124+
**Good:**
125+
126+
.. code-block:: python
127+
128+
value = str
129+
f"Unknown recived type, got: '{type(value).__name__}'"

doc/source/development/contributing.rst

+1-2
Original file line numberDiff line numberDiff line change
@@ -569,8 +569,7 @@ do not make sudden changes to the code that could have the potential to break
569569
a lot of user code as a result, that is, we need it to be as *backwards compatible*
570570
as possible to avoid mass breakages.
571571

572-
Additional standards are outlined on the `code style wiki
573-
page <https://github.com/pandas-dev/pandas/wiki/Code-Style-and-Conventions>`_.
572+
Additional standards are outlined on the `pandas code style guide <code_style>`_
574573

575574
Optional dependencies
576575
---------------------

doc/source/development/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Development
1313
:maxdepth: 2
1414

1515
contributing
16+
code_style
1617
maintaining
1718
internals
1819
extending

doc/source/index.rst.template

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ See the :ref:`overview` for more detail about what's in the library.
109109
* :doc:`development/index`
110110

111111
* :doc:`development/contributing`
112+
* :doc:`development/code_style`
112113
* :doc:`development/internals`
113114
* :doc:`development/extending`
114115
* :doc:`development/developer`

0 commit comments

Comments
 (0)