-
Notifications
You must be signed in to change notification settings - Fork 14.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fixing circular import error in providers caused by airflow version check #31379
fixing circular import error in providers caused by airflow version check #31379
Conversation
We should do this on all providers please. |
Actually I worry if this will cause a problem on 2.4 and 2.5 specifically: if not os.environ.get("_AIRFLOW__AS_LIBRARY", None):
settings.initialize()
# Things to lazy import in form {local_name: ('target_module', 'target_name')}
__lazy_imports: dict[str, tuple[str, str]] = {
'DAG': ('.models.dag', 'DAG'),
'Dataset': ('.datasets', 'Dataset'),
'XComArg': ('.models.xcom_arg', 'XComArg'),
'AirflowException': ('.exceptions', 'AirflowException'),
'version': ('.version', ''),
'__version__': ('.version', 'version'),
}
def __getattr__(name: str):
... # Lazy load code So on 2.4, the logging import will be triggered by the 2.5 is the same. |
then maybe we can try something like the following? try:
airflow_version = airflow.__version__
except ImportError:
airflow_version = airflow.version.version
if packaging.version.parse(airflow_version) < packaging.version.parse("2.4.0"):
... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As dicussed in the https://github.com/apache/airflow/pull/30994/files#r1197635148 - we need to also update the JINJA template in dev/provider_packages
once we figure out the best approach also for 2.4 (just in case I add request changes so that it is not merged accidentally).
I think it's AttributeError (at least by your original error message) but yes. |
I thnk that is not going to work - it will trigger the very same recursive import error. I think the only "good" solution is to query airflow package metadata if It will be slower but it's not going to get into any danger of resursive error. |
Actually, maybe catch both AttributeError and ImportError? I can't say which will be thrown? (flip, flop. Sorry) |
catch Exception ? |
Do you mean something like from importlib.metadata import version
version("airflow") |
05c489a
to
6775bfd
Compare
Almost but not quite:
Also we still need to support Python 3.7 (till the end of June) so we need to do it even more complicated:
In Python 3.7 importlib.metadata does not have version - it only comes with the |
@potiuk Thanks! Just updated it |
@potiuk I think try to import |
And what shoudl we do then :) ? It will not serve its purpose, becaue it will also fail with the same recursion error on 2.3 or 2.2 likely. so we will not be able to detect what we wanted to detect (specifically if someone tries to import new http or smtp provider on Airflow < 2.4 (that was the reason of this change to detect this case) |
My point is the recursion error is catchable, and if that happens the Wei and I will test what works on 2.3 and 2.4 - cos not having to have the importlib mess run in each provider would be good (otherwise it would slow things down a lot in 2.4/2.5) |
Do we have other (faster) way to check which version of airflow are we in < 2.4 then (without triggering the recursion)? |
Because the whole point of this check is to check it - if we catch recursion error - we still do not know which airflow version we are at - and this is the whole point of the check. |
Potentially, we could do the fallback only for those preinstalled providers if we have no other way. For all others, this check is a bit superfluous (it is only needed if someone forced installation of provider, without looking at the requiremenst. For the preinstalled provider, this runtime check is our only protection. |
We could make "if preinstalled_provider" in JINJA template - we already have this in context I think. |
I tested this on 2.3.0 and 2.4.0 and it works -- ran try:
form airflow import version as airflow_version
except ImportError:
form airflow.version import version as airflow_version
if packaging.version.parse(airflow_version) < packaging.version.parse("2.4.0"): I even set the min dep to 2.4.1 to double check, all good. AttributeError is never thrown if we do the |
… for avoiding circular import error https://github.com/apache/airflow/pull/30994/files#r1197635148
…n__ for avoiding circular import error
…__ does not exist
…sm for providers in the template
the original error encounter AttributeError. We might encounter both exception in different cases
487eaf4
to
af04d4a
Compare
That's cool. |
And with #31385 we are going to double check it - because the test is going to check it all that also on Airflow 2.4 version. |
Rebased #31385 to cross-check it. |
Oh sorry -- might have been a bit eager in merging this then. |
When using `AstroCustomXcomBackend` and Python SDK 1.5, we were raising the exception: ``` Traceback (most recent call last): File "/usr/local/bin/airflow", line 5, in <module> from airflow.__main__ import main File "/usr/local/lib/python3.9/site-packages/airflow/__init__.py", line 55, in <module> settings.initialize() File "/usr/local/lib/python3.9/site-packages/airflow/settings.py", line 567, in initialize import_local_settings() File "/usr/local/lib/python3.9/site-packages/airflow/settings.py", line 524, in import_local_settings import airflow_local_settings File "/usr/local/airflow/airflow_local_settings.py", line 10, in <module> from airflow.models.baseoperator import BaseOperator File "/usr/local/lib/python3.9/site-packages/airflow/models/baseoperator.py", line 75, in <module> from airflow.models.taskinstance import TaskInstance, clear_task_instances File "/usr/local/lib/python3.9/site-packages/airflow/models/taskinstance.py", line 93, in <module> from airflow.models.xcom import XCOM_RETURN_KEY, LazyXComAccess, XCom File "/usr/local/lib/python3.9/site-packages/airflow/models/xcom.py", line 835, in <module> XCom = resolve_xcom_backend() File "/usr/local/lib/python3.9/site-packages/airflow/models/xcom.py", line 818, in resolve_xcom_backend clazz = conf.getimport("core", "xcom_backend", fallback=f"airflow.models.xcom.{BaseXCom.__name__}") File "/usr/local/lib/python3.9/site-packages/airflow/configuration.py", line 809, in getimport return import_string(full_qualified_path) File "/usr/local/lib/python3.9/site-packages/airflow/utils/module_loading.py", line 33, in import_string module = import_module(module_path) File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "/usr/local/lib/python3.9/site-packages/astro/custom_backend/astro_custom_backend.py", line 6, in <module> from astro.custom_backend.serializer import deserialize, serialize File "/usr/local/lib/python3.9/site-packages/astro/custom_backend/serializer.py", line 12, in <module> if airflow.__version__ >= "2.3": AttributeError: partially initialized module 'airflow' has no attribute '__version__' (most likely due to a circular import) ``` Relates to: apache/airflow#31379 Closes: #1939
When using AstroCustomXcomBackend and Python SDK 1.5, we were raising the exception: Traceback (most recent call last): File /usr/local/bin/airflow, line 5, in <module> from airflow.__main__ import main File /usr/local/lib/python3.9/site-packages/airflow/__init__.py, line 55, in <module> settings.initialize() File /usr/local/lib/python3.9/site-packages/airflow/settings.py, line 567, in initialize import_local_settings() File /usr/local/lib/python3.9/site-packages/airflow/settings.py, line 524, in import_local_settings import airflow_local_settings File /usr/local/airflow/airflow_local_settings.py, line 10, in <module> from airflow.models.baseoperator import BaseOperator File /usr/local/lib/python3.9/site-packages/airflow/models/baseoperator.py, line 75, in <module> from airflow.models.taskinstance import TaskInstance, clear_task_instances File /usr/local/lib/python3.9/site-packages/airflow/models/taskinstance.py, line 93, in <module> from airflow.models.xcom import XCOM_RETURN_KEY, LazyXComAccess, XCom File /usr/local/lib/python3.9/site-packages/airflow/models/xcom.py, line 835, in <module> XCom = resolve_xcom_backend() File /usr/local/lib/python3.9/site-packages/airflow/models/xcom.py, line 818, in resolve_xcom_backend clazz = conf.getimport(core, xcom_backend, fallback=fairflow.models.xcom.{BaseXCom.__name__}) File /usr/local/lib/python3.9/site-packages/airflow/configuration.py, line 809, in getimport return import_string(full_qualified_path) File /usr/local/lib/python3.9/site-packages/airflow/utils/module_loading.py, line 33, in import_string module = import_module(module_path) File /usr/local/lib/python3.9/importlib/__init__.py, line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File /usr/local/lib/python3.9/site-packages/astro/custom_backend/astro_custom_backend.py, line 6, in <module> from astro.custom_backend.serializer import deserialize, serialize File /usr/local/lib/python3.9/site-packages/astro/custom_backend/serializer.py, line 12, in <module> if airflow.__version__ >= 2.3: AttributeError: partially initialized module 'airflow' has no attribute '__version__' (most likely due to a circular import) Relates to: apache/airflow#31379 Closes: #1939
When using AstroCustomXcomBackend and Python SDK 1.5, we were raising the exception: Traceback (most recent call last): File /usr/local/bin/airflow, line 5, in <module> from airflow.__main__ import main File /usr/local/lib/python3.9/site-packages/airflow/__init__.py, line 55, in <module> settings.initialize() File /usr/local/lib/python3.9/site-packages/airflow/settings.py, line 567, in initialize import_local_settings() File /usr/local/lib/python3.9/site-packages/airflow/settings.py, line 524, in import_local_settings import airflow_local_settings File /usr/local/airflow/airflow_local_settings.py, line 10, in <module> from airflow.models.baseoperator import BaseOperator File /usr/local/lib/python3.9/site-packages/airflow/models/baseoperator.py, line 75, in <module> from airflow.models.taskinstance import TaskInstance, clear_task_instances File /usr/local/lib/python3.9/site-packages/airflow/models/taskinstance.py, line 93, in <module> from airflow.models.xcom import XCOM_RETURN_KEY, LazyXComAccess, XCom File /usr/local/lib/python3.9/site-packages/airflow/models/xcom.py, line 835, in <module> XCom = resolve_xcom_backend() File /usr/local/lib/python3.9/site-packages/airflow/models/xcom.py, line 818, in resolve_xcom_backend clazz = conf.getimport(core, xcom_backend, fallback=fairflow.models.xcom.{BaseXCom.__name__}) File /usr/local/lib/python3.9/site-packages/airflow/configuration.py, line 809, in getimport return import_string(full_qualified_path) File /usr/local/lib/python3.9/site-packages/airflow/utils/module_loading.py, line 33, in import_string module = import_module(module_path) File /usr/local/lib/python3.9/importlib/__init__.py, line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File /usr/local/lib/python3.9/site-packages/astro/custom_backend/astro_custom_backend.py, line 6, in <module> from astro.custom_backend.serializer import deserialize, serialize File /usr/local/lib/python3.9/site-packages/astro/custom_backend/serializer.py, line 12, in <module> if airflow.__version__ >= 2.3: AttributeError: partially initialized module 'airflow' has no attribute '__version__' (most likely due to a circular import) Relates to: apache/airflow#31379 Closes: #1939 (cherry picked from commit 486ea53)
When using AstroCustomXcomBackend and Python SDK 1.5, we were raising the exception: ``` Traceback (most recent call last): File /usr/local/bin/airflow, line 5, in <module> from airflow.__main__ import main File /usr/local/lib/python3.9/site-packages/airflow/__init__.py, line 55, in <module> settings.initialize() File /usr/local/lib/python3.9/site-packages/airflow/settings.py, line 567, in initialize import_local_settings() File /usr/local/lib/python3.9/site-packages/airflow/settings.py, line 524, in import_local_settings import airflow_local_settings File /usr/local/airflow/airflow_local_settings.py, line 10, in <module> from airflow.models.baseoperator import BaseOperator File /usr/local/lib/python3.9/site-packages/airflow/models/baseoperator.py, line 75, in <module> from airflow.models.taskinstance import TaskInstance, clear_task_instances File /usr/local/lib/python3.9/site-packages/airflow/models/taskinstance.py, line 93, in <module> from airflow.models.xcom import XCOM_RETURN_KEY, LazyXComAccess, XCom File /usr/local/lib/python3.9/site-packages/airflow/models/xcom.py, line 835, in <module> XCom = resolve_xcom_backend() File /usr/local/lib/python3.9/site-packages/airflow/models/xcom.py, line 818, in resolve_xcom_backend clazz = conf.getimport(core, xcom_backend, fallback=fairflow.models.xcom.{BaseXCom.__name__}) File /usr/local/lib/python3.9/site-packages/airflow/configuration.py, line 809, in getimport return import_string(full_qualified_path) File /usr/local/lib/python3.9/site-packages/airflow/utils/module_loading.py, line 33, in import_string module = import_module(module_path) File /usr/local/lib/python3.9/importlib/__init__.py, line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File /usr/local/lib/python3.9/site-packages/astro/custom_backend/astro_custom_backend.py, line 6, in <module> from astro.custom_backend.serializer import deserialize, serialize File /usr/local/lib/python3.9/site-packages/astro/custom_backend/serializer.py, line 12, in <module> if airflow.__version__ >= 2.3: AttributeError: partially initialized module 'airflow' has no attribute '__version__' (most likely due to a circular import) ``` Relates to: apache/airflow#31379 Closes: #1939
When using AstroCustomXcomBackend and Python SDK 1.5, we were raising the exception: Traceback (most recent call last): File /usr/local/bin/airflow, line 5, in <module> from airflow.__main__ import main File /usr/local/lib/python3.9/site-packages/airflow/__init__.py, line 55, in <module> settings.initialize() File /usr/local/lib/python3.9/site-packages/airflow/settings.py, line 567, in initialize import_local_settings() File /usr/local/lib/python3.9/site-packages/airflow/settings.py, line 524, in import_local_settings import airflow_local_settings File /usr/local/airflow/airflow_local_settings.py, line 10, in <module> from airflow.models.baseoperator import BaseOperator File /usr/local/lib/python3.9/site-packages/airflow/models/baseoperator.py, line 75, in <module> from airflow.models.taskinstance import TaskInstance, clear_task_instances File /usr/local/lib/python3.9/site-packages/airflow/models/taskinstance.py, line 93, in <module> from airflow.models.xcom import XCOM_RETURN_KEY, LazyXComAccess, XCom File /usr/local/lib/python3.9/site-packages/airflow/models/xcom.py, line 835, in <module> XCom = resolve_xcom_backend() File /usr/local/lib/python3.9/site-packages/airflow/models/xcom.py, line 818, in resolve_xcom_backend clazz = conf.getimport(core, xcom_backend, fallback=fairflow.models.xcom.{BaseXCom.__name__}) File /usr/local/lib/python3.9/site-packages/airflow/configuration.py, line 809, in getimport return import_string(full_qualified_path) File /usr/local/lib/python3.9/site-packages/airflow/utils/module_loading.py, line 33, in import_string module = import_module(module_path) File /usr/local/lib/python3.9/importlib/__init__.py, line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File /usr/local/lib/python3.9/site-packages/astro/custom_backend/astro_custom_backend.py, line 6, in <module> from astro.custom_backend.serializer import deserialize, serialize File /usr/local/lib/python3.9/site-packages/astro/custom_backend/serializer.py, line 12, in <module> if airflow.__version__ >= 2.3: AttributeError: partially initialized module 'airflow' has no attribute '__version__' (most likely due to a circular import) Relates to: apache/airflow#31379 Closes: #1939 (cherry picked from commit 486ea53)
When using `AstroCustomXcomBackend` and Python SDK 1.5, we were raising the exception: ``` Traceback (most recent call last): File "/usr/local/bin/airflow", line 5, in <module> from airflow.__main__ import main File "/usr/local/lib/python3.9/site-packages/airflow/__init__.py", line 55, in <module> settings.initialize() File "/usr/local/lib/python3.9/site-packages/airflow/settings.py", line 567, in initialize import_local_settings() File "/usr/local/lib/python3.9/site-packages/airflow/settings.py", line 524, in import_local_settings import airflow_local_settings File "/usr/local/airflow/airflow_local_settings.py", line 10, in <module> from airflow.models.baseoperator import BaseOperator File "/usr/local/lib/python3.9/site-packages/airflow/models/baseoperator.py", line 75, in <module> from airflow.models.taskinstance import TaskInstance, clear_task_instances File "/usr/local/lib/python3.9/site-packages/airflow/models/taskinstance.py", line 93, in <module> from airflow.models.xcom import XCOM_RETURN_KEY, LazyXComAccess, XCom File "/usr/local/lib/python3.9/site-packages/airflow/models/xcom.py", line 835, in <module> XCom = resolve_xcom_backend() File "/usr/local/lib/python3.9/site-packages/airflow/models/xcom.py", line 818, in resolve_xcom_backend clazz = conf.getimport("core", "xcom_backend", fallback=f"airflow.models.xcom.{BaseXCom.__name__}") File "/usr/local/lib/python3.9/site-packages/airflow/configuration.py", line 809, in getimport return import_string(full_qualified_path) File "/usr/local/lib/python3.9/site-packages/airflow/utils/module_loading.py", line 33, in import_string module = import_module(module_path) File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "/usr/local/lib/python3.9/site-packages/astro/custom_backend/astro_custom_backend.py", line 6, in <module> from astro.custom_backend.serializer import deserialize, serialize File "/usr/local/lib/python3.9/site-packages/astro/custom_backend/serializer.py", line 12, in <module> if airflow.__version__ >= "2.3": AttributeError: partially initialized module 'airflow' has no attribute '__version__' (most likely due to a circular import) ``` Relates to: apache/airflow#31379 Closes: #1939
When using AstroCustomXcomBackend and Python SDK 1.5, we were raising the exception: ``` Traceback (most recent call last): File /usr/local/bin/airflow, line 5, in <module> from airflow.__main__ import main File /usr/local/lib/python3.9/site-packages/airflow/__init__.py, line 55, in <module> settings.initialize() File /usr/local/lib/python3.9/site-packages/airflow/settings.py, line 567, in initialize import_local_settings() File /usr/local/lib/python3.9/site-packages/airflow/settings.py, line 524, in import_local_settings import airflow_local_settings File /usr/local/airflow/airflow_local_settings.py, line 10, in <module> from airflow.models.baseoperator import BaseOperator File /usr/local/lib/python3.9/site-packages/airflow/models/baseoperator.py, line 75, in <module> from airflow.models.taskinstance import TaskInstance, clear_task_instances File /usr/local/lib/python3.9/site-packages/airflow/models/taskinstance.py, line 93, in <module> from airflow.models.xcom import XCOM_RETURN_KEY, LazyXComAccess, XCom File /usr/local/lib/python3.9/site-packages/airflow/models/xcom.py, line 835, in <module> XCom = resolve_xcom_backend() File /usr/local/lib/python3.9/site-packages/airflow/models/xcom.py, line 818, in resolve_xcom_backend clazz = conf.getimport(core, xcom_backend, fallback=fairflow.models.xcom.{BaseXCom.__name__}) File /usr/local/lib/python3.9/site-packages/airflow/configuration.py, line 809, in getimport return import_string(full_qualified_path) File /usr/local/lib/python3.9/site-packages/airflow/utils/module_loading.py, line 33, in import_string module = import_module(module_path) File /usr/local/lib/python3.9/importlib/__init__.py, line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File /usr/local/lib/python3.9/site-packages/astro/custom_backend/astro_custom_backend.py, line 6, in <module> from astro.custom_backend.serializer import deserialize, serialize File /usr/local/lib/python3.9/site-packages/astro/custom_backend/serializer.py, line 12, in <module> if airflow.__version__ >= 2.3: AttributeError: partially initialized module 'airflow' has no attribute '__version__' (most likely due to a circular import) ``` Relates to: apache/airflow#31379 Closes: #1939
We recently worked on testing our project with the latest providers' version, #31322 but encountered a circular import error. Thanks, @ash, for suggesting to me how to solve this on https://github.com/apache/airflow/pull/30994/files#r1197635148
^ Add meaningful description above
Read the Pull Request Guidelines for more information.
In case of fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
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.