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

Removing false flagging of assert statements from tests. #4236

Merged
merged 13 commits into from
Jul 19, 2024

Conversation

prady0t
Copy link
Contributor

@prady0t prady0t commented Jul 2, 2024

Codacy keeps flagging assert statements from tests. This added rule solves this.

@agriyakhetarpal agriyakhetarpal changed the title Removing flase flagging of assert statements from tests. Removing false flagging of assert statements from tests. Jul 2, 2024
Copy link
Contributor

@kratman kratman left a comment

Choose a reason for hiding this comment

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

Doesn't this ignore all asserts? We do not want asserts in the regular codebase.

Copy link

codecov bot commented Jul 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.45%. Comparing base (27c29df) to head (daf3918).
Report is 211 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #4236   +/-   ##
========================================
  Coverage    99.45%   99.45%           
========================================
  Files          288      288           
  Lines        22086    22089    +3     
========================================
+ Hits         21966    21969    +3     
  Misses         120      120           

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

@agriyakhetarpal
Copy link
Member

Doesn't this ignore all asserts? We do not want asserts in the regular codebase.

Yes, it does and we discussed this – I don't think there is a way to configure it per path, and many other projects have the same issue: fossasia/query-server#332. We can look at assert statements in PRs manually though, even though that means having more work on our end.

@kratman
Copy link
Contributor

kratman commented Jul 2, 2024

Is codacy even doing anything for us? Can we just make ruff stricter?

@agriyakhetarpal
Copy link
Member

@valentinsulzer brought up the idea, and yes, we can do it because Ruff has the same rules available: https://docs.astral.sh/ruff/rules/#flake8-bandit-s

@kratman
Copy link
Contributor

kratman commented Jul 2, 2024

I do think it is good to check for complexity (which codacy does), and asserts should be avoided in the main codebase. I think there is a lot we could improve by ramping up the ruff strictness. I think it should probably get a lot stricter based on the warnings I see in PyCharm

Signed-off-by: Pradyot Ranjan <[email protected]>
@kratman
Copy link
Contributor

kratman commented Jul 16, 2024

@prady0t Looks like a good improvement to me. It caught asserts in main code as well. We probably want to raise exceptions or log warnings instead.

@kratman
Copy link
Contributor

kratman commented Jul 16, 2024

I think from here we could try suppressing the asserts in codacy (if that does not cause issues in ruff's analysis) then just rely on ruff for most of it. Can you confirm that ruff triggers while the bandit yaml file suppresses codacy?

@prady0t prady0t requested review from a team, rtimms and valentinsulzer as code owners July 17, 2024 10:32
Copy link
Member

@agriyakhetarpal agriyakhetarpal left a comment

Choose a reason for hiding this comment

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

I think from here we could try suppressing the asserts in codacy (if that does not cause issues in ruff's analysis) then just rely on ruff for most of it. Can you confirm that ruff triggers while the bandit yaml file suppresses codacy?

@kratman, I think it does, I see Codacy is happy now and doesn't flag the assert statements in tests/, while Ruff caught the ones to catch in pybamm/. I shall approve this PR; IMO, we should add the rest of the flake8-bandit rules that Codacy uses to Ruff separately from here so that we can continue to use Codacy for now, and to let these changes be helpful for other PRs where tests are being modified.

@kratman
Copy link
Contributor

kratman commented Jul 17, 2024

I suspect codacy is "happy" because it only flags new findings. No new asserts were added in the tests here. I think Codacy is still going to have findings when you add new PyTest code. That is why I was asking @prady0t if the bandit yaml file could still be added while adding the ruff changes.

@kratman
Copy link
Contributor

kratman commented Jul 17, 2024

In my local tests the bandit.yml file does not seem to stop ruff from finding asserts in the main code, so we should add that back

@agriyakhetarpal
Copy link
Member

In my local tests the bandit.yml file does not seem to stop ruff from finding asserts in the main code, so we should add that back

But that's what we were wanting to do, right? i.e., have Ruff find asserts through its own rules in pyproject.toml, but let Codacy silence itself through bandit.yml – Ruff won't read bandit.yml.

I suspect codacy is "happy" because it only flags new findings. No new asserts were added in the tests here. I think Codacy is still going to have findings when you add new PyTest code. That is why I was asking @prady0t if the bandit yaml file could still be added while adding the ruff changes.

I see. Yes, I've seen Codacy flagging just new findings too earlier. @prady0t, could you push a dummy commit here where you add an assert statement somewhere, one each for the code and the tests, and we can check if Codacy flags those? Ruff seems to be working, I guess, because it flagged the ones in the code but left the ones in the tests (we check with --all-files in the style job).

@kratman
Copy link
Contributor

kratman commented Jul 17, 2024

@agriyakhetarpal, @prady0t That is what I am pointing out. The bandit.yml file was remove from this PR. It should be put back otherwise codacy will continue to flag pytest updates

@agriyakhetarpal agriyakhetarpal dismissed their stale review July 17, 2024 17:23

Never mind – thanks to @kratman for pointing out. We need the bandit.yml file that was added and then removed here to be added again.

@prady0t
Copy link
Contributor Author

prady0t commented Jul 17, 2024

Let me add it back.

@kratman
Copy link
Contributor

kratman commented Jul 17, 2024

@prady0t Thanks, should be good now

@agriyakhetarpal
Copy link
Member

Coverage seems to drop because the in-line assert tests will now need separate test cases

@prady0t
Copy link
Contributor Author

prady0t commented Jul 18, 2024

Let me add tests

@kratman
Copy link
Contributor

kratman commented Jul 18, 2024

@prady0t While you are at it, can you add the type hints to the function call?

@kratman
Copy link
Contributor

kratman commented Jul 19, 2024

Tests were passing before along with coverage, so I will merge this shortly

@kratman kratman merged commit ed412f8 into pybamm-team:develop Jul 19, 2024
23 of 24 checks passed
@prady0t
Copy link
Contributor Author

prady0t commented Jul 19, 2024

Thanks @kratman

@agriyakhetarpal
Copy link
Member

@prady0t, I was able to find a better fix for this – could you open a PR?

The fix is to add

assert_used:
  skips: ['*_test.py', '*test_*.py']

in bandit.yml, through which we can let Codacy ignore the assert statements selectively and keep the tool enabled till the time we are able to make Ruff implement all of the options.

js1tr3 pushed a commit to js1tr3/PyBaMM that referenced this pull request Aug 12, 2024
…#4236)

* Removing flase flagging of assert statements from tests

Signed-off-by: Pradyot Ranjan <[email protected]>

* style: pre-commit fixes

* Using ruff to check for asserts

Signed-off-by: Pradyot Ranjan <[email protected]>

* Using TypeError instead of assert

Signed-off-by: Pradyot Ranjan <[email protected]>

* Adding back bandit file

Signed-off-by: Pradyot Ranjan <[email protected]>

* style: pre-commit fixes

* Adding tests

Signed-off-by: Pradyot Ranjan <[email protected]>

---------

Signed-off-by: Pradyot Ranjan <[email protected]>
Co-authored-by: Pradyot Ranjan <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Eric G. Kratz <[email protected]>
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.

5 participants