afonasev / flake8-return

Flake8 plugin for return expressions checking.
MIT License
62 stars 69 forks source link

Fix/116/R504-try-except #120

Closed calumy closed 2 years ago

calumy commented 2 years ago

This PR builds on #119 and updates the UnnecessaryAssignMixin class to fix the issues identified in #116. The _stack is expanded to include tries to log the position of try: statements and loops to log the position of any loops. visit_Try and _visit_loop were added/updated to add the start and end positions of each statement to the stack for python 3.8+. A function was also added for python 3.8+ to check if a variable's refs or assigns were within either a try statement or loop and skip reporting an error if so.

As the attribute end_lineno was only added to the AST class in python 3.8 (https://docs.python.org/3.8/library/ast.html), this fix can only be implemented for python 3.8 and above. For this reason, test cases have been added for both below python 3.8 and 3.8+. However, this negatively impacts code coverage as it is either calculated for 3.6/7 or 3.8+. This issue could be avoided if the minimum version was updated to 3.8; however, as 3.7 is not EOL , this is probably not appropriate at this time.

Test cases from #116 and #66 were added, and one of the test cases from #47 that had to be refactored can now be updated to not show the warning (43862bb173df93e031d1712736275a58b930ea09).

Fixes #116 and fixes #66 for python 3.8 and above.

codecov[bot] commented 2 years ago

Codecov Report

Merging #120 (fb09b43) into master (3bba9cd) will decrease coverage by 8.68%. The diff coverage is 57.69%.

@@            Coverage Diff             @@
##           master     #120      +/-   ##
==========================================
- Coverage   94.04%   85.36%   -8.69%     
==========================================
  Files           3        3              
  Lines         168      205      +37     
  Branches       35       59      +24     
==========================================
+ Hits          158      175      +17     
- Misses          5       23      +18     
- Partials        5        7       +2     
Impacted Files Coverage Δ
flake8_return/visitors.py 83.87% <56.86%> (-9.42%) :arrow_down:
flake8_return/errors.py 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

calumy commented 2 years ago

@afonasev, failing code coverage test explained in PR description. I know this is not ideal, but I believe this feature will be useful for 3.8+ and is, therefore, worth reducing the code coverage at this time.

afonasev commented 2 years ago

I agree. Thx for your contribution.