You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "Lee-W (via GitHub)" <gi...@apache.org> on 2023/07/05 11:27:14 UTC

[GitHub] [airflow] Lee-W opened a new pull request, #32370: build(pre-commit): check deferrable default value

Lee-W opened a new pull request, #32370:
URL: https://github.com/apache/airflow/pull/32370

   <!--
    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.
    -->
   
   <!--
   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 an 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/main/CONTRIBUTING.rst#pull-request-guidelines)** for more information.
   In case of fundamental code changes, an Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+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 a newsfragment file, named `{pr_number}.significant.rst` or `{issue_number}.significant.rst`, in [newsfragments](https://github.com/apache/airflow/tree/main/newsfragments).
   


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

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

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


[GitHub] [airflow] potiuk merged pull request #32370: build(pre-commit): check deferrable default value

Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk merged PR #32370:
URL: https://github.com/apache/airflow/pull/32370


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

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

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


[GitHub] [airflow] uranusjr commented on a diff in pull request #32370: build(pre-commit): check deferrable default value

Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #32370:
URL: https://github.com/apache/airflow/pull/32370#discussion_r1253372181


##########
scripts/ci/pre_commit/pre_commit_check_deferrable_default.py:
##########
@@ -0,0 +1,87 @@
+#!/usr/bin/env python
+#
+# 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 __future__ import annotations
+
+import ast
+import glob
+import itertools
+import os
+import sys
+
+ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, os.pardir))
+
+
+def _is_valid_deferrable_default(default: ast.AST) -> bool:
+    """Check whether default is 'conf.getboolean("operators", "default_deferrable", fallback=False)'"""
+
+    return (
+        isinstance(default, ast.Call)
+        and default.func.value.id == "conf"  # type: ignore[attr-defined]
+        and default.func.attr == "getboolean"  # type: ignore[attr-defined]
+        and len(default.args) == 2
+        and default.args[0].value == "operators"  # type: ignore[attr-defined]
+        and default.args[1].value == "default_deferrable"  # type: ignore[attr-defined]
+        and len(default.keywords) == 1
+        and default.keywords[0].value.value is False  # type: ignore[attr-defined]
+    )
+
+
+def check_deferrable_default(module_filename: str) -> bool:
+    ast_obj = ast.parse(open(module_filename).read())
+    cls_nodes = (node for node in ast.iter_child_nodes(ast_obj) if isinstance(node, ast.ClassDef))
+    init_method_nodes = (
+        node
+        for cls_node in cls_nodes
+        for node in ast.iter_child_nodes(cls_node)
+        if isinstance(node, ast.FunctionDef) and node.name == "__init__"
+    )
+
+    invalid_value_exists: bool = False
+
+    for node in init_method_nodes:
+        args = node.args
+        arguments = reversed([*args.args, *args.kwonlyargs])
+        defaults = reversed([*args.defaults, *args.kw_defaults])
+        for argument, default in itertools.zip_longest(arguments, defaults, fillvalue=None):
+            if argument is None or default is None:
+                continue
+            if argument.arg == "deferrable":
+                if not _is_valid_deferrable_default(default):
+                    print(
+                        f"{module_filename}:{default.lineno}\n"
+                        "Incorrect deferrable default value\n"
+                        "Please set the default value of deferrbale to "
+                        """"conf.getboolean("operators", "default_deferrable", fallback=False)"\n"""
+                        "See: https://github.com/apache/airflow/blob/main/docs/apache-airflow/authoring-and-scheduling/deferring.rst#writing-deferrable-operators\n"
+                    )
+                    invalid_value_exists = True
+    return invalid_value_exists
+
+
+if __name__ == "__main__":
+    modules = glob.glob(f"{ROOT_DIR}/airflow/**/sensors/**.py", recursive=True)
+    modules.extend(glob.glob(f"{ROOT_DIR}/airflow/**/operators/**.py", recursive=True))

Review Comment:
   ```suggestion
       modules = [
           *glob.glob(f"{ROOT_DIR}/airflow/**/sensors/**.py", recursive=True),
           *glob.glob(f"{ROOT_DIR}/airflow/**/operators/**.py", recursive=True),
       ]
   ```



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

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

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


[GitHub] [airflow] potiuk commented on pull request #32370: build(pre-commit): check deferrable default value

Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk commented on PR #32370:
URL: https://github.com/apache/airflow/pull/32370#issuecomment-1624420027

   Seems the checks are failing now :( 


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

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

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


[GitHub] [airflow] uranusjr commented on a diff in pull request #32370: build(pre-commit): check deferrable default value

Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #32370:
URL: https://github.com/apache/airflow/pull/32370#discussion_r1253372181


##########
scripts/ci/pre_commit/pre_commit_check_deferrable_default.py:
##########
@@ -0,0 +1,87 @@
+#!/usr/bin/env python
+#
+# 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 __future__ import annotations
+
+import ast
+import glob
+import itertools
+import os
+import sys
+
+ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, os.pardir))
+
+
+def _is_valid_deferrable_default(default: ast.AST) -> bool:
+    """Check whether default is 'conf.getboolean("operators", "default_deferrable", fallback=False)'"""
+
+    return (
+        isinstance(default, ast.Call)
+        and default.func.value.id == "conf"  # type: ignore[attr-defined]
+        and default.func.attr == "getboolean"  # type: ignore[attr-defined]
+        and len(default.args) == 2
+        and default.args[0].value == "operators"  # type: ignore[attr-defined]
+        and default.args[1].value == "default_deferrable"  # type: ignore[attr-defined]
+        and len(default.keywords) == 1
+        and default.keywords[0].value.value is False  # type: ignore[attr-defined]
+    )
+
+
+def check_deferrable_default(module_filename: str) -> bool:
+    ast_obj = ast.parse(open(module_filename).read())
+    cls_nodes = (node for node in ast.iter_child_nodes(ast_obj) if isinstance(node, ast.ClassDef))
+    init_method_nodes = (
+        node
+        for cls_node in cls_nodes
+        for node in ast.iter_child_nodes(cls_node)
+        if isinstance(node, ast.FunctionDef) and node.name == "__init__"
+    )
+
+    invalid_value_exists: bool = False
+
+    for node in init_method_nodes:
+        args = node.args
+        arguments = reversed([*args.args, *args.kwonlyargs])
+        defaults = reversed([*args.defaults, *args.kw_defaults])
+        for argument, default in itertools.zip_longest(arguments, defaults, fillvalue=None):
+            if argument is None or default is None:
+                continue
+            if argument.arg == "deferrable":
+                if not _is_valid_deferrable_default(default):
+                    print(
+                        f"{module_filename}:{default.lineno}\n"
+                        "Incorrect deferrable default value\n"
+                        "Please set the default value of deferrbale to "
+                        """"conf.getboolean("operators", "default_deferrable", fallback=False)"\n"""
+                        "See: https://github.com/apache/airflow/blob/main/docs/apache-airflow/authoring-and-scheduling/deferring.rst#writing-deferrable-operators\n"
+                    )
+                    invalid_value_exists = True
+    return invalid_value_exists
+
+
+if __name__ == "__main__":
+    modules = glob.glob(f"{ROOT_DIR}/airflow/**/sensors/**.py", recursive=True)
+    modules.extend(glob.glob(f"{ROOT_DIR}/airflow/**/operators/**.py", recursive=True))

Review Comment:
   ```suggestion
       modules = [
           *glob.glob(f"{ROOT_DIR}/airflow/**/sensors/**.py", recursive=True),
           *glob.glob(f"{ROOT_DIR}/airflow/**/operators/**.py", recursive=True),
       ]
   ```
   
   Or `itertools.chain` might be even better since we only do a `for` on this anyway.



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

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

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


[GitHub] [airflow] Lee-W commented on pull request #32370: build(pre-commit): check deferrable default value

Posted by "Lee-W (via GitHub)" <gi...@apache.org>.
Lee-W commented on PR #32370:
URL: https://github.com/apache/airflow/pull/32370#issuecomment-1621894151

   @potiuk Just finished the implementation. I use it to detect 2 operators with `deferrable = False` and fix in this PR.


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

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

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


[GitHub] [airflow] Lee-W commented on a diff in pull request #32370: build(pre-commit): check deferrable default value

Posted by "Lee-W (via GitHub)" <gi...@apache.org>.
Lee-W commented on code in PR #32370:
URL: https://github.com/apache/airflow/pull/32370#discussion_r1253788944


##########
scripts/ci/pre_commit/pre_commit_check_deferrable_default.py:
##########
@@ -0,0 +1,87 @@
+#!/usr/bin/env python
+#
+# 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 __future__ import annotations
+
+import ast
+import glob
+import itertools
+import os
+import sys
+
+ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, os.pardir))
+
+
+def _is_valid_deferrable_default(default: ast.AST) -> bool:
+    """Check whether default is 'conf.getboolean("operators", "default_deferrable", fallback=False)'"""
+
+    return (
+        isinstance(default, ast.Call)
+        and default.func.value.id == "conf"  # type: ignore[attr-defined]
+        and default.func.attr == "getboolean"  # type: ignore[attr-defined]
+        and len(default.args) == 2
+        and default.args[0].value == "operators"  # type: ignore[attr-defined]
+        and default.args[1].value == "default_deferrable"  # type: ignore[attr-defined]
+        and len(default.keywords) == 1
+        and default.keywords[0].value.value is False  # type: ignore[attr-defined]
+    )
+
+
+def check_deferrable_default(module_filename: str) -> bool:
+    ast_obj = ast.parse(open(module_filename).read())
+    cls_nodes = (node for node in ast.iter_child_nodes(ast_obj) if isinstance(node, ast.ClassDef))
+    init_method_nodes = (
+        node
+        for cls_node in cls_nodes
+        for node in ast.iter_child_nodes(cls_node)
+        if isinstance(node, ast.FunctionDef) and node.name == "__init__"
+    )
+
+    invalid_value_exists: bool = False
+
+    for node in init_method_nodes:
+        args = node.args
+        arguments = reversed([*args.args, *args.kwonlyargs])
+        defaults = reversed([*args.defaults, *args.kw_defaults])
+        for argument, default in itertools.zip_longest(arguments, defaults, fillvalue=None):
+            if argument is None or default is None:
+                continue
+            if argument.arg == "deferrable":
+                if not _is_valid_deferrable_default(default):
+                    print(
+                        f"{module_filename}:{default.lineno}\n"
+                        "Incorrect deferrable default value\n"
+                        "Please set the default value of deferrbale to "
+                        """"conf.getboolean("operators", "default_deferrable", fallback=False)"\n"""
+                        "See: https://github.com/apache/airflow/blob/main/docs/apache-airflow/authoring-and-scheduling/deferring.rst#writing-deferrable-operators\n"
+                    )
+                    invalid_value_exists = True
+    return invalid_value_exists
+
+
+if __name__ == "__main__":
+    modules = glob.glob(f"{ROOT_DIR}/airflow/**/sensors/**.py", recursive=True)
+    modules.extend(glob.glob(f"{ROOT_DIR}/airflow/**/operators/**.py", recursive=True))

Review Comment:
   Thanks! Just updated it with `itertools.chain`



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

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

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


[GitHub] [airflow] potiuk commented on pull request #32370: build(pre-commit): check deferrable default value

Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk commented on PR #32370:
URL: https://github.com/apache/airflow/pull/32370#issuecomment-1621571610

   looks good :). Let's see how it works :) 


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

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

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


[GitHub] [airflow] potiuk commented on pull request #32370: build(pre-commit): check deferrable default value

Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk commented on PR #32370:
URL: https://github.com/apache/airflow/pull/32370#issuecomment-1621895984

   > @potiuk Just finished the implementation. I use it to detect 2 operators with `deferrable = False` and fix in this PR.
   
   That was my hope that we will get something like that to test it :)


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

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

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


[GitHub] [airflow] uranusjr commented on pull request #32370: build(pre-commit): check deferrable default value

Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on PR #32370:
URL: https://github.com/apache/airflow/pull/32370#issuecomment-1622913949

   One more change to make the doc URL only appear once, not for every error detected.


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

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

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