flake8-return 1.2.0

pip install flake8-return Copy PIP instructions

Released: Oct 28, 2022

Flake8 plugin that checks return values

Verified details

Maintainers.

Avatar for afonasev from gravatar.com

Unverified details

Project links, github statistics.

  • Open issues:

View statistics for this project via Libraries.io , or by using our public dataset on Google BigQuery

License: MIT License (MIT)

Author: Afonasev Evgeniy

Tags flake8, plugin, return

Requires: Python >=3.6, <4.0

Classifiers

  • OSI Approved :: MIT License
  • Python :: 3
  • Python :: 3.6
  • Python :: 3.7
  • Python :: 3.8
  • Python :: 3.9
  • Python :: 3.10

Project description

Flake8-return.

r504 unnecessary variable assignment before return statement

Flake8 plugin that checks return values.

Installation

  • R501 do not explicitly return None in function if it is the only possible return value.
  • R502 do not implicitly return None in function able to return non-None value.
  • R503 missing explicit return at the end of function able to return non-None value.
  • R504 unnecessary variable assignment before return statement.
  • R505 unnecessary else after return statement.
  • R506 unnecessary else after raise statement.
  • R507 unnecessary else after continue statement.
  • R508 unnecessary else after break statement.

Returns in asyncio coroutines also supported.

For developers

Create venv and install deps, install git precommit hook, run linters, autoformat, tests etc, bump new version, 1.2.0 - 2022-10-28.

  • Port no-else-break, no-else-continue, no-else-raise, no-else-return from pylint (#122) Calum Young
  • PEP 621: Migrate more config to pyproject.toml (#123) Christian Clauss
  • Fix/116/R504-try-except (#120) Calum Young
  • Update ci (#119) Calum Young
  • Fix/47/Update-R504-for-assignment-value (#117) Calum Young
  • Upgrade GitHub Actions (#113) Christian Clauss
  • Add a space to avoid a typo in R503 (#98) Christian Clauss
  • GitHub Action to lint Python code (#97) Christian Clauss
  • Typo fixes (#92) Aarni Koskela
  • Create codeql-analysis.yml Afonasev Evgeniy
  • Bump flake8-plugin-utils from 1.1.1 to 1.3.2 (#87) dependabot
  • Bump mypy from 0.812 to 0.971 (#114) dependabot
  • Bump pytest-cov from 3.0.0 to 4.0.0 (#124) dependabot
  • Bump pytest-cov from 2.11.1 to 3.0.0 (#102) dependabot
  • Bump pytest-mock from 3.6.0 to 3.6.1 (#91) dependabot
  • Bump pytest from 6.2.4 to 6.2.5 (#99) dependabot
  • Bump pylint from 2.8.2 to 2.10.2 (#100) dependabot
  • Bump pytest from 6.2.3 to 6.2.4 (#86) dependabot

1.1.3 - 2021-05-05

  • Error clarifications (#77) Clément Robert
  • fix linting (migrate to black 20.0b1) (#78) Clément Robert

1.1.2 - 2020-07-09

  • Make R504 visitors handle while loops (#56) Frank Tackitt
  • Rename allows-prereleases to allow-prereleases (#55) Frank Tackitt
  • Fix typo: → haven't (#24) Jon Dufresne

1.1.1 - 2019-09-21

  • fixed #3 The R504 doesn't detect that the variable is modified in loop
  • fixed #4 False positive with R503 inside async with clause

1.1.0 - 2019-05-23

  • update flask_plugin_utils version to 1.0

1.0.0 - 2019-05-13

  • skip assign after unpacking while unnecessary assign checking "(x, y = my_obj)"

0.3.2 - 2019-04-01

  • allow "assert False" as last function return

0.3.1 - 2019-03-11

  • add pypi deploy into travis config
  • add make bump_version command

0.3.0 - 2019-02-26

  • skip functions that consist only return None
  • fix false positive when last return inner with statement
  • add unnecessary assign error
  • add support tuple in assign or return expressions
  • add support asyncio coroutines

0.2.0 - 2019-02-21

  • fix explicit/implicit
  • add flake8-plugin-utils as dependency
  • allow raise as last function return
  • allow no return as last line in while block
  • fix if/elif/else cases

0.1.1 - 2019-02-10

  • fix error messages

0.1.0 - 2019-02-10

Project details, release history release notifications | rss feed.

Oct 28, 2022

May 5, 2021

Jul 9, 2020

Sep 20, 2019

May 23, 2019

May 13, 2019

Apr 1, 2019

Mar 11, 2019

Feb 26, 2019

Feb 21, 2019

Feb 9, 2019

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages .

Source Distribution

Uploaded Oct 28, 2022 Source

Built Distribution

Uploaded Oct 28, 2022 Python 3

Hashes for flake8-return-1.2.0.tar.gz

Hashes for flake8_return-1.2.0-py3-none-any.whl.

  • português (Brasil)

Supported by

r504 unnecessary variable assignment before return statement

unnecessary-return-none (RET501) #

Derived from the flake8-return linter.

Fix is always available.

What it does #

Checks for the presence of a return None statement when None is the only possible return value.

Why is this bad? #

Python implicitly assumes return None if an explicit return value is omitted. Therefore, explicitly returning None is redundant and should be avoided when it is the only possible return value across all code paths in a given function.

Use instead:

CAST Highlight

Avoid variable assignments in conditional expressions

Why you should care.

5362

CAST Recommendations

Assign variables outside conditional expressions.

https://stackoverflow.com/questions/16148580/assign-variable-value-inside-if-statement https://stackoverflow.com/questions/17681535/variable-assignment-in-if-condition

How we detect

CAST Highlight counts one occurrence each time the assignment operator (=) is detected within a conditional expression, whether it is IF, EACH or WHILE.

About CAST and Highlight’s Code Insights

Over the last 25 years, CAST has leveraged unique knowledge on software quality measurement by analyzing thousands of applications and billions of lines of code. Based on this experience and community standards on programming best practices, Highlight implements hundreds of code insights across 15+ technologies to calculate health factors of a software.

r504 unnecessary variable assignment before return statement

Navigation Menu

Search code, repositories, users, issues, pull requests..., provide feedback.

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly.

To see all available qualifiers, see our documentation .

  • Notifications You must be signed in to change notification settings

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

Implement flake8-return #304

@charliermarsh

charliermarsh commented Oct 3, 2022 • edited

@charliermarsh

charliermarsh commented Dec 3, 2022

  • 🚀 1 reaction

Sorry, something went wrong.

Successfully merging a pull request may close this issue.

@charliermarsh

IMAGES

  1. return statement in C++ with Examples

    r504 unnecessary variable assignment before return statement

  2. Return statement in C

    r504 unnecessary variable assignment before return statement

  3. 1.4. Expressions and Assignment Statements

    r504 unnecessary variable assignment before return statement

  4. Return Statement in C

    r504 unnecessary variable assignment before return statement

  5. UnboundLocalError: Local Variable Referenced Before Assignment

    r504 unnecessary variable assignment before return statement

  6. Solved 1) Write a variable assignment statement that assigns

    r504 unnecessary variable assignment before return statement

VIDEO

  1. 43 Augmented Assignment statement

  2. variable and assignment #learnngrow #coding #coder

  3. 6 storing values in variable, assignment statement

  4. R variable assignment

  5. variable declaration and assignment

  6. ENG 504 Midterm Preparation / Vu Midterm Preparation / Most Important Questions / Correct Solution 💯

COMMENTS

  1. GitHub

    R504 unnecessary variable assignment before return statement. def x (): a = 1 # some code that not using `a` print ('test') return a # error! R505 unnecessary else after return statement. def x (y, z): if y: # error! return 1 else: return z. R506 unnecessary else after raise statement.

  2. flake8-return · PyPI

    R503 missing explicit return at the end of function able to return non-None value. def x (y): if not y: return 1 # error! R504 unnecessary variable assignment before return statement. def x (): a = 1 # some code that not using `a` print ('test') return a # error! R505 unnecessary else after return statement. def x (y, z): if y: # error! return ...

  3. False-positive RET504 "Unnecessary variable assignment before `return

    $ ruff test_ruff_ret_504.py Found 1 error(s). test_ruff_ret_504.py:8:12: RET504 Unnecessary variable assignment before `return` statement. ... actionless changed the title False-positive RET504 False-positive RET504 "Unnecessary variable assignment before return statement" Dec 19, 2022. Copy link Contributor. squiddy commented Dec 19, 2022.

  4. False-Positive R504 in non-trivial if-else Block #132

    flake8-return version used, if any: 1.2.0; Python version, if any: 3.10; Operating System: Windows 11; Description. The application of R504 unnecessary variable assignment before return statement seems wrong here. I've specifically not inlined my return statement in order to have a single return in this "complicated" if-else block.

  5. Local variables before return statements, does it matter?

    There is actually a SonarQube rule inherited from PMD called Unnecessary Local Before Return that talks about this. It says: Avoid unnecessarily creating local variables. This rule was later replaced by SSLR rule Variables should not be declared and then immediately returned or thrown, which maintains the same position:. Declaring a variable only to immediately return or throw it is a bad ...

  6. unnecessary-assign (RET504)

    unnecessary-assign (RET504)# Derived from the flake8-return linter. Fix is always available. What it does# Checks for variable assignments that immediately precede a return of the assigned variable. Why is this bad?# The variable assignment is not necessary, as the value can be returned directly. Example#

  7. flake8-return

    R502 do not implicitly return None in function able to return non-None value. def x (y): if not y: return # error! return 1. R503 missing explicit return at the end of function able to return non-None value. def x (y): if not y: return 1 # error! R504 unnecessary variable assignment before return statement.

  8. The flake8-return from afonasev

    afonasev / flake8-return Goto Github PK View Code? Open in Web Editor NEW 61.0 3.0 71.0 187 KB. Flake8 plugin for return expressions checking. License: MIT License. Python 96.90% Makefile 3.10% flake8-plugin. Introduction · People · Discuss; flake8-return's Introduction

  9. python

    Variable assignment before declaration [duplicate] Ask Question Asked 1 year, 8 months ago. ... Your function should return values that the caller can assign to L, M, and S, if necessary. - chepner. ... The global declaration is unnecessary. Something like this should solve your problem.

  10. R504 false positive when variable is a function parameter #133

    Sure, it's got typing issues too - but the code that inspired the above example was actually an attempt to get rid of R504 for a more complex case - where the underlying complexity is in class attribute modifications where adding a new variable doesn't help:

  11. superfluous-else-return (RET505)

    superfluous-else-return (RET505)# Derived from the flake8-return linter.. Fix is sometimes available. What it does#. Checks for else statements with a return statement in the preceding if block.. Why is this bad?# The else statement is not needed as the return statement will always break out of the enclosing function. Removing the else will reduce nesting and make the code more readable.

  12. unnecessary-return-none (RET501)

    unnecessary-return-none (RET501)# Derived from the flake8-return linter.. Fix is always available. What it does#. Checks for the presence of a return None statement when None is the only possible return value.. Why is this bad?# Python implicitly assumes return None if an explicit return value is omitted. Therefore, explicitly returning None is redundant and should be avoided when it is the ...

  13. Avoid variable assignments in conditional expressions

    This pattern verifies the presence of variable assignments in conditional (IF) expressions. It is generally not recommended to use the assignment operator in this case as it is close to the comparison operator (==) and could lead to misreading the code logic. ... Use of 'return' statement is not recommended Programming Best Practices. Try ...

  14. False positive on R504 · Issue #3 · afonasev/flake8-return

    Development. No branches or pull requests. 2 participants. Date you used flake8-return: 2019-05-16 flake8-return version used, if any: 1.0.0 Python version, if any: 3.7.1 Operating System: Debian Description The R504 doesn't detect that the variable is conditionally modified prior to return.

  15. How is the return status of a variable assignment determined?

    That's not a terrible way of looking at it. A crude scheme for determining the return status of a simple command (one not containing ;, &, |, && or || ) is: Scan the line from left to right until you reach the end or a command word (typically a program name). If you see a variable assignment, the return status for the line just might be 0.

  16. Implement flake8-return · Issue #304 · astral-sh/ruff · GitHub

    R501 ("do not explicitly return None in function if it is the only possible return value."); R502 ("do not implicitly return None in function able to return non-None value."); R503 ("missing explicit return at the end of function able to return non-None value."); R504 ("unnecessary variable assignment before return statement."); R505 ("unnecessary else after return statement.")

  17. c#

    1. I have question about unnecessary assignments of variables. I have lot of functions and some of them when they catch exception they will write it into logfile. For example this is one of them (Just rename PC using powershell): try. Runspace rs; rs = RunspaceFactory.CreateRunspace(); rs.Open();

  18. "UnboundLocalError: local variable referenced before assignment" After

    Assigning col outside the function creates the variable, but to be able to write to it inside a function, the global statement needs to be inside each function. btw As a programmer you should try very, very, very hard not to use globals. You should pass variables into functions for them to operate on:

  19. Is 'return' necessary in the last line of JS function?

    10. No, a return statement is not necessary at the end of a void function (sorry for the C-terms there). If you want to exit a function early, however (say if a specific condition wasn't met), then you'll use a return statement, even if you're not returning a value. Including a return statement at the end of a void function for readability ...