You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2020/08/07 07:57:12 UTC

[GitHub] [airflow] potiuk commented on a change in pull request #10207: Pylint checks should be way faster now

potiuk commented on a change in pull request #10207:
URL: https://github.com/apache/airflow/pull/10207#discussion_r466883568



##########
File path: tests/airflow_pylint/disable_checks_for_tests.py
##########
@@ -0,0 +1,60 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+
+from astroid import MANAGER, scoped_nodes
+from pylint.lint import PyLinter
+
+DISABLED_CHECKS_FOR_TESTS = \
+    "missing-docstring, no-self-use, too-many-public-methods, protected-access, do-not-use-asserts"
+
+
+def register(_: PyLinter):
+    """
+    Skip registering any plugin. This is not a real plugin - we only need it to register transform before
+    running pylint.
+
+    :param _:
+    :return:
+    """
+
+
+def transform(mod):
+    """
+    It's a small hack but one that gives us a lot of speedup in pylint tests. We are replacing the first
+    line of the file with pylint-disable (or update existing one) when file name start with `test_` or
+    (for providers) when it is the full path of the package (both cases occur in pylint)
+
+    :param mod: astroid module
+    :return: None
+    """
+    if mod.name.startswith("test_") or \
+            mod.name.startswith("tests.") or \
+            mod.name.startswith("kubernetes_tests."):
+        decoded_lines = mod.stream().read().decode("utf-8").split("\n")
+        if decoded_lines[0].startswith("# pylint: disable="):
+            decoded_lines[0] = decoded_lines[0] + " " + DISABLED_CHECKS_FOR_TESTS
+        elif decoded_lines[0].startswith("#") or decoded_lines[0].strip() == "":
+            decoded_lines[0] = "# pylint: disable=" + DISABLED_CHECKS_FOR_TESTS
+        else:
+            raise Exception(f"The first line of module {mod.name} is not a comment or empty. "
+                            f"Please make sure it is!")
+        # pylint will read from `.file_bytes` attribute later when tokenization
+        mod.file_bytes = "\n".join(decoded_lines).encode("utf-8")
+
+
+MANAGER.register_transform(scoped_nodes.Module, transform)

Review comment:
       This is exactly what we did before and that was a pain.  It took almost 2 x time than this one.
   This is a huge optimization, that's why I did it.
   
   The problem is that when you run pylint for tests, it also pulls in all the tested code which means that basically a lot of the main airflow code was parsed and processed twice - once in the "pylint main" and once in the "pylint tests". This code was not verified by Pylint but it was parsed and analyzed so that the test code could be tested so it's not a full duplication, but I thin the savings are significant
   
   Some random stats:
   
   Before the change:
   
   pylint main: 6m 20s 
   pylint tests: 3:59s 
   
   Tota: 10m 20s
   
   After the change: 
   combined pylint: 8:20s
   
   
   So we save 20% of elapsed time by doing this.
   
   
   




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org