Skip to content

STY: enable flake8-bugbear #40570

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

Closed
wants to merge 0 commits into from
Closed

STY: enable flake8-bugbear #40570

wants to merge 0 commits into from

Conversation

twoertwein
Copy link
Member

@twoertwein twoertwein commented Mar 22, 2021

There are some cases where one of the values of a zip() was not used. I removed the zip(). If the two sequences for zip() have different lengths, this would change the behavior.

@@ -463,7 +463,7 @@ def check_compound_invert_op(self, lhs, cmp1, rhs):

# make sure the other engines work the same as this one
for engine in self.current_engines:
ev = pd.eval(ex, engine=self.engine, parser=self.parser)
ev = pd.eval(ex, engine=engine, parser=self.parser)
Copy link
Member Author

Choose a reason for hiding this comment

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

I assume engine was supposed to be used here. This breaks some tests for me locally!

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 you're probably right. if its breaking tests, probably merits its own PR?

Copy link
Member Author

Choose a reason for hiding this comment

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

I opened #40748 to keep track of this.

@MarcoGorelli
Copy link
Member

nice - does this solve all bugbear errors? If so, do you want to add it as an additional dependency to the flake8 check?

additional_dependencies: [flake8-comprehensions>=3.1.0]

@twoertwein
Copy link
Member Author

nice - does this solve all bugbear errors?

There are still plenty of other warnings. It might be possible to enable bugbear but we would need to disable some rules/add ignore statements (especially when covering the test code).

@twoertwein twoertwein marked this pull request as draft March 22, 2021 23:16
@twoertwein
Copy link
Member Author

bugbear could be enabled when the following warnings are ignored: B004, B005, B006, B008, B009, B010, B011, B015.

I feel there is something wrong with this test:
https://github.com/pandas-dev/pandas/pull/40570/files#diff-5974844fa02c9b81f3477198023bd02503bf82da1e3d15df6dfd62cdca48666bR132
but replacing r with l results in some errors.

I honestly do not understand how v is used in this test - is there a weird side effect (or I am blind)?
https://github.com/pandas-dev/pandas/pull/40570/files#diff-64777e769142fd7a85a91ae1809c0485036fb28ebb32cc6cccba9ae83646ac15R959


r1, _ = idx1.get_indexer_non_unique(idx2)
r1, _ = idx1.get_indexer_non_unique(indexer)
Copy link
Member Author

Choose a reason for hiding this comment

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

I assume that idx2 was intended to be indexer?

@@ -128,7 +128,7 @@ def has_horizontally_truncated_repr(df):
return False
# Make sure each row has this ... in the same place
r = repr(df)
for ix, l in enumerate(r.splitlines()):
for _ in r.splitlines():
Copy link
Member Author

Choose a reason for hiding this comment

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

Is this just testing that r can be split? The for loop is not needed!

@@ -956,7 +956,7 @@ def test_query_compare_column_type(setup_path):
with pytest.raises(ValueError, match=msg):
store.select("test", where=query)

for v, col in zip(
for v, col in zip( # noqa: B007
Copy link
Member 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 why v cannot be removed here.

Copy link
Member

Choose a reason for hiding this comment

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

best guess is because somewhere down the line the string on L962 is being passed to eval or exec

@MarcoGorelli MarcoGorelli self-requested a review March 23, 2021 20:47
@twoertwein
Copy link
Member Author

@MarcoGorelli I enabled almost all of bugbear's checks. The current way to handle B008 might decrease performance a bit (could either add ignore statements or have module-level variables).

@twoertwein twoertwein changed the title STY: unused loop variables, get/setattr with constant strings STY: enable flake8-bugbear Mar 26, 2021
@jreback jreback added the Code Style Code style, linting, code_checks label Mar 26, 2021
@@ -561,7 +561,11 @@ class AccessorCallableDocumenter(AccessorLevelDocumenter, MethodDocumenter):
priority = 0.5

def format_name(self):
return MethodDocumenter.format_name(self).rstrip(".__call__")
Copy link
Member

Choose a reason for hiding this comment

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

why is it complaining about this? this just seems more verbose

Copy link
Member Author

@twoertwein twoertwein Mar 28, 2021

Choose a reason for hiding this comment

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

I agree it is more verbose. I put the style changes into separate commits so that they can easily be dropped.

(r)strip removes an arbitrary set of characters. strip("_") is the same as strip("__"). Calling (r)strip with multiple characters might imply to the reader that exactly this substring is removed, which is not correct.

".....ccaaa__caller.__caller".rstrip(".__caller__") == ""

@@ -1651,7 +1651,7 @@ def _is_dtype_type(arr_or_dtype, condition) -> bool:

try:
tipo = pandas_dtype(arr_or_dtype).type
except (TypeError, ValueError, UnicodeEncodeError):
Copy link
Member

Choose a reason for hiding this comment

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

is this a leftover-from-py2 thing?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't know. UnicodeEncodeError is a sub-class of ValueError.

@@ -49,7 +49,7 @@ def _unpack_zerodim_and_defer(method, name: str):
-------
method
"""
is_cmp = name.strip("__") in {"eq", "ne", "lt", "le", "gt", "ge"}
Copy link
Member

Choose a reason for hiding this comment

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

it doesnt make a differnece, but this was intentional to only strip dunders

@@ -686,7 +686,7 @@ def test_time_series_plot_color_with_empty_kwargs(self):
ncolors = 3

_, ax = self.plt.subplots()
for i in range(ncolors):
for _ in range(ncolors):
Copy link
Member

Choose a reason for hiding this comment

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

maybe intended to pss the arg to s.plot?

Copy link
Member Author

Choose a reason for hiding this comment

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

The subplot seems to have only one axis. I think this test makes sure that matplotlib cycles through its colors when called multiple times.

@jbrockmendel
Copy link
Member

I like getting rid of unnecessary zip/enumerates, bug things like requiring unused loop variable to be "_" seem annoying

@twoertwein
Copy link
Member Author

I like getting rid of unnecessary zip/enumerates, bug things like requiring unused loop variable to be "_" seem annoying

I will separate the removal of unnecessary statements into a new PR later today.

@twoertwein
Copy link
Member Author

Would it make sense to have a new PR that enables only a smaller subset of rules that are less controversial?

Skip:
B005 (strip with multiple characters)
B006 (mutable default values)
B008 (function calls in default values)
B007 (unused loop variables)
B301 (iteritems - false positives)
and the already skipped rules in this PR

Personally, I would like to have B007 since it found unused zips/enumerates and weird tests. B005 would also be great: there is a comment in the function that uses rstrip(".__call__") that this function has a lower priority because it causes issues otherwise (speculating: might be because of this rstrip call).

@jreback
Copy link
Contributor

jreback commented Mar 31, 2021

sure happy to have some of these enabled in a non controversial PR

then can follow up enabling others

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.

4 participants