-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
all: ruff --fix F841 Local variable assigned to but never used. #10994
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
all: ruff --fix F841 Local variable assigned to but never used. #10994
Conversation
Code size report:
|
Codecov Report
📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more @@ Coverage Diff @@
## master #10994 +/- ##
=======================================
Coverage 98.50% 98.50%
=======================================
Files 155 155
Lines 20549 20549
=======================================
Hits 20241 20241
Misses 308 308 Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
@@ -28,7 +28,7 @@ def main(): | |||
while True: | |||
res = s.accept() | |||
client_s = res[0] | |||
client_addr = res[1] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this mean that there is a convention in Python land that names beginning with underscores mean 'unused'? I though it was 'private', which is not the same at all. Wouldn't using # noqa: unused
or so be better here? That doesn't need to mangle the name and makes it very explicit and clear that this variable is here only to show what res[1] is. Or even just comment the whole thing like client_addr = res[1]
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please reread the commit message.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That doesn't mention unused at all, only private?
Also what do you think about the other suggestions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that this local variable is never used again, _
indicates that it is considered private (to this line alone).
The # noqa: F841
is possible (# noqa: unused
is not) but clunky when reading the code and most contributors will not have memorized ruff's 500 rules.
That doesn't need to mangle the name
Who cares if the name is mangled if it never gets used?
My hope is that when maintainers review this pull request, they spot the logic error and remove the whole line if that makes sense to them. For me, it would just be a SWAG because I do not know this codebase.
It is interesting to note that ruff --select=F841 --fix .
is more brutal than am.
client_addr = res[1]
-->res[1]
# Which is clearly a noop.
To which flake-bugbear would then reply:
B018: Found useless expression. Either assign it to a variable or remove it.
(bugbear's rules are the best!)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and remove the whole line
That would actually be my preference as well
Who cares if the name is mangled if it never gets used?
The reader. The line not being there means less reading.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would be happy close this PR and you can create a replacement if you would like.
pipx install ruff && ruff --select=F841 --fix .
is easy to run.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd say just remove this line.
This is an example and is actually a simplified version of http_server_simplistic_commented.py
. The latter uses this variable, so if someone wants more details they should look at http_server_simplistic_commented.py
instead.
9789aad
to
eec36bd
Compare
@@ -28,7 +28,7 @@ def main(): | |||
while True: | |||
res = s.accept() | |||
client_s = res[0] | |||
client_addr = res[1] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd say just remove this line.
This is an example and is actually a simplified version of http_server_simplistic_commented.py
. The latter uses this variable, so if someone wants more details they should look at http_server_simplistic_commented.py
instead.
ports/mimxrt/boards/make-pins.py
Outdated
@@ -226,7 +226,7 @@ def parse_af_file(self, filename, iomux_filename): | |||
# Extract indexes from header row | |||
pad_col = header.index("Pad") | |||
adc_col = header.index("ADC") | |||
acmp_col = header.index("ACMP") | |||
_acmp_col = header.index("ACMP") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest to remove this line.
ports/rp2/boards/make-pins.py
Outdated
@@ -228,7 +228,7 @@ def parse_board_file(self, filename): | |||
|
|||
cpu_pin_name = row[1] | |||
try: | |||
pin_num = parse_pin(cpu_pin_name) | |||
_pin_num = parse_pin(cpu_pin_name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest to just do parse_pin(cpu_pin_name)
.
tools/mpy-tool.py
Outdated
@@ -1276,7 +1275,7 @@ def read_raw_code(reader, parent_name, qstr_table, obj_table, segments): | |||
if native_scope_flags & MP_SCOPE_FLAG_VIPERRODATA: | |||
rodata_size = reader.read_uint() | |||
if native_scope_flags & MP_SCOPE_FLAG_VIPERBSS: | |||
bss_size = reader.read_uint() | |||
_bss_size = reader.read_uint() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest:
reader.read_uint() # bss size
tools/mpy-tool.py
Outdated
@@ -1285,10 +1284,10 @@ def read_raw_code(reader, parent_name, qstr_table, obj_table, segments): | |||
if op == 0xFF: | |||
break | |||
if op & 1: | |||
addr = reader.read_uint() | |||
_addr = reader.read_uint() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As above, I suggest:
reader.read_uint() # addr
tools/mpy-tool.py
Outdated
op >>= 1 | ||
if op <= 5 and op & 1: | ||
n = reader.read_uint() | ||
_n = reader.read_uint() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As above:
reader.read_uint() # n
tools/mpy_ld.py
Outdated
@@ -471,7 +471,7 @@ def do_relocation_text(env, text_addr, r): | |||
# Extract relevant info about symbol that's being relocated | |||
s = r.sym | |||
s_bind = s.entry["st_info"]["bind"] | |||
s_shndx = s.entry["st_shndx"] | |||
_s_shndx = s.entry["st_shndx"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line can be removed.
eec36bd
to
e7be2bb
Compare
This fixes ruff rule F841.
e7be2bb
to
2a1db77
Compare
Rebased and updated commit message. |
%
ruff rule F841
unused-variable (F841)
Derived from the Pyflakes linter.
Autofix is always available.
What it does
Checks for the presence of unused variables in function scopes.
Why is this bad?
A variable that is defined but not used is likely a mistake, and should
be removed to avoid confusion.
If a variable is intentionally defined-but-not-used, it should be
prefixed with an underscore, or some other value that adheres to the
[
dummy-variable-rgx
] pattern.Options
dummy-variable-rgx
Example
Use instead: