This is an automated email from the ASF dual-hosted git repository.
andrewzhaoluo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm.git
The following commit(s) were added to refs/heads/main by this push:
new fb5e9c9cad [CI][Lint] Disable no-else-return check in pylint (#11327)
fb5e9c9cad is described below
commit fb5e9c9cad7bfa949b1fb71dbfd12b955d1c668e
Author: Eric Lunderberg <[email protected]>
AuthorDate: Fri Jun 24 13:53:18 2022 -0500
[CI][Lint] Disable no-else-return check in pylint (#11327)
* [CI][Lint] Disabled no-else-return check in pylint
* Line breaks and alphabetical order for readability
* Added description of reasoning/style in the code_guide
---
docs/contribute/code_guide.rst | 35 +++++++++++++++++++++++++++++++++++
tests/lint/pylintrc | 27 ++++++++++++++++++++++++++-
2 files changed, 61 insertions(+), 1 deletion(-)
diff --git a/docs/contribute/code_guide.rst b/docs/contribute/code_guide.rst
index a7137297f1..3849b795f6 100644
--- a/docs/contribute/code_guide.rst
+++ b/docs/contribute/code_guide.rst
@@ -89,6 +89,41 @@ Python Code Styles
- Check your code style using ``python tests/scripts/ci.py lint``
- Stick to language features in ``python 3.7``
+- For functions with early returns, prefer ``if``/``elif``/``else`
+ chains for functions with parallel and short bodies to the
+ conditions, such as functions that apply a simple mapping to the
+ arguments. For more procedural functions, especially where the
+ final ``else`` block would be much longer than the ``if`` and
+ ``elif`` blocks, prefer having the final ``else`` case unindented.
+
+ The pylint check ``no-else-return`` is disabled to allow for this
+ distinction. See further discussion `here
+ <https://github.com/apache/tvm/pull/11327>`.
+
+ .. code:: python
+
+ # All cases have bodies with similar flow control. While this could
+ # be expressed as a sequence of if conditions, a reader would need to
+ # inspect the body of each condition to know that only one conditional
+ # body may be reached.
+ def sign(x):
+ if x > 0:
+ return "+"
+ elif x < 0:
+ return "-"
+ else:
+ return ""
+
+ # The initial special case is an early return for a special case,
+ # followed by a more general method. Using an else block for the
+ # condition would add unnecessary indentation for the remainder of the
+ # function.
+ def num_unique_subsets(values):
+ if len(values)==0:
+ return 1
+
+ # Longer, more general solution here
+ ...
Writing Python Tests
--------------------
diff --git a/tests/lint/pylintrc b/tests/lint/pylintrc
index bf9539cb0b..b91f642496 100644
--- a/tests/lint/pylintrc
+++ b/tests/lint/pylintrc
@@ -82,7 +82,32 @@ enable=indexing-exception,old-raise-syntax
# --enable=similarities". If you want to run only the classes checker, but have
# no Warning level messages displayed, use"--disable=all --enable=classes
# --disable=W"
-disable=design,similarities,no-self-use,attribute-defined-outside-init,locally-disabled,star-args,pointless-except,bad-option-value,global-statement,fixme,suppressed-message,useless-suppression,locally-enabled,no-member,no-name-in-module,import-error,unsubscriptable-object,unbalanced-tuple-unpacking,undefined-variable,protected-access,useless-object-inheritance,consider-using-get,bad-continuation,too-many-lines
+disable=
+ attribute-defined-outside-init,
+ bad-continuation,
+ bad-option-value,
+ consider-using-get,
+ design,
+ fixme,
+ global-statement,
+ import-error,
+ locally-disabled,
+ locally-enabled,
+ no-else-return,
+ no-member,
+ no-name-in-module,
+ no-self-use,
+ pointless-except,
+ protected-access,
+ similarities,
+ star-args,
+ suppressed-message,
+ too-many-lines,
+ unbalanced-tuple-unpacking,
+ undefined-variable,
+ unsubscriptable-object,
+ useless-object-inheritance,
+ useless-suppression
[REPORTS]