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

Implement flake8-bugbear #389

Closed
charliermarsh opened this issue Oct 10, 2022 · 34 comments
Closed

Implement flake8-bugbear #389

charliermarsh opened this issue Oct 10, 2022 · 34 comments
Labels
rule Implementing or modifying a lint rule

Comments

@charliermarsh
Copy link
Member

This is a big set of rules but flake8-bugbear is extremely popular (~1.5M downloads per month).

@tgross35
Copy link
Contributor

Link to bugbear warnings for reference: https://github.com/PyCQA/flake8-bugbear#list-of-warnings

B001, B002, B006, B007, B008, B017, B023, and B904 are the most useful for catching real bugs imho, out of the warnings that are not yet implemented.

B950 covers line length - it is not really necessary since ruff is already designed to work with black. However, it may be good thing to mention somewhere since black mentions configuring it explicitly.

(awesome project by the way!)

@charliermarsh
Copy link
Member Author

Cool, those are all pretty straightforward! Will prioritize them over the others.

@harupy
Copy link
Contributor

harupy commented Oct 30, 2022

I'm working on B006 implementation.

This was referenced Oct 30, 2022
@tgross35
Copy link
Contributor

tgross35 commented Nov 4, 2022

Quick checklist for convenience:

  • B001: bare except
  • B002: ++ -> +=
  • B003: os.environ assigning
  • B004: hasattr(x, '__call__') to test if x is callable
  • B005: .strip() with multi-character strings
  • B006: mutable data structures for argument defaults
  • B007: Loop control variable not used within the loop body
  • B008: function calls in argument defaults
  • B009: getattr(x, 'attr') -> x.attr
  • B010: setattr(x, 'attr', val) -> x.attr = val
  • B011: assert False calls
  • B012: break, continue or return inside finally blocks
  • B013: length-one tuple literal is redundant
  • B014: Redundant exception types in except (Exception, TypeError)
  • B015: Pointless comparison
  • B016: Cannot raise a literal
  • B017: self.assertRaises(Exception)
  • B018: Found useless expression
  • B019: Use of functools.lru_cache or functools.cache on methods
  • B020: Loop control variable overrides iterable it iterates
  • B021: f-string used as docstring
  • B022: arguments passed to contextlib.suppress
  • B023: functions defined inside a loop using variables redefined in the loop
  • B024: Abstract base class with no abstract method
  • B025: try-except block with duplicate exceptions
  • B026: Star-arg unpacking after a keyword argument is strongly discouraged
  • B027: Empty method in abstract base class

Opinionated:

  • B901: return x in a generator function
  • B902: invalid first argument for method. Use self for instance methods, and cls for class methods
  • B903: Use collections.namedtuple (or typing.NamedTuple) for data classes that only set attributes in an init method
  • B904: Within an except clause, raise exceptions with raise ... from err or raise ... from None
  • B950: Line too long, generous option

@charliermarsh
Copy link
Member Author

That's awesome, thank you for collating it!

I'm tentatively marking B001 and B950 as complete, since IIUC B001 is the same as E722, and B950 is close enough to E501 that we may not support it independently (I know they're not exactly the same).

This was referenced Nov 5, 2022
@charliermarsh
Copy link
Member Author

Just updated the list as @harupy is making awesome progress here. We're now at 50% coverage.

@grillazz
Copy link

@charliermarsh for B008 is there any simple way to setup extend-immutable-calls = fastapi.Depends, fastapi.params.Depends ?

@edgarrmondragon
Copy link
Contributor

edgarrmondragon commented Nov 12, 2022

@grillazz It's rather straightforward to add. I'm working on it. PR is #706 🙂

@grillazz
Copy link

Test 0.0.120 and problem solved. Thx

@harupy harupy mentioned this issue Nov 15, 2022
@charliermarsh
Copy link
Member Author

@harupy is dangerously close to implementing the entire plugin ;)

@harupy
Copy link
Contributor

harupy commented Nov 16, 2022

Nice to see a task list with lots of ✅ :)

This was referenced Nov 20, 2022
@charliermarsh
Copy link
Member Author

I think if we implement B023, we should feel comfortable closing this.

@harupy harupy mentioned this issue Nov 23, 2022
@harupy
Copy link
Contributor

harupy commented Nov 24, 2022

I think if we implement B023, we should feel comfortable closing this.

Agreed! Feel free to implement it if anyone wants to :)

@charliermarsh
Copy link
Member Author

@harupy - Sounds good, I can give it a try!

@harupy
Copy link
Contributor

harupy commented Nov 25, 2022

Awesome, thank you!

@harupy
Copy link
Contributor

harupy commented Nov 25, 2022

Is B902 covered by N804 and N805 in pep8-namiing?

@charliermarsh
Copy link
Member Author

@harupy - Yeah I believe so.

@g-as
Copy link
Contributor

g-as commented Dec 7, 2022

B905 was just added:

B905: zip() without an explicit strict= parameter set. strict=True causes the resulting iterator to raise a ValueError if the arguments are exhausted at differing lengths. The strict= argument was added in Python 3.10, so don't enable this flag for code that should work on <3.10. For more information: https://peps.python.org/pep-0618/

@charliermarsh
Copy link
Member Author

@g-as -- Added in #1122.

@SRv6d
Copy link

SRv6d commented Jan 15, 2023

@charliermarsh Are there still plans on supporting B950 ? Enforcing a line length while at the same time giving some headroom is often a pretty good compromise between sane line length and unnecessarily ugly code resulting from a hard limit.

@charliermarsh
Copy link
Member Author

We could support it. I was a bit hesitant but we could. Can you talk me through the rationale of using B950 vs. just configuring E501 to use a 10% increase? I think they're exactly equivalent, so I'm guessing it's more of a philosophical thing?

@SRv6d
Copy link

SRv6d commented Jan 15, 2023

I wouldn't call it philosophical, it mostly comes down to personal preference and experience. I think a line length of 88 is a great middle ground, but personally find my code to often be right around that or slightly over. Reformatting those lines of code just because they are a couple of characters over the limit often looks silly and increasing the limit by 10% we are getting close to 100 which results in an entirely different looking codebase just for a couple of outliers.

@charliermarsh
Copy link
Member Author

Sorry -- didn't intend "philosophical" to come off as pejorative so apologies if it did. Was mostly trying to confirm that, functionally, using B950 with line-length of 88 is identical to using E501 with a line-length of 96.8, right?

Assuming that's correct, is the difference here that if you treat 88 as the line length (when in reality, the enforced limit is 96.8), then folks set that as the line-length in their editors, tend to format around it, Black gets configured with 88, and so on?

@SRv6d
Copy link

SRv6d commented Jan 15, 2023

No offense taken whatsoever. The latter is exactly what I meant to say.

@SRv6d
Copy link

SRv6d commented Jan 27, 2023

@charliermarsh Would you be open to implementing B950 ? It's the only thing missing for me coming from flake8. I could give it a try, but I have no rust experience (yet?).

@jamesbraza
Copy link
Contributor

jamesbraza commented Apr 30, 2023

@SRv6d to make E501 behave like B950 I am just using this:

[tool.ruff]

# Line length to use when enforcing long-lines violations (like `E501`).
line-length = 97  # ceil(1.1 * 88) makes `E501` equivalent to `B950`

abkfenris added a commit to xpublish-community/xpublish-opendap that referenced this issue Apr 30, 2023
Thanks @ocefpaf for the suggestions and the fact that I could swipe the actions config from erddapy.

Needed to tweak bugbear settings to play nice with FastAPI. astral-sh/ruff#389
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
Development

No branches or pull requests

8 participants