[AIRFLOW-6817] Lazy-load airflow.DAG to keep user-facing API untouched#7517
[AIRFLOW-6817] Lazy-load airflow.DAG to keep user-facing API untouched#7517ashb merged 3 commits intoapache:masterfrom
airflow.DAG to keep user-facing API untouched#7517Conversation
|
Probably worth some people double checking this in their IDE of choice -- I did install VSCode and PyCharm Community Edition for this, but I am not an IDE user. |
|
|
`from airflow import DAG` is _such_ a common pattern that it's worth making sure this stays working. Since we are now on Python 3 we can use PEP-562 to have officially supported lazy-loading of attributes on a module. Since the example_dags will be referred to/copied by users I have updated them all back to import from the airflow module, but have left any internal uses untouched
452820e to
6cde5ec
Compare
|
Travis failure: |
|
Fantastic! Works like a charm in both IntelliJ and VSCode! I think we need to just make pytest understand it as well. I will also add later some more protection against importing airflow.DAG in other places than example_dags. Also I have already idea how we can significantly speed up Pylint for local pre-commits (bringing back parallelism without risking cyclic dependencies). |
| def __getattr__(name): | ||
| # PEP-562: Lazy loaded attributes on python modules | ||
| if name == "DAG": | ||
| from airflow.models.dag import DAG # pylint: disable=redefined-outer-name |
There was a problem hiding this comment.
I think we also need to disable cyclic dependency pylint check as well (cyclic-import). I think disabling it in the first line of the file (just above the licence) will do the trick.
There was a problem hiding this comment.
Worth to try but I have a feeling that this has to be disabled in all places where the cycle occurs.
There was a problem hiding this comment.
Given this isn't at the top level does pylint still detect it as a cycle?
There was a problem hiding this comment.
Let's see and disable it if we find we need it. I am not 100% sure -> the algorithm is quite a bit different than python import mechanism (otherwise we would not have the cyclic imports issue at all).
|
Oh fark, PEP-562 is only Py 3.7+ I'll conditionally pull in https://github.com/facelessuser/pep562 for py <3.7 (Hell, that might even work on Py2 if we want it to) |
@potiuk Do you just mean the test failures, or something else? I.e. I'm not sure what pytest needs to understand as it runs the code so doesn't need the hack. |
Codecov Report
@@ Coverage Diff @@
## master #7517 +/- ##
==========================================
+ Coverage 86.81% 86.81% +<.01%
==========================================
Files 893 893
Lines 42190 42221 +31
==========================================
+ Hits 36626 36654 +28
- Misses 5564 5567 +3
Continue to review full report at Codecov.
|
|
Go ahead @ashb -> I will make pylint related changes as folow up. It's great we could do this! |
…hed (apache#7517) `from airflow import DAG` is _such_ a common pattern that it's worth making sure this stays working. Since we are now on Python 3 we can use PEP-562 to have officially supported lazy-loading of attributes on a module. Since the example_dags will be referred to/copied by users I have updated them all back to import from the airflow module, but have left any internal uses untouched.
from airflow import DAGis such a common pattern that it's worthmaking sure this stays working.
Since we are now on Python 3 we can use PEP-562 to have officially
supported lazy-loading of attributes on a module.
Since the example_dags will be referred to/copied by users I have
updated them all back to import from the airflow module, but have left
any internal uses untouched.
This approach taken from Celery: https://github.com/celery/celery/blob/f2ddd894c32f642a20f03b805b97e460f4fb3b4f/celery/__init__.py#L63-L78
Issue link: AIRFLOW-6817
Make sure to mark the boxes below before creating PR: [x]
[AIRFLOW-NNNN]. AIRFLOW-NNNN = JIRA ID** For document-only changes commit message can start with
[AIRFLOW-XXXX].In case of fundamental code change, 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 UPDATING.md.
Read the Pull Request Guidelines for more information.