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

CLN: Fix pylint undefined-loop-variable warnings #49740

Merged
merged 7 commits into from
Dec 19, 2022

Conversation

Leviob
Copy link
Contributor

@Leviob Leviob commented Nov 17, 2022

Contributes to #48855. I added in-line pylint disables or refactored code where relevant.

I'm still a little unsure about the some of these warnings, any help is appreciated.

pandas/core/util/hashing.py Outdated Show resolved Hide resolved
@@ -2298,6 +2298,7 @@ def set_sticky(
"selector": f"thead tr:nth-child({obj.nlevels+1}) th",
"props": props
+ (
# pylint: disable=undefined-loop-variable
f"top:{(i+1) * pixel_size}px; height:{pixel_size}px; "
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't understand what's going on in this file. The i causing the warning is defined on line 2284.

Copy link
Member

Choose a reason for hiding this comment

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

it's just that it's outside of the loop

@@ -883,7 +883,7 @@ def parse(
if ret_dict:
return output
else:
return output[asheetname]
return output[asheetname] # pylint: disable=undefined-loop-variable
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure about this one. asheetname is defined on line 731.

Copy link
Contributor

@uzzell uzzell Nov 28, 2022

Choose a reason for hiding this comment

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

I think that the key here is the sequence of conditional statements that start in line 715. In the first two cases, ret_dict is set to True, which means that the method won't reach this branch. In the last two cases, we have sheets = [sheet_name]. Because sheets is non-empty, asheetname will be defined.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, thanks.

Copy link
Member

Choose a reason for hiding this comment

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

is there a way to refactor such as to not have to turn off the warning? the warning is there because asheetname is being used outside the scope of the loop

Copy link
Member

Choose a reason for hiding this comment

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

Aside from being a silly example, is this an undesired pattern?

for x in ['a', 'b', 'c']:
   if random.choice([True, False]):
       break
return x

Here the loop variable is guaranteed to exist after the for-loop (the same as in this code here).

Copy link
Member

Choose a reason for hiding this comment

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

I think it's just to help prevent Edge cases like

for x in mylist:
    print(x)
return x

in which x in the last line would be undefined if mylist was empty

Copy link
Member

Choose a reason for hiding this comment

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

Then I think it's okay to disable the warning in this case (the for loop is guaranteed to be entered). I'm also very much in favor of refactoring this code, but for reasons outside of the scope of this issue.

@@ -141,6 +141,7 @@ def test_publishes(self, ip):
with_latex = pd.option_context("display.latex.repr", True)

with opt, with_latex:
# pylint: disable-next=undefined-loop-variable
formatted = ipython.display_formatter.format(obj)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't understand obj here.

Copy link
Contributor

Choose a reason for hiding this comment

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

obj is an element of objects, which is defined as objects = [df["A"], df, df]. So, this one is a false positive.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the help!

Copy link
Contributor

Choose a reason for hiding this comment

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

You're welcome!

@mroeschke mroeschke added the Code Style Code style, linting, code_checks label Nov 17, 2022
@Leviob Leviob marked this pull request as ready for review December 1, 2022 00:38
@MarcoGorelli MarcoGorelli self-requested a review December 1, 2022 09:25
@@ -883,7 +883,7 @@ def parse(
if ret_dict:
return output
else:
return output[asheetname]
return output[asheetname] # pylint: disable=undefined-loop-variable
Copy link
Member

Choose a reason for hiding this comment

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

is there a way to refactor such as to not have to turn off the warning? the warning is there because asheetname is being used outside the scope of the loop

Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

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

Thanks for working on this

This causes quite a few false-positives - shall we keep it turned off, but keep the two refactors you found (which are quite nice)?

break
return [x[0]] + [i if i else " " * len(pad) for i in x[1:]]
return [x[0]] + [i if i else " " * len(pad) for i in x[1:]]
Copy link
Member

Choose a reason for hiding this comment

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

nice!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that's reasonable. But I also want to change the variable s in this test to col. I think the current variable probably a mistake that this warning brought to my attention.

str(s) # pylint: disable=undefined-loop-variable

Copy link
Member

Choose a reason for hiding this comment

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

yeah sounds good - looks like the test is just there to check that iteration works anyway

Comment on lines 2300 to 2302
+ (
f"top:{(i+1) * pixel_size}px; height:{pixel_size}px; "
f"top:{(len(levels_)) * pixel_size}px; \
height:{pixel_size}px; "
Copy link
Contributor Author

Choose a reason for hiding this comment

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

My reasoning here is that the for loop will have already completed by this point. If levels_ were an empty list, i would be undefined, whereas len(levels_) would be 0.

Copy link
Member

Choose a reason for hiding this comment

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

can you run the other pre-commit checks please? pretty sure this'll be reformatted

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I ran pre-commit run pylint --hook-stage manual --files pandas/io/formats/style.py and pre-commit run --all-files and they all passed. What other checks need to be run?

Copy link
Member

Choose a reason for hiding this comment

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

ah my bad, I thought black forbade this \, but it looks like it's fine if it's in a string

Copy link
Member

Choose a reason for hiding this comment

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

this is probably fine, I'll give it another look tomorrow

Copy link
Member

Choose a reason for hiding this comment

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

Without running this, it's not clear to me that it's the same

Could you write it as something like

                                f"top:{(len(levels_)) * pixel_size}px; "
                                f"height:{pixel_size}px; "

please?

The rest looks fine

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep. Thanks for the help!

@Leviob Leviob requested a review from MarcoGorelli December 15, 2022 20:21
@MarcoGorelli MarcoGorelli requested review from MarcoGorelli and removed request for MarcoGorelli December 15, 2022 21:55
@MarcoGorelli MarcoGorelli added this to the 2.0 milestone Dec 19, 2022
Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

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

thanks @Leviob

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Code Style Code style, linting, code_checks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants