From e4ea6c9b2e669bcd7dba4dbecfd6af080b6acf99 Mon Sep 17 00:00:00 2001 From: Souvik Mandal Date: Fri, 10 Jan 2020 21:13:11 +0530 Subject: [PATCH 1/3] DOC: Move import conventions from wiki to docs #30808 --- doc/source/development/code_style.rst | 39 +++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/doc/source/development/code_style.rst b/doc/source/development/code_style.rst index 2fc2f1fb6ee8d..7191c37c6a05b 100644 --- a/doc/source/development/code_style.rst +++ b/doc/source/development/code_style.rst @@ -127,3 +127,42 @@ For example: value = str f"Unknown recived type, got: '{type(value).__name__}'" + + +Imports (aim for absolute) +========================== + +In Python 3, absolute imports are recommended. In absolute import doing something +like ``import string`` will import the string module rather than ``string.py`` +in the same directory. As much as possible, you should try to write out +absolute imports that show the whole import chain from toplevel pandas. + +For example: + +**Good:** + +.. code-block:: python + + import pandas.core.common as com + +Explicit relative imports are also supported in Python 3. In test code, it might +be easier to just reference local variables with explicit relative imports +(that start with ``.``) for clarity, but in other code better to be absolute. + +For example: + +**Good (Test Code Only):** + +.. code-block:: python + + from .common import test_base + +Implicit relative imports should never be used and is removed in Python 3. + +For example: + +**Wrong:** + +.. code-block:: python + + from common import test_base From 6ef08b644472cd5cc6312ee1c4ebbe0d89e88e9a Mon Sep 17 00:00:00 2001 From: Souvik Mandal Date: Sat, 11 Jan 2020 16:49:57 +0530 Subject: [PATCH 2/3] fix unused import warning --- doc/source/development/code_style.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/source/development/code_style.rst b/doc/source/development/code_style.rst index 7191c37c6a05b..f2701f93a1c33 100644 --- a/doc/source/development/code_style.rst +++ b/doc/source/development/code_style.rst @@ -141,7 +141,7 @@ For example: **Good:** -.. code-block:: python +:: import pandas.core.common as com @@ -153,7 +153,7 @@ For example: **Good (Test Code Only):** -.. code-block:: python +:: from .common import test_base @@ -163,6 +163,6 @@ For example: **Wrong:** -.. code-block:: python +:: from common import test_base From 291c5359f9db315f78c278995470564f249e57a3 Mon Sep 17 00:00:00 2001 From: Souvik Mandal Date: Sun, 12 Jan 2020 00:53:44 +0530 Subject: [PATCH 3/3] update import preference for explicit relative imports --- doc/source/development/code_style.rst | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/doc/source/development/code_style.rst b/doc/source/development/code_style.rst index f2701f93a1c33..a295038b5a0bd 100644 --- a/doc/source/development/code_style.rst +++ b/doc/source/development/code_style.rst @@ -137,32 +137,19 @@ like ``import string`` will import the string module rather than ``string.py`` in the same directory. As much as possible, you should try to write out absolute imports that show the whole import chain from toplevel pandas. -For example: +Explicit relative imports are also supported in Python 3. But it is not +recommended to use it. Implicit relative imports should never be used +and is removed in Python 3. -**Good:** +For example: :: + # preferred import pandas.core.common as com -Explicit relative imports are also supported in Python 3. In test code, it might -be easier to just reference local variables with explicit relative imports -(that start with ``.``) for clarity, but in other code better to be absolute. - -For example: - -**Good (Test Code Only):** - -:: - + # not preferred from .common import test_base -Implicit relative imports should never be used and is removed in Python 3. - -For example: - -**Wrong:** - -:: - + # wrong from common import test_base