This is an automated email from the ASF dual-hosted git repository.

ash pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 86aa5ef27be Fix completion/linting/type checking with VSCode/pyright 
(#43899)
86aa5ef27be is described below

commit 86aa5ef27be40d84c2b0295d66e2dedf48cc5d67
Author: Ash Berlin-Taylor <[email protected]>
AuthorDate: Tue Nov 12 12:09:19 2024 +0000

    Fix completion/linting/type checking with VSCode/pyright (#43899)
    
    Pyright (the type engine powering VSCode's python extension) doesn't treat
    `airflow` as a namespace package because of the `airflow/__init__.py` and it
    doesn't want to/can't support detecting the `__path__ = ...` method of 
making
    it an explicit namespace package, so we are left with no option but to 
create
    Yet Another Stub File.
    
    Tested by pytting `reveal_type(FAB_VERSION); reveal_type(TaskSDKDag)` inside
    `_upgrade_outdated_dag_access_control` in `airflow/model/dag.py` -- before
    this change it was reporting both as Unknown.
    
    And to continue to keep Pycharm happy we have to have the `__path__` stanzas
    without future annotations in all the "empty" files.
    
    Ugly, but at least it works
---
 Dockerfile                                                    |  2 +-
 Dockerfile.ci                                                 |  2 +-
 providers/src/airflow/{providers => }/__init__.py             | 11 ++---------
 providers/src/airflow/providers/__init__.py                   | 11 ++---------
 pyproject.toml                                                |  1 +
 scripts/docker/install_airflow.sh                             |  2 +-
 task_sdk/pyproject.toml                                       |  5 +++++
 .../airflow/providers => task_sdk/src/airflow}/__init__.py    | 11 ++---------
 8 files changed, 15 insertions(+), 30 deletions(-)

diff --git a/Dockerfile b/Dockerfile
index 48d53af815d..5ca9949b021 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -890,7 +890,7 @@ function install_airflow() {
 
         # Similarly we need _a_ file for task_sdk too
         mkdir -p ./task_sdk/src/airflow/sdk/
-        touch ./task_sdk/src/airflow/__init__.py
+        touch ./task_sdk/src/airflow/sdk/__init__.py
 
         trap 'rm -f ./providers/src/airflow/providers/__init__.py 
./task_sdk/src/airflow/__init__.py 2>/dev/null' EXIT
 
diff --git a/Dockerfile.ci b/Dockerfile.ci
index 5adb2d6210a..943270aec69 100644
--- a/Dockerfile.ci
+++ b/Dockerfile.ci
@@ -660,7 +660,7 @@ function install_airflow() {
 
         # Similarly we need _a_ file for task_sdk too
         mkdir -p ./task_sdk/src/airflow/sdk/
-        touch ./task_sdk/src/airflow/__init__.py
+        touch ./task_sdk/src/airflow/sdk/__init__.py
 
         trap 'rm -f ./providers/src/airflow/providers/__init__.py 
./task_sdk/src/airflow/__init__.py 2>/dev/null' EXIT
 
diff --git a/providers/src/airflow/providers/__init__.py 
b/providers/src/airflow/__init__.py
similarity index 61%
copy from providers/src/airflow/providers/__init__.py
copy to providers/src/airflow/__init__.py
index adfa8eb4b98..716fea5d472 100644
--- a/providers/src/airflow/providers/__init__.py
+++ b/providers/src/airflow/__init__.py
@@ -16,13 +16,6 @@
 # specific language governing permissions and limitations
 # under the License.
 
-# We do not use "from __future__ import annotations" here because it is not 
supported
-# by Pycharm when we want to make sure all imports in airflow work from 
namespace packages
-# Adding it automatically is excluded in pyproject.toml via I002 ruff rule 
exclusion
-
-# Make `airflow` a namespace package, supporting installing
-# airflow.providers.* in different locations (i.e. one in site, and one in user
-# lib.)  This is required by some IDEs to resolve the import paths.
-#
-# Note: this file is not installed or distributed in any distribution!
+# Pycharm needs to see this line. VSCode/pyright doesn't care about it, but 
this file needs to exist
+# https://github.com/microsoft/pyright/issues/9439#issuecomment-2468990559
 __path__ = __import__("pkgutil").extend_path(__path__, __name__)  # type: 
ignore
diff --git a/providers/src/airflow/providers/__init__.py 
b/providers/src/airflow/providers/__init__.py
index adfa8eb4b98..716fea5d472 100644
--- a/providers/src/airflow/providers/__init__.py
+++ b/providers/src/airflow/providers/__init__.py
@@ -16,13 +16,6 @@
 # specific language governing permissions and limitations
 # under the License.
 
-# We do not use "from __future__ import annotations" here because it is not 
supported
-# by Pycharm when we want to make sure all imports in airflow work from 
namespace packages
-# Adding it automatically is excluded in pyproject.toml via I002 ruff rule 
exclusion
-
-# Make `airflow` a namespace package, supporting installing
-# airflow.providers.* in different locations (i.e. one in site, and one in user
-# lib.)  This is required by some IDEs to resolve the import paths.
-#
-# Note: this file is not installed or distributed in any distribution!
+# Pycharm needs to see this line. VSCode/pyright doesn't care about it, but 
this file needs to exist
+# https://github.com/microsoft/pyright/issues/9439#issuecomment-2468990559
 __path__ = __import__("pkgutil").extend_path(__path__, __name__)  # type: 
ignore
diff --git a/pyproject.toml b/pyproject.toml
index c5361472fc1..6b23735cb3e 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -317,6 +317,7 @@ testing = ["dev", "providers.tests", "task_sdk.tests", 
"tests_common", "tests"]
 "airflow/models/__init__.py" = ["F401", "TCH004"]
 "airflow/models/sqla_models.py" = ["F401"]
 "providers/src/airflow/providers/__init__.py" = ["I002"]
+"providers/src/airflow/__init__.py" = ["I002"]
 
 # The test_python.py is needed because adding __future__.annotations breaks 
runtime checks that are
 # needed for the test to work
diff --git a/scripts/docker/install_airflow.sh 
b/scripts/docker/install_airflow.sh
index 769279c2cb0..2975c50c2d6 100644
--- a/scripts/docker/install_airflow.sh
+++ b/scripts/docker/install_airflow.sh
@@ -54,7 +54,7 @@ function install_airflow() {
 
         # Similarly we need _a_ file for task_sdk too
         mkdir -p ./task_sdk/src/airflow/sdk/
-        touch ./task_sdk/src/airflow/__init__.py
+        touch ./task_sdk/src/airflow/sdk/__init__.py
 
         trap 'rm -f ./providers/src/airflow/providers/__init__.py 
./task_sdk/src/airflow/__init__.py 2>/dev/null' EXIT
 
diff --git a/task_sdk/pyproject.toml b/task_sdk/pyproject.toml
index 37ea2d300ab..f290dfb17fd 100644
--- a/task_sdk/pyproject.toml
+++ b/task_sdk/pyproject.toml
@@ -33,6 +33,8 @@ build-backend = "hatchling.build"
 
 [tool.hatch.build.targets.wheel]
 packages = ["src/airflow"]
+# This file only exists to make pyright/VSCode happy, don't ship it
+exclude = ["src/airflow/__init__.py"]
 
 [tool.ruff]
 extend = "../pyproject.toml"
@@ -46,6 +48,9 @@ namespace-packages = ["src/airflow"]
 
 "src/airflow/sdk/__init__.py" = ["TCH004"]
 
+# Pycharm barfs if this "stub" file has future imports
+"src/airflow/__init__.py" = ["I002"]
+
 [tool.uv]
 dev-dependencies = [
     "kgb>=7.1.1",
diff --git a/providers/src/airflow/providers/__init__.py 
b/task_sdk/src/airflow/__init__.py
similarity index 61%
copy from providers/src/airflow/providers/__init__.py
copy to task_sdk/src/airflow/__init__.py
index adfa8eb4b98..716fea5d472 100644
--- a/providers/src/airflow/providers/__init__.py
+++ b/task_sdk/src/airflow/__init__.py
@@ -16,13 +16,6 @@
 # specific language governing permissions and limitations
 # under the License.
 
-# We do not use "from __future__ import annotations" here because it is not 
supported
-# by Pycharm when we want to make sure all imports in airflow work from 
namespace packages
-# Adding it automatically is excluded in pyproject.toml via I002 ruff rule 
exclusion
-
-# Make `airflow` a namespace package, supporting installing
-# airflow.providers.* in different locations (i.e. one in site, and one in user
-# lib.)  This is required by some IDEs to resolve the import paths.
-#
-# Note: this file is not installed or distributed in any distribution!
+# Pycharm needs to see this line. VSCode/pyright doesn't care about it, but 
this file needs to exist
+# https://github.com/microsoft/pyright/issues/9439#issuecomment-2468990559
 __path__ = __import__("pkgutil").extend_path(__path__, __name__)  # type: 
ignore

Reply via email to