-
Notifications
You must be signed in to change notification settings - Fork 16.3k
[AIRFLOW-6817] Lazy-load airflow.DAG to keep user-facing API untouched
#7517
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
Conversation
|
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given this isn't at the top level does pylint still detect it as a cycle?
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.
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.