-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
CLN: Fix pylint undefined-loop-variable warnings #49740
Conversation
pandas/io/formats/style.py
Outdated
@@ -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; " |
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 don't understand what's going on in this file. The i
causing the warning is defined on line 2284.
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.
it's just that it's outside of the loop
pandas/io/excel/_base.py
Outdated
@@ -883,7 +883,7 @@ def parse( | |||
if ret_dict: | |||
return output | |||
else: | |||
return output[asheetname] | |||
return output[asheetname] # pylint: disable=undefined-loop-variable |
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'm not sure about this one. asheetname
is defined on line 731.
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 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.
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.
Makes sense, thanks.
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.
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
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.
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).
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 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
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.
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) |
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 don't understand obj
here.
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.
obj
is an element of objects
, which is defined as objects = [df["A"], df, df]
. So, this one is a false positive.
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.
Thanks for the help!
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.
You're welcome!
pandas/io/excel/_base.py
Outdated
@@ -883,7 +883,7 @@ def parse( | |||
if ret_dict: | |||
return output | |||
else: | |||
return output[asheetname] | |||
return output[asheetname] # pylint: disable=undefined-loop-variable |
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.
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
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.
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:]] |
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.
nice!
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 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.
pandas/pandas/tests/frame/test_iteration.py
Line 162 in d7bab20
str(s) # pylint: disable=undefined-loop-variable |
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.
yeah sounds good - looks like the test is just there to check that iteration works anyway
…s-leviob into fix-undefined-loop-variable
…s-leviob into fix-undefined-loop-variable
pandas/io/formats/style.py
Outdated
+ ( | ||
f"top:{(i+1) * pixel_size}px; height:{pixel_size}px; " | ||
f"top:{(len(levels_)) * pixel_size}px; \ | ||
height:{pixel_size}px; " |
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.
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.
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.
can you run the other pre-commit checks please? pretty sure this'll be reformatted
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 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?
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.
ah my bad, I thought black
forbade this \
, but it looks like it's fine if it's in a string
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.
this is probably fine, I'll give it another look tomorrow
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.
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
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.
Yep. Thanks for the help!
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.
thanks @Leviob
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.