Skip to content

refactor: fix flake8-bugbear violations (B006/B008/B017/B023/B904). Closes #677#679

Merged
regulartim merged 5 commits intointelowlproject:developfrom
opbot-xd:refactor/fix-bugbear-violations
Jan 5, 2026
Merged

refactor: fix flake8-bugbear violations (B006/B008/B017/B023/B904). Closes #677#679
regulartim merged 5 commits intointelowlproject:developfrom
opbot-xd:refactor/fix-bugbear-violations

Conversation

@opbot-xd
Copy link
Copy Markdown
Contributor

@opbot-xd opbot-xd commented Jan 4, 2026

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 First

raise exc from None  # Suppress exception chain

Why: In LoginSerializer.validate(), we intentionally hide User.DoesNotExist to prevent user enumeration attacks. Using from None ensures no information leaks about whether a username exists in the database.

greedybear/cronjobs/repositories/cowrie_session.py - Debugging First

raise ValueError(...) from e  # Preserve exception chain

Why: In background data processing, we need full error context for debugging. When session_id parsing fails, preserving the original ValueError helps trace the root cause (e.g., "invalid literal for int() with base 16: 'gggggg'").

tests/test_repositories.py - Precision in Tests

with self.assertRaises(IntegrityError):  # Specific, not Exception

Why: Tests should be strict. If the code starts raising a different exception type (e.g., ValidationError instead of IntegrityError), we want the test to fail and catch the regression.

Related issues

#677
#640

Type of change

  • Bug fix (non-breaking change which fixes an issue).

Checklist

  • I have read and understood the rules about how to Contribute to this project.
  • The pull request is for the branch develop.
  • I have added documentation of the new features.
  • Linter (Ruff) gave 0 errors. If you have correctly installed pre-commit, it does these checks and adjustments on your behalf.
  • All the tests (old ones) gave 0 errors.
  • If changes were made to an existing model/serializer/view, the docs were updated and regenerated (check CONTRIBUTE.md).

- 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.
@opbot-xd
Copy link
Copy Markdown
Contributor Author

opbot-xd commented Jan 4, 2026

Update on File Count Estimates

In the initial issue, I estimated:

  • Phase 1 (B006/B008): ~10-15 files
  • Phase 2 (B017/B904): ~20-30 files
  • Phase 3 (B023): ~5-10 files
  • Total: ~35-55 files

Actual changes:

  • Phase 1: 1 file
  • Phase 2: 3 files
  • Phase 3: 1 file
  • Total: 5 files

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.

@opbot-xd
Copy link
Copy Markdown
Contributor Author

opbot-xd commented Jan 4, 2026

Hi @regulartim please can you review this PR when you get time?

@regulartim
Copy link
Copy Markdown
Collaborator

most of the codebase was already following these best practices.

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
@opbot-xd opbot-xd requested a review from regulartim January 5, 2026 08:07
Copy link
Copy Markdown
Collaborator

@regulartim regulartim left a comment

Choose a reason for hiding this comment

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

Nice work! Thank you!

@regulartim regulartim merged commit a7394ff into intelowlproject:develop Jan 5, 2026
5 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.

2 participants