Skip to content

Reformat based on official Airflow Best Practices #8

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

Merged
merged 2 commits into from
Jun 16, 2022

Conversation

feluelle
Copy link
Owner

  • fix issue with ReplaceVariableGetByJinja
  • add new rule ReplaceTopLevelImportByFunctionLevelImport

@codecov-commenter
Copy link

Codecov Report

Merging #8 (6dd8339) into main (8787be4) will decrease coverage by 2.51%.
The diff coverage is 93.22%.

@@            Coverage Diff             @@
##             main       #8      +/-   ##
==========================================
- Coverage   91.66%   89.15%   -2.52%     
==========================================
  Files           8        6       -2     
  Lines         156       83      -73     
==========================================
- Hits          143       74      -69     
+ Misses         13        9       -4     
Impacted Files Coverage Δ
airflint/__main__.py 0.00% <0.00%> (ø)
airflint/rules/use_jinja_variable_get.py 88.88% <75.00%> (ø)
airflint/actions/move_statements.py 100.00% <100.00%> (ø)
airflint/representatives/import_finder.py 100.00% <100.00%> (ø)
airflint/rules/use_function_level_imports.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8787be4...6dd8339. Read the comment docs.

@@ -0,0 +1,54 @@
# Credits go to GitHub user @isidentical who provided most of the solution.
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@isidentical Is that okay, if I mention you here? Do you want that?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing, thanks!

@@ -0,0 +1,59 @@
# Credits go to GitHub user @isidentical who provided most of the solution.
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@isidentical Is that okay, if I mention you here? Do you want that?

- [x] task decorator instead of PythonOperator and PythonVenvOperator
- [x] dag decorator instead of DAG
- [x] jinja string instead of Variable.get
- [x] Use function-level imports instead of top-level imports[^1] (see [Top level Python Code](https://airflow.apache.org/docs/apache-airflow/stable/best-practices.html#top-level-python-code))
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@isidentical In Airflow, not sure if you know the tool, you should avoid top-level code especially imports can cause huge performance issues as Airflow has a parser that continously parses the python file.

Comment on lines +49 to +51
# We remove the old statements.
for line in split_lines(new_source):
lines.remove(line)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I basically added to your code. Is this how you would do it? Is this safe to do or do I have to take care of edge cases where I can't remove the global imports @isidentical?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think using a solution like unimport right after your rules are ran would be better option. There are a lot of edge cases when dealing with import analysis (like try/excepts etc.). https://github.com/hakancelikdev/unimport

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are probably right - will change that 👍

feluelle added 2 commits June 16, 2022 11:12
- fix issue with ReplaceVariableGetByJinja
- add new rule ReplaceTopLevelImportByFunctionLevelImport
@feluelle feluelle force-pushed the feature/top-level-imports branch from 6dd8339 to 86972c9 Compare June 16, 2022 09:16
@feluelle feluelle merged commit 9d829c6 into main Jun 16, 2022
@feluelle feluelle deleted the feature/top-level-imports branch June 16, 2022 12:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants