refactor: fix flake8-bugbear violations (B006/B008/B017/B023/B904). Closes #677#679
Merged
regulartim merged 5 commits intointelowlproject:developfrom Jan 5, 2026
Conversation
- Replace empty list defaults with None in _create_mock_ioc() - Replace datetime.now() call in default argument with None - Initialize mutable defaults inside function to avoid shared state - Fixes flake8-bugbear violations B006 and B008 Phase 1 of bugbear violations fix. All tests passing (282/282).
- Add 'from None' to LoginSerializer to suppress exception context when re-raising ValidationError (intentionally hiding user existence) - Add 'from e' to CowrieSessionRepository to preserve exception chain when raising descriptive ValueError - Replace bare Exception with IntegrityError in test for database constraint violations Phase 2 of bugbear violations fix. All tests passing (282/282).
- Add default argument to lambda in multi_label_encode to capture loop variable correctly - Prevents late binding issue where all lambdas would reference the final loop value instead of capturing each iteration's value Phase 3 of bugbear violations fix. All tests passing (282/282).
- Remove B006, B008, B017, B023, and B904 from ignore list - All bugbear violations have been fixed in previous commits - Enforces proper exception handling, mutable defaults, and lambda patterns Phase 4 (final) of bugbear violations fix. All tests passing (282/282). All ruff checks passing.
Contributor
Author
Update on File Count EstimatesIn the initial issue, I estimated:
Actual changes:
Why the Overestimation?The initial estimates were based on potential violations the linter could catch, not actual violations in the codebase. It turns out the ignore list in .ruff.toml was overly defensive - most of the codebase was already following these best practices. |
Contributor
Author
|
Hi @regulartim please can you review this PR when you get time? |
regulartim
requested changes
Jan 4, 2026
Collaborator
Good to hear that! :D |
Use ternary operators directly in mock assignments instead of separate if-else blocks for a more concise and Pythonic approach. Co-authored-by: regulartim
regulartim
approved these changes
Jan 5, 2026
Collaborator
regulartim
left a comment
There was a problem hiding this comment.
Nice work! Thank you!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes all remaining flake8-bugbear violations and enables enforcement of B006, B008, B017, B023, and B904 rules.
Exception Handling Rationale (B904/B017)
The key design decision was context-specific exception handling:
authentication/serializers.py- Security FirstWhy: In
LoginSerializer.validate(), we intentionally hideUser.DoesNotExistto prevent user enumeration attacks. Usingfrom Noneensures no information leaks about whether a username exists in the database.greedybear/cronjobs/repositories/cowrie_session.py- Debugging FirstWhy: In background data processing, we need full error context for debugging. When
session_idparsing fails, preserving the originalValueErrorhelps trace the root cause (e.g., "invalid literal for int() with base 16: 'gggggg'").tests/test_repositories.py- Precision in TestsWhy: Tests should be strict. If the code starts raising a different exception type (e.g.,
ValidationErrorinstead ofIntegrityError), we want the test to fail and catch the regression.Related issues
#677
#640
Type of change
Checklist
develop.Ruff) gave 0 errors. If you have correctly installed pre-commit, it does these checks and adjustments on your behalf.