Skip to content

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

Conversation

cclauss
Copy link
Contributor

@cclauss cclauss commented Mar 10, 2023

% 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

def foo():
    x = 1
    y = 2
    return x

Use instead:

def foo():
    x = 1
    return x

@github-actions
Copy link

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:    +0 +0.000% PYBV10
        rp2:    +0 +0.000% PICO

@codecov-commenter
Copy link

codecov-commenter commented Mar 10, 2023

Codecov Report

Merging #10994 (9789aad) into master (4376c96) will not change coverage.
The diff coverage is n/a.

📣 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]
Copy link
Contributor

@stinos stinos Mar 10, 2023

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]?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

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?

Copy link
Contributor Author

@cclauss cclauss Mar 10, 2023

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!)

Copy link
Contributor

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.

Copy link
Contributor Author

@cclauss cclauss Mar 10, 2023

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.

Copy link
Member

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.

@cclauss cclauss requested a review from stinos March 10, 2023 09:20
@cclauss cclauss force-pushed the ruff_--fix_Local_variable_assigned_to_but_never_used branch from 9789aad to eec36bd Compare March 10, 2023 13:11
@cclauss cclauss changed the title ruff --fix F841 Local variable assigned to but never used all: ruff --fix F841 Local variable assigned to but never used. Mar 10, 2023
@@ -28,7 +28,7 @@ def main():
while True:
res = s.accept()
client_s = res[0]
client_addr = res[1]
Copy link
Member

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.

@@ -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")
Copy link
Member

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.

@@ -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)
Copy link
Member

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).

@@ -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()
Copy link
Member

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

@@ -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()
Copy link
Member

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

op >>= 1
if op <= 5 and op & 1:
n = reader.read_uint()
_n = reader.read_uint()
Copy link
Member

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"]
Copy link
Member

Choose a reason for hiding this comment

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

Line can be removed.

@cclauss cclauss force-pushed the ruff_--fix_Local_variable_assigned_to_but_never_used branch from eec36bd to e7be2bb Compare May 2, 2023 06:08
@cclauss cclauss requested a review from dpgeorge May 2, 2023 06:08
@dpgeorge dpgeorge force-pushed the ruff_--fix_Local_variable_assigned_to_but_never_used branch from e7be2bb to 2a1db77 Compare May 2, 2023 06:38
@dpgeorge
Copy link
Member

dpgeorge commented May 2, 2023

Rebased and updated commit message.

@dpgeorge dpgeorge merged commit 2a1db77 into micropython:master May 2, 2023
@cclauss cclauss deleted the ruff_--fix_Local_variable_assigned_to_but_never_used branch May 2, 2023 06:59
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.

4 participants