Skip to content

Commit

Permalink
Add get_airflow_version helper
Browse files Browse the repository at this point in the history
The point of this helper is to make it simpler to figure out what airflow version is installed, in particular to implement conditional logic in providers.

Currently the logic is too complicated and this has resulted in the proliferation of sort of redundant constants.  Here's an example:

```
from airflow import __version__ as AIRFLOW_VERSION

AIRFLOW_V_3_0_PLUS = Version(Version(AIRFLOW_VERSION).base_version) >= Version("3.0.0")
if AIRFLOW_V_3_0_PLUS:
    from airflow.sdk.definitions.asset import Asset
```

With this helper, we can do this instead:

```
from airflow import get_airflow_version

if get_airflow_version() > Version("3.0.0"):
    from airflow.sdk.definitions.asset import Asset
```
  • Loading branch information
dstandish committed Dec 6, 2024
1 parent 1b4922d commit 96e5fe8
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions airflow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,20 @@
# very easily cause import cycles in the conf init/validate code (since downstream code from
# those functions likely import settings).
# configuration is therefore initted early here, simply by importing it.

from packaging.version import Version

from airflow import configuration, settings


def get_airflow_version() -> Version:
"""Return packaging Version object representing the base version."""
return Version(Version(__version__).base_version)


__all__ = [
"__version__",
"get_airflow_version",
"DAG",
"Asset",
"XComArg",
Expand Down

0 comments on commit 96e5fe8

Please sign in to comment.