Skip to content
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

Add pre-commit hook for McCabe max complexity check and fix errors #629

Merged

Conversation

jbandoro
Copy link
Collaborator

@jbandoro jbandoro commented Oct 26, 2023

Description

Opening this up as a draft to work through the code that have been flagged as too complex given the default threshold of 10 below:

❯ pre-commit run flake8 --all-files
flake8...................................................................Failed
- hook id: flake8
- exit code: 1

cosmos/dbt/graph.py:134:5: C901 'DbtGraph.load_via_dbt_ls' is too complex (16)
cosmos/dbt/parser/project.py:136:5: C901 'DbtModel.__post_init__' is too complex (18)
cosmos/dbt/parser/project.py:346:5: C901 'LegacyDbtProject._handle_config_file' is too complex (15)
cosmos/dbt/selector.py:87:1: C901 'select_nodes_ids_by_intersection' is too complex (16)

I'm happy to do this since it will help be get more familiar with some inner workings of cosmos, but if someone from the team wants to take over they're welcome.

If its lowered to 6 as mentioned in the issue, there are more errors that could be fixed:

❯ pre-commit run flake8 --all-files
flake8...................................................................Failed
- hook id: flake8
- exit code: 1

cosmos/airflow/graph.py:196:1: C901 'build_airflow_graph' is too complex (9)
cosmos/config.py:66:5: C901 'ProjectConfig.__init__' is too complex (7)
cosmos/dbt/graph.py:134:5: C901 'DbtGraph.load_via_dbt_ls' is too complex (16)
cosmos/dbt/parser/project.py:63:5: C901 'DbtModelConfig._config_selector_ooo' is too complex (7)
cosmos/dbt/parser/project.py:98:1: C901 'extract_python_file_upstream_requirements' is too complex (7)
cosmos/dbt/parser/project.py:136:5: C901 'DbtModel.__post_init__' is too complex (18)
cosmos/dbt/parser/project.py:262:5: C901 'LegacyDbtProject.__post_init__' is too complex (10)
cosmos/dbt/parser/project.py:346:5: C901 'LegacyDbtProject._handle_config_file' is too complex (15)
cosmos/dbt/selector.py:52:5: C901 'SelectorConfig.load_from_statement' is too complex (7)
cosmos/dbt/selector.py:87:1: C901 'select_nodes_ids_by_intersection' is too complex (16)
cosmos/dbt/selector.py:170:1: C901 'select_nodes' is too complex (9)
cosmos/hooks/subprocess.py:34:5: C901 'FullOutputSubprocessHook.run_command' is too complex (8)
cosmos/operators/base.py:137:5: C901 'DbtBaseOperator.get_env' is too complex (7)
cosmos/operators/base.py:186:5: C901 'DbtBaseOperator.add_global_flags' is too complex (7)
cosmos/operators/local.py:136:5: C901 'DbtLocalBaseOperator.store_compiled_sql' is too complex (7)
cosmos/profiles/base.py:160:5: C901 'BaseProfileMapping.get_dbt_value' is too complex (8)
dev/dags/dbt_docs.py:45:1: C901 'which_upload' is too complex (7)

Related Issue(s)

Closes: #525

Breaking Change?

Checklist

  • I have made corresponding changes to the documentation (if required)
  • I have added tests that prove my fix is effective or that my feature works

@jbandoro jbandoro temporarily deployed to external October 26, 2023 00:38 — with GitHub Actions Inactive
@jbandoro jbandoro changed the title Add pre-commit hook for McCabe max complexity check and fix Add pre-commit hook for McCabe max complexity check and fix errors Oct 26, 2023
@netlify
Copy link

netlify bot commented Oct 26, 2023

👷 Deploy Preview for amazing-pothos-a3bca0 processing.

Name Link
🔨 Latest commit 0295fce
🔍 Latest deploy log https://app.netlify.com/sites/amazing-pothos-a3bca0/deploys/654180f917e778000864af24

@codecov
Copy link

codecov bot commented Oct 26, 2023

Codecov Report

Attention: 13 lines in your changes are missing coverage. Please review.

Comparison is base (58de67e) 93.38% compared to head (0295fce) 93.41%.
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #629      +/-   ##
==========================================
+ Coverage   93.38%   93.41%   +0.03%     
==========================================
  Files          53       53              
  Lines        2115     2158      +43     
==========================================
+ Hits         1975     2016      +41     
- Misses        140      142       +2     
Files Coverage Δ
cosmos/dbt/graph.py 99.40% <98.41%> (+0.63%) ⬆️
cosmos/dbt/selector.py 96.96% <97.87%> (+0.38%) ⬆️
cosmos/dbt/parser/project.py 89.73% <85.13%> (-0.32%) ⬇️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@tatiana tatiana left a comment

Choose a reason for hiding this comment

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

@jbandoro Thank you for taking the time to work on the code complexity!
This type of task sometimes stays hanging in the backlog - but it's critical for the maintainability of the code. Keep up the fantastic work!

@jbandoro jbandoro temporarily deployed to external October 27, 2023 02:21 — with GitHub Actions Inactive
@jbandoro jbandoro temporarily deployed to external October 27, 2023 20:02 — with GitHub Actions Inactive
@jbandoro jbandoro temporarily deployed to external October 27, 2023 20:17 — with GitHub Actions Inactive
@jbandoro jbandoro temporarily deployed to external October 30, 2023 18:55 — with GitHub Actions Inactive
@jbandoro jbandoro temporarily deployed to external October 30, 2023 18:57 — with GitHub Actions Inactive
@tatiana tatiana added this to the 1.3.0 milestone Oct 31, 2023
@jbandoro jbandoro temporarily deployed to external October 31, 2023 20:06 — with GitHub Actions Inactive
@jbandoro jbandoro marked this pull request as ready for review October 31, 2023 20:12
@jbandoro jbandoro requested a review from a team as a code owner October 31, 2023 20:12
@jbandoro jbandoro requested a review from a team October 31, 2023 20:12
@jbandoro jbandoro temporarily deployed to external October 31, 2023 22:34 — with GitHub Actions Inactive
@jbandoro
Copy link
Collaborator Author

Apologies for the follow-up commits, I finally was able to get the integration tests to run completely in my local dev environment with hatch run tests.py3.10-2.6:test-integration and fixed some errors.

@jbandoro jbandoro requested a review from tatiana November 2, 2023 00:12
@tatiana
Copy link
Collaborator

tatiana commented Nov 2, 2023

This is outstanding work, @jbandoro. We're very grateful for this! I changed the scope of the original ticket, that stated the goal of accomplishing code complexity 6 to 10, and created a new ticket for us to reduce to 6: #641

@tatiana tatiana merged commit f9809a8 into astronomer:main Nov 2, 2023
39 of 40 checks passed
tatiana pushed a commit that referenced this pull request Nov 6, 2023
)

Reduce Cosmos code complexity from 18 to 10, automating checks as part of the CI.

```shell
❯ pre-commit run flake8 --all-files
flake8...................................................................Failed
- hook id: flake8
- exit code: 1

cosmos/dbt/graph.py:134:5: C901 'DbtGraph.load_via_dbt_ls' is too complex (16)
cosmos/dbt/parser/project.py:136:5: C901 'DbtModel.__post_init__' is too complex (18)
cosmos/dbt/parser/project.py:346:5: C901 'LegacyDbtProject._handle_config_file' is too complex (15)
cosmos/dbt/selector.py:87:1: C901 'select_nodes_ids_by_intersection' is too complex (16)
```

Closes: #525
(cherry picked from commit f9809a8)
tatiana added a commit that referenced this pull request Nov 6, 2023
Bug fixes

* Support ProjectConfig.dbt_project_path = None & different paths for Rendering and Execution by @MrBones757 in #634
* Fix adding test nodes to DAGs built using LoadMethod.DBT_MANIFEST and LoadMethod.CUSTOM by @edgga in #615

Others

* Add pre-commit hook for McCabe max complexity check and fix errors by @jbandoro in #629
* Update contributing docs for running integration tests by @jbandoro in #638
* Fix CI issue running integration tests by @tatiana in #640 and #644
* pre-commit updates in #637
@tatiana tatiana mentioned this pull request Nov 6, 2023
tatiana pushed a commit that referenced this pull request Nov 6, 2023
)

Reduce Cosmos code complexity from 18 to 10, automating checks as part of the CI.

```shell
❯ pre-commit run flake8 --all-files
flake8...................................................................Failed
- hook id: flake8
- exit code: 1

cosmos/dbt/graph.py:134:5: C901 'DbtGraph.load_via_dbt_ls' is too complex (16)
cosmos/dbt/parser/project.py:136:5: C901 'DbtModel.__post_init__' is too complex (18)
cosmos/dbt/parser/project.py:346:5: C901 'LegacyDbtProject._handle_config_file' is too complex (15)
cosmos/dbt/selector.py:87:1: C901 'select_nodes_ids_by_intersection' is too complex (16)
```

Closes: #525
(cherry picked from commit f9809a8)
tatiana added a commit that referenced this pull request Nov 6, 2023
Bug fixes

* Support ProjectConfig.dbt_project_path = None & different paths for Rendering and Execution by @MrBones757 in #634
* Fix adding test nodes to DAGs built using LoadMethod.DBT_MANIFEST and LoadMethod.CUSTOM by @edgga in #615

Others

* Add pre-commit hook for McCabe max complexity check and fix errors by @jbandoro in #629
* Update contributing docs for running integration tests by @jbandoro in #638
* Fix CI issue running integration tests by @tatiana in #640 and #644
* pre-commit updates in #637
@tatiana tatiana modified the milestones: 1.3.0, 1.2.2 Nov 7, 2023
tatiana added a commit that referenced this pull request Nov 7, 2023
Bug fixes

* Support ProjectConfig.dbt_project_path = None & different paths for Rendering and Execution by @MrBones757 in #634
* Fix adding test nodes to DAGs built using LoadMethod.DBT_MANIFEST and LoadMethod.CUSTOM by @edgga in #615

Others

* Add pre-commit hook for McCabe max complexity check and fix errors by @jbandoro in #629
* Update contributing docs for running integration tests by @jbandoro in #638
* Fix CI issue running integration tests by @tatiana in #640 and #644
* pre-commit updates in #637

(cherry picked from commit fa0620a)
tatiana added a commit that referenced this pull request Dec 5, 2023
[Justin Bandoro](https://www.linkedin.com/in/justin-bandoro-592b14a7/)
(@jbandoro) is a Data Engineer at Kevala Inc. He's based in San
Francisco (USA) and has been an early adopter of Cosmos, using it
regularly at his company.

Not only has he been using Cosmos since the early stages, but he has
consistently improved Cosmos since January 2023:
![Screenshot 2023-12-04 at 16 28
29](https://github.com/astronomer/astronomer-cosmos/assets/272048/43197938-d1ab-431f-b101-b6026e5cd3ab)

Some of his contributions include new features, code quality,
documentation and overall improvements. Some examples:
* Speed up integration tests in 67% #732 
* Prevent override of dbt profile fields #702
* Add support for env vars in `RenderConfig` in #690 
* Use symbolic links to run local tasks, avoiding to copy potentially
huge dbt project folders in #660
* Improve documentation in #638
* Automated and improved the code complexity checks in #629
* Added `DbtDocsGCSOperator` in #616 
* Added support for Python 3.7 in #88 and #214

Additionally, he has been interacting with users in the #airflow-dbt
Slack channel in a very collaborative and supportive way.

We want to promote him as a Cosmos committer and maintainer for all
these, recognising his constant efforts and achievements towards our
community. Thank you very much, @jbandoro !
arojasb3 pushed a commit to arojasb3/astronomer-cosmos that referenced this pull request Jul 14, 2024
Bug fixes

* Support ProjectConfig.dbt_project_path = None & different paths for Rendering and Execution by @MrBones757 in astronomer#634
* Fix adding test nodes to DAGs built using LoadMethod.DBT_MANIFEST and LoadMethod.CUSTOM by @edgga in astronomer#615

Others

* Add pre-commit hook for McCabe max complexity check and fix errors by @jbandoro in astronomer#629
* Update contributing docs for running integration tests by @jbandoro in astronomer#638
* Fix CI issue running integration tests by @tatiana in astronomer#640 and astronomer#644
* pre-commit updates in astronomer#637

(cherry picked from commit fa0620a)
arojasb3 pushed a commit to arojasb3/astronomer-cosmos that referenced this pull request Jul 14, 2024
[Justin Bandoro](https://www.linkedin.com/in/justin-bandoro-592b14a7/)
(@jbandoro) is a Data Engineer at Kevala Inc. He's based in San
Francisco (USA) and has been an early adopter of Cosmos, using it
regularly at his company.

Not only has he been using Cosmos since the early stages, but he has
consistently improved Cosmos since January 2023:
![Screenshot 2023-12-04 at 16 28
29](https://github.com/astronomer/astronomer-cosmos/assets/272048/43197938-d1ab-431f-b101-b6026e5cd3ab)

Some of his contributions include new features, code quality,
documentation and overall improvements. Some examples:
* Speed up integration tests in 67% astronomer#732 
* Prevent override of dbt profile fields astronomer#702
* Add support for env vars in `RenderConfig` in astronomer#690 
* Use symbolic links to run local tasks, avoiding to copy potentially
huge dbt project folders in astronomer#660
* Improve documentation in astronomer#638
* Automated and improved the code complexity checks in astronomer#629
* Added `DbtDocsGCSOperator` in astronomer#616 
* Added support for Python 3.7 in astronomer#88 and astronomer#214

Additionally, he has been interacting with users in the #airflow-dbt
Slack channel in a very collaborative and supportive way.

We want to promote him as a Cosmos committer and maintainer for all
these, recognising his constant efforts and achievements towards our
community. Thank you very much, @jbandoro !
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.

Add complexity metrics and enable checks in pre-commit
2 participants