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/12/01 04:04:46 UTC

[GitHub] [airflow] dimberman opened a new pull request #12730: Add DBApiHook check for 2.0 migration

dimberman opened a new pull request #12730:
URL: https://github.com/apache/airflow/pull/12730


   Adds a check that ensures that any hook that uses the
   run, get_pandas_df or get_records functions does not import from the
   base_hook
   
   <!--
   Thank you for contributing! Please make sure that your code changes
   are covered with tests. And in case of new features or big changes
   remember to adjust the documentation.
   
   Feel free to ping committers for the review!
   
   In case of existing issue, reference it using one of the following:
   
   closes: #ISSUE
   related: #ISSUE
   
   How to write a good git commit message:
   http://chris.beams.io/posts/git-commit/
   -->
   
   ---
   **^ Add meaningful description above**
   
   Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/master/CONTRIBUTING.rst#pull-request-guidelines)** for more information.
   In case of fundamental code change, Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvements+Proposals)) is needed.
   In case of a new dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
   In case of backwards incompatible changes please leave a note in [UPDATING.md](https://github.com/apache/airflow/blob/master/UPDATING.md).
   


----------------------------------------------------------------
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



[GitHub] [airflow] dimberman commented on a change in pull request #12730: Add DBApiHook check for 2.0 migration

Posted by GitBox <gi...@apache.org>.
dimberman commented on a change in pull request #12730:
URL: https://github.com/apache/airflow/pull/12730#discussion_r539451362



##########
File path: airflow/upgrade/rules/db_api_functions.py
##########
@@ -0,0 +1,97 @@
+# 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 airflow.hooks.base_hook import BaseHook
+from airflow.upgrade.rules.base_rule import BaseRule
+
+
+def check_get_pandas_df(cls):
+    try:
+        cls.__new__(cls).get_pandas_df("fake SQL")
+        return return_error_string(cls, "get_pandas_df")
+    except NotImplementedError:
+        pass
+    except Exception:
+        return return_error_string(cls, "get_pandas_df")
+
+
+def check_run(cls):
+    try:
+        cls.__new__(cls).run("fake SQL")
+        return return_error_string(cls, "run")
+    except NotImplementedError:
+        pass
+    except Exception:
+        return return_error_string(cls, "run")
+
+
+def check_get_records(cls):
+    try:
+        cls.__new__(cls).get_records("fake SQL")
+        return return_error_string(cls, "get_records")
+    except NotImplementedError:
+        pass
+    except Exception:
+        return return_error_string(cls, "get_records")
+
+
+def return_error_string(cls, method):
+    return (
+        "Class {} incorrectly implements the function {} while inheriting from BaseHook. "
+        "Please make this class inherit from airflow.hooks.db_api_hook.DbApiHook instead".format(
+            cls, method
+        )
+    )
+
+
+def get_all_non_dbapi_children():
+    basehook_children = [
+        child for child in BaseHook.__subclasses__() if child.__name__ != "DbApiHook"
+    ]
+    res = basehook_children[:]
+    while basehook_children:
+        next_generation = []
+        for child in basehook_children:
+            subclasses = child.__subclasses__()
+            if subclasses:
+                next_generation.extend(subclasses)
+        res.extend(next_generation)
+        basehook_children = next_generation
+    return res
+
+
+class DbApiRule(BaseRule):
+    title = "Hooks that run DB functions must inherit from DBApiHook"
+
+    description = (
+        "Hooks that run DB functions must inherit from DBApiHook instead of BaseHook"
+    )
+
+    def check(self):
+        basehook_subclasses = get_all_non_dbapi_children()
+        incorrect_implementations = []
+        for child in basehook_subclasses:
+            pandas_df = check_get_pandas_df(child)
+            if pandas_df:
+                incorrect_implementations.append(pandas_df)
+            run = check_run(child)
+            if run:
+                incorrect_implementations.append(run)
+            get_records = check_get_records(child)
+            if get_records:
+                incorrect_implementations.append(get_records)
+        return incorrect_implementations

Review comment:
       @turbaszek are there operators that inherit from BaseHook? What would an example operator be?
   
   I think it should work with anything i the globals




----------------------------------------------------------------
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



[GitHub] [airflow] github-actions[bot] commented on pull request #12730: Add DBApiHook check for 2.0 migration

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #12730:
URL: https://github.com/apache/airflow/pull/12730#issuecomment-736325579


   The PR most likely needs to run full matrix of tests because it modifies parts of the core of Airflow. However, committers might decide to merge it quickly and take the risk. If they don't merge it quickly - please rebase it to the latest master at your convenience, or amend the last commit of the PR, and push it with --force-with-lease.


----------------------------------------------------------------
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



[GitHub] [airflow] dimberman merged pull request #12730: Add DBApiHook check for 2.0 migration

Posted by GitBox <gi...@apache.org>.
dimberman merged pull request #12730:
URL: https://github.com/apache/airflow/pull/12730


   


----------------------------------------------------------------
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



[GitHub] [airflow] turbaszek commented on a change in pull request #12730: Add DBApiHook check for 2.0 migration

Posted by GitBox <gi...@apache.org>.
turbaszek commented on a change in pull request #12730:
URL: https://github.com/apache/airflow/pull/12730#discussion_r538845618



##########
File path: airflow/upgrade/rules/db_api_functions.py
##########
@@ -0,0 +1,97 @@
+# 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 airflow.hooks.base_hook import BaseHook
+from airflow.upgrade.rules.base_rule import BaseRule
+
+
+def check_get_pandas_df(cls):
+    try:
+        cls.__new__(cls).get_pandas_df("fake SQL")
+        return return_error_string(cls, "get_pandas_df")
+    except NotImplementedError:
+        pass
+    except Exception:
+        return return_error_string(cls, "get_pandas_df")
+
+
+def check_run(cls):
+    try:
+        cls.__new__(cls).run("fake SQL")
+        return return_error_string(cls, "run")
+    except NotImplementedError:
+        pass
+    except Exception:
+        return return_error_string(cls, "run")
+
+
+def check_get_records(cls):
+    try:
+        cls.__new__(cls).get_records("fake SQL")
+        return return_error_string(cls, "get_records")
+    except NotImplementedError:
+        pass
+    except Exception:
+        return return_error_string(cls, "get_records")
+
+
+def return_error_string(cls, method):
+    return (
+        "Class {} incorrectly implements the function {} while inheriting from BaseHook. "
+        "Please make this class inherit from airflow.hooks.db_api_hook.DbApiHook instead".format(
+            cls, method
+        )
+    )
+
+
+def get_all_non_dbapi_children():
+    basehook_children = [
+        child for child in BaseHook.__subclasses__() if child.__name__ != "DbApiHook"
+    ]
+    res = basehook_children[:]
+    while basehook_children:
+        next_generation = []
+        for child in basehook_children:
+            subclasses = child.__subclasses__()
+            if subclasses:
+                next_generation.extend(subclasses)
+        res.extend(next_generation)
+        basehook_children = next_generation
+    return res
+
+
+class DbApiRule(BaseRule):
+    title = "Hooks that run DB functions must inherit from DBApiHook"
+
+    description = (
+        "Hooks that run DB functions must inherit from DBApiHook instead of BaseHook"
+    )
+
+    def check(self):
+        basehook_subclasses = get_all_non_dbapi_children()
+        incorrect_implementations = []
+        for child in basehook_subclasses:
+            pandas_df = check_get_pandas_df(child)
+            if pandas_df:
+                incorrect_implementations.append(pandas_df)
+            run = check_run(child)
+            if run:
+                incorrect_implementations.append(run)
+            get_records = check_get_records(child)
+            if get_records:
+                incorrect_implementations.append(get_records)
+        return incorrect_implementations

Review comment:
       This rule only verifies custom hooks but not operators that may use `BaseHook` methods. Btw. will it work with hooks defined on PYTHONPATH or in plugins?




----------------------------------------------------------------
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