Skip to content

Start enforcing bugbear rules (B) #1602

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

Merged
merged 8 commits into from
May 28, 2024
Merged

Conversation

DimitriPapadopoulos
Copy link
Contributor

No description provided.

Copy link
Contributor

@Skylion007 Skylion007 left a comment

Choose a reason for hiding this comment

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

1 major bug in fsspec caching. Otherwise, looks good to me. Can we also enable the 'B' checks in ruff in pyproject.toml?

@Skylion007
Copy link
Contributor

Please uncomment this line to enable the bugbear rules:

# "B", enable in later PR

@DimitriPapadopoulos
Copy link
Contributor Author

DimitriPapadopoulos commented May 22, 2024

We're not there yet, the following rules have not yet been applied:

My fear was that the maintainers wouldn't accept all rules, so I started with the most useful. Do you think I should enforce them all?

@martindurant
Copy link
Member

I believe going piecemeal is the best approach, as you have been doing

@DimitriPapadopoulos
Copy link
Contributor Author

What I can do is enable B rules in general, and ignore the above specific rules.

@@ -574,7 +574,7 @@ def __init__(
blocksize: int,
fetcher: Fetcher,
size: int,
data: dict[tuple[int, int], bytes] = {},
data: dict[tuple[int, int], bytes] = None,
Copy link
Member

Choose a reason for hiding this comment

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

The type hint is now wrong. It could be made into a class attribute hint instead...

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've added Optional[] for consistency with the other arguments, which all have a type hint.

@@ -381,7 +381,7 @@ def client(self):
return mock

def func_2(self):
assert False, "have to overwrite this"
assert False, "have to overwrite this" # noqa: B011
Copy link
Member

Choose a reason for hiding this comment

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

raise AssertError ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Or perhaps disable this rule in all tests?

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 certainly a special case, it's not normal to assert False! On second thoughts, I'm OK with the original noqa too.

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 changed to raise AssertionError in the mean time. Is that OK?

Copy link
Member

Choose a reason for hiding this comment

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

In the end, I think both are fine :)

@DimitriPapadopoulos DimitriPapadopoulos force-pushed the B branch 4 times, most recently from 164564b to 62ad8e0 Compare May 24, 2024 07:54
@martindurant
Copy link
Member

I see you added additional rules one by one - is the process now finished?

@DimitriPapadopoulos
Copy link
Contributor Author

Yes, I'm finished in the near term, the remaining B rules have been disabled. They might be enabled in the future, but I am not certain you'll like them all.

B015 Pointless comparison. Did you mean to assign a value?
Otherwise, prepend `assert` or remove it.
B033 Sets should not contain duplicate item `"..."`
B006 Do not use mutable data structures for argument defaults
B018 Found useless expression. Either assign it to a variable or remove it.
B020 Loop control variable overrides iterable it iterates
B034 `re.split` should pass `maxsplit` and `flags` as keyword arguments
     to avoid confusion due to unintuitive argument positions
B011 Do not `assert False` (`python -O` removes these calls),
     raise `AssertionError()`
For now, ignore these:
- B007
- B019
- B026
- B028
- B904
@@ -139,7 +139,7 @@ def _fetch_range(self, start, end):
try:
import snappy

snappy.compress
snappy.compress(b"")
Copy link
Contributor

Choose a reason for hiding this comment

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

Wait, this one isn't quite right, it's an attribute look up that could be an assert statement I suppose

Copy link
Contributor Author

@DimitriPapadopoulos DimitriPapadopoulos May 25, 2024

Choose a reason for hiding this comment

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

Indeed, I guess the idea of @martindurant was that the compress(b"") call cannot fail; any error has to be an attribute look up. I can revert to the pure attribute look up with # noqa - I agree it makes the intent clearer.

Copy link
Member

@martindurant martindurant May 28, 2024

Choose a reason for hiding this comment

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

An assert in regular code is not recommended, since they can be turned off for the whole interpreter session. I think the function call is fine, or if we wanted a different function call to appease linting, we could do a getattr.

Background: python-snappy is not the only installable to provide a package called snappy (not my fault!), so this is a way to differentiate. Actually, snappy-compressing whole files (as opposed to buffers within files) is super-rare, so we could perhaps even remove this, rather than increase import time by checking this. We should probably not be importing anything at import time!

In [1]: import fsspec
In [2]: import sys
In [3]: "snappy" in sys.modules
Out[3]: True

I intend to merge this PR as is; @DimitriPapadopoulos or @Skylion007 , you are most welcome to make a PR to defer imports in the fsspec.compression module.

@martindurant martindurant merged commit 0a37adc into fsspec:master May 28, 2024
11 checks passed
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.

3 participants