From da87aefa301bbcb879393dbf0eb74bc0f8da62be Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Sat, 7 Dec 2019 13:15:56 +0200 Subject: [PATCH 1/8] First draft --- doc/source/development/contributing.rst | 4 +- .../development/contributing_code_style.rst | 199 ++++++++++++++++++ 2 files changed, 201 insertions(+), 2 deletions(-) create mode 100644 doc/source/development/contributing_code_style.rst diff --git a/doc/source/development/contributing.rst b/doc/source/development/contributing.rst index d7b3e159f8ce7..1c552ad95c842 100644 --- a/doc/source/development/contributing.rst +++ b/doc/source/development/contributing.rst @@ -569,8 +569,8 @@ do not make sudden changes to the code that could have the potential to break a lot of user code as a result, that is, we need it to be as *backwards compatible* as possible to avoid mass breakages. -Additional standards are outlined on the `code style wiki -page `_. +Additional standards are outlined on the (Placeholder as a reminder to ask the +devs for the help on how to properly link the new created file) Optional dependencies --------------------- diff --git a/doc/source/development/contributing_code_style.rst b/doc/source/development/contributing_code_style.rst new file mode 100644 index 0000000000000..fa8b2a86ae4b7 --- /dev/null +++ b/doc/source/development/contributing_code_style.rst @@ -0,0 +1,199 @@ +.. _Not_sure_what_to_put_here: + +{{ header }} + +======================= +pandas code style guide +======================= + +.. contents:: Table of contents: + :local: + +Patterns +======== + +foo.__class__ +------------- + +*pandas* uses 'type(foo)' instead 'foo.__class__' as it is making the code more +readable. + +For example: + +**Good:** + +.. code-block:: python + + cls = type(self) + +**Good:** + +.. code-block:: python + + name = type(self).__name__ + +**Bad:** + +.. code-block:: python + + cls = self.__class__ + +**Bad:** + +.. code-block:: python + + name = self.__class__.__name__ + +String formatting +================= + +Concatenated strings +-------------------- + +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:** + +.. code-block:: python + + my_warning_message = ( + f"Warning, {old_function_name} is deprecated, " + "please use the new and way better " + f"{new_function_name}" + ) + +**Bad:** + +.. code-block:: python + + my_warning_message = ( + f"Warning, {old_function_name} is deprecated, " + f"please use the new and way better " + f"{new_function_name}" + ) + +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:** + +.. code-block:: python + + example_string = ( + "Some long concatenated string, " + "with good placement of the " + "whitespaces" + ) + +**Bad:** + +.. code-block:: python + + example_string = ( + "Some long concatenated string," + " with bad placement of the" + " whitespaces" + ) + +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:** + +.. code-block:: python + + raise ValueError(f"Unknown recived value, got: {repr(value)}") + +**Good:** + +.. code-block:: python + + raise ValueError(f"Unknown recived type, got: '{type(value).__name__}'") + +**Bad:** + +.. code-block:: python + + raise ValueError(f"Unknown recived type, got: {repr(type(value).__name__)}") + +Single and double quotes +------------------------ + +*pandas* uses single quotes when .... + +For example: + +**Good:** + +.. code-block:: python + + placeholder = True + + +**Bad:** + +.. code-block:: python + + placeholder = True + +And using double quotes when.... + +For example: + +**Good:** + +.. code-block:: python + + placeholder = True + + +**Bad:** + +.. code-block:: python + + placeholder = True + +Typing +====== + +Annotating __init__ +------------------- + +*pandas* does not annotate the '__init__' function. it is redundant +as almost every '__init__' function will most like not to return anything. + +For example: + +**Good:** + +.. code-block:: python + + def __init__(self, name): + self.name = name + +**Bad:** + +.. code-block:: python + + def __init__(self, name) -> None: + self.name = name + From e01085e0d2cab5a4c129ebbc206e329bede82b24 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Sun, 29 Dec 2019 16:57:26 +0200 Subject: [PATCH 2/8] Fixes for datapythonista's review --- doc/source/development/contributing.rst | 3 +- ..._style.rst => contributing_code_guide.rst} | 77 ++----------------- 2 files changed, 6 insertions(+), 74 deletions(-) rename doc/source/development/{contributing_code_style.rst => contributing_code_guide.rst} (66%) diff --git a/doc/source/development/contributing.rst b/doc/source/development/contributing.rst index 1c552ad95c842..dd9c9f39ce3a5 100644 --- a/doc/source/development/contributing.rst +++ b/doc/source/development/contributing.rst @@ -569,8 +569,7 @@ do not make sudden changes to the code that could have the potential to break a lot of user code as a result, that is, we need it to be as *backwards compatible* as possible to avoid mass breakages. -Additional standards are outlined on the (Placeholder as a reminder to ask the -devs for the help on how to properly link the new created file) +Additional standards are outlined on the `pandas code style guide `_ Optional dependencies --------------------- diff --git a/doc/source/development/contributing_code_style.rst b/doc/source/development/contributing_code_guide.rst similarity index 66% rename from doc/source/development/contributing_code_style.rst rename to doc/source/development/contributing_code_guide.rst index fa8b2a86ae4b7..57119192789f1 100644 --- a/doc/source/development/contributing_code_style.rst +++ b/doc/source/development/contributing_code_guide.rst @@ -1,4 +1,4 @@ -.. _Not_sure_what_to_put_here: +.. _contributing_code_guide: {{ header }} @@ -121,79 +121,12 @@ For example: .. code-block:: python - raise ValueError(f"Unknown recived value, got: {repr(value)}") + value = str + f"Unknown recived value, got: {repr(value)}" **Good:** .. code-block:: python - raise ValueError(f"Unknown recived type, got: '{type(value).__name__}'") - -**Bad:** - -.. code-block:: python - - raise ValueError(f"Unknown recived type, got: {repr(type(value).__name__)}") - -Single and double quotes ------------------------- - -*pandas* uses single quotes when .... - -For example: - -**Good:** - -.. code-block:: python - - placeholder = True - - -**Bad:** - -.. code-block:: python - - placeholder = True - -And using double quotes when.... - -For example: - -**Good:** - -.. code-block:: python - - placeholder = True - - -**Bad:** - -.. code-block:: python - - placeholder = True - -Typing -====== - -Annotating __init__ -------------------- - -*pandas* does not annotate the '__init__' function. it is redundant -as almost every '__init__' function will most like not to return anything. - -For example: - -**Good:** - -.. code-block:: python - - def __init__(self, name): - self.name = name - -**Bad:** - -.. code-block:: python - - def __init__(self, name) -> None: - self.name = name - + value = str + f"Unknown recived type, got: '{type(value).__name__}'" From 8a7991612427b51732c2fbdb6f0b1a2d010a52ba Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Sun, 29 Dec 2019 17:20:06 +0200 Subject: [PATCH 3/8] Lint issues --- .../development/contributing_code_guide.rst | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/doc/source/development/contributing_code_guide.rst b/doc/source/development/contributing_code_guide.rst index 57119192789f1..36f5d7e7d7900 100644 --- a/doc/source/development/contributing_code_guide.rst +++ b/doc/source/development/contributing_code_guide.rst @@ -24,25 +24,33 @@ For example: .. code-block:: python - cls = type(self) + class MyClass: + def __init__(self): + cls = type(self) **Good:** .. code-block:: python - name = type(self).__name__ + class MyClass: + def __init__(self): + name = type(self).__name__ **Bad:** .. code-block:: python - cls = self.__class__ + class MyClass: + def __init__(self): + cls = self.__class__ **Bad:** .. code-block:: python - name = self.__class__.__name__ + class MyClass: + def __init__(self): + name = self.__class__.__name__ String formatting ================= From cc2d613b79979ee22e53a843b8c48c0281668e74 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Sun, 29 Dec 2019 17:34:09 +0200 Subject: [PATCH 4/8] Removed classes, replaced with foo/bar --- .../development/contributing_code_guide.rst | 25 +++---------------- 1 file changed, 4 insertions(+), 21 deletions(-) diff --git a/doc/source/development/contributing_code_guide.rst b/doc/source/development/contributing_code_guide.rst index 36f5d7e7d7900..f1f39f52d64e1 100644 --- a/doc/source/development/contributing_code_guide.rst +++ b/doc/source/development/contributing_code_guide.rst @@ -24,33 +24,16 @@ For example: .. code-block:: python - class MyClass: - def __init__(self): - cls = type(self) - -**Good:** - -.. code-block:: python - - class MyClass: - def __init__(self): - name = type(self).__name__ + foo = "bar" + type(foo) **Bad:** .. code-block:: python - class MyClass: - def __init__(self): - cls = self.__class__ - -**Bad:** - -.. code-block:: python + foo = "bar" + foo.__class__ - class MyClass: - def __init__(self): - name = self.__class__.__name__ String formatting ================= From 02c0ab52d13dc00623bdf58a08bc51bc5818a2a1 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Tue, 31 Dec 2019 13:02:57 +0200 Subject: [PATCH 5/8] Fixed CI issues --- doc/source/development/contributing_code_guide.rst | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/doc/source/development/contributing_code_guide.rst b/doc/source/development/contributing_code_guide.rst index f1f39f52d64e1..c42b5b7e38fd3 100644 --- a/doc/source/development/contributing_code_guide.rst +++ b/doc/source/development/contributing_code_guide.rst @@ -55,20 +55,26 @@ For example: .. code-block:: python + foo = "old_function" + bar = "new_function" + my_warning_message = ( - f"Warning, {old_function_name} is deprecated, " + f"Warning, {foo} is deprecated, " "please use the new and way better " - f"{new_function_name}" + f"{bar}" ) **Bad:** .. code-block:: python + foo = "old_function" + bar = "new_function" + my_warning_message = ( - f"Warning, {old_function_name} is deprecated, " + f"Warning, {foo} is deprecated, " f"please use the new and way better " - f"{new_function_name}" + f"{bar}" ) White spaces From 635a81b057fb12718d66ba1bc454fa4f09d1572f Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Wed, 1 Jan 2020 15:22:32 +0200 Subject: [PATCH 6/8] Added doctree --- doc/source/development/contributing.rst | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/doc/source/development/contributing.rst b/doc/source/development/contributing.rst index dd9c9f39ce3a5..4681c82c36905 100644 --- a/doc/source/development/contributing.rst +++ b/doc/source/development/contributing.rst @@ -569,7 +569,12 @@ do not make sudden changes to the code that could have the potential to break a lot of user code as a result, that is, we need it to be as *backwards compatible* as possible to avoid mass breakages. -Additional standards are outlined on the `pandas code style guide `_ +Additional standards are outlined on the :ref:`pandas code style guide ` + + .. toctree:: + :maxdepth: 2 + + contributing_code_guide.rst Optional dependencies --------------------- From 90a5a4e3f16b9692d5964708b1e62c3510edba25 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Wed, 1 Jan 2020 16:54:27 +0200 Subject: [PATCH 7/8] Added index as datapythonista suggested --- doc/source/development/contributing.rst | 7 +------ doc/source/development/index.rst | 1 + doc/source/index.rst.template | 1 + 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/doc/source/development/contributing.rst b/doc/source/development/contributing.rst index 4681c82c36905..dd9c9f39ce3a5 100644 --- a/doc/source/development/contributing.rst +++ b/doc/source/development/contributing.rst @@ -569,12 +569,7 @@ do not make sudden changes to the code that could have the potential to break a lot of user code as a result, that is, we need it to be as *backwards compatible* as possible to avoid mass breakages. -Additional standards are outlined on the :ref:`pandas code style guide ` - - .. toctree:: - :maxdepth: 2 - - contributing_code_guide.rst +Additional standards are outlined on the `pandas code style guide `_ Optional dependencies --------------------- diff --git a/doc/source/development/index.rst b/doc/source/development/index.rst index 757b197c717e6..89118723c9c59 100644 --- a/doc/source/development/index.rst +++ b/doc/source/development/index.rst @@ -13,6 +13,7 @@ Development :maxdepth: 2 contributing + contributing_code_guide maintaining internals extending diff --git a/doc/source/index.rst.template b/doc/source/index.rst.template index 9cea68530fbe7..699b0f3dc9851 100644 --- a/doc/source/index.rst.template +++ b/doc/source/index.rst.template @@ -109,6 +109,7 @@ See the :ref:`overview` for more detail about what's in the library. * :doc:`development/index` * :doc:`development/contributing` + * :doc:`development/contributing_code_guide` * :doc:`development/internals` * :doc:`development/extending` * :doc:`development/developer` From b6443b83f725ba85475bfae8ec024ab19a474469 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Wed, 1 Jan 2020 20:41:44 +0200 Subject: [PATCH 8/8] Changed file name to code_style --- .../development/{contributing_code_guide.rst => code_style.rst} | 2 +- doc/source/development/contributing.rst | 2 +- doc/source/development/index.rst | 2 +- doc/source/index.rst.template | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) rename doc/source/development/{contributing_code_guide.rst => code_style.rst} (98%) diff --git a/doc/source/development/contributing_code_guide.rst b/doc/source/development/code_style.rst similarity index 98% rename from doc/source/development/contributing_code_guide.rst rename to doc/source/development/code_style.rst index c42b5b7e38fd3..2fc2f1fb6ee8d 100644 --- a/doc/source/development/contributing_code_guide.rst +++ b/doc/source/development/code_style.rst @@ -1,4 +1,4 @@ -.. _contributing_code_guide: +.. _code_style: {{ header }} diff --git a/doc/source/development/contributing.rst b/doc/source/development/contributing.rst index dd9c9f39ce3a5..4d38a89b97bcb 100644 --- a/doc/source/development/contributing.rst +++ b/doc/source/development/contributing.rst @@ -569,7 +569,7 @@ do not make sudden changes to the code that could have the potential to break a lot of user code as a result, that is, we need it to be as *backwards compatible* as possible to avoid mass breakages. -Additional standards are outlined on the `pandas code style guide `_ +Additional standards are outlined on the `pandas code style guide `_ Optional dependencies --------------------- diff --git a/doc/source/development/index.rst b/doc/source/development/index.rst index 89118723c9c59..f8a6bb6deb52d 100644 --- a/doc/source/development/index.rst +++ b/doc/source/development/index.rst @@ -13,7 +13,7 @@ Development :maxdepth: 2 contributing - contributing_code_guide + code_style maintaining internals extending diff --git a/doc/source/index.rst.template b/doc/source/index.rst.template index 699b0f3dc9851..10705787dfedf 100644 --- a/doc/source/index.rst.template +++ b/doc/source/index.rst.template @@ -109,7 +109,7 @@ See the :ref:`overview` for more detail about what's in the library. * :doc:`development/index` * :doc:`development/contributing` - * :doc:`development/contributing_code_guide` + * :doc:`development/code_style` * :doc:`development/internals` * :doc:`development/extending` * :doc:`development/developer`