Add more typing to bugbear.py#532
Merged
Merged
Conversation
- Attempt to type all functions - Change namedtuples to tryuped NamedTuples - Add checks for optional / None variables - Lots of isinstance replacing getattr etc. - Add b005 to visit_ImportFrom - Found bug in check_for_b020 for reassigned_in_loop checking - Need to handle comprehensions A lot of AI here, so happy to revert anything that does not make sense. Test: - `pre-commit run -a` - `tox -e mypy`
Contributor
There was a problem hiding this comment.
Pull request overview
This PR comprehensively adds type annotations to bugbear.py, converting namedtuples to typed NamedTuples and replacing dynamic attribute checking with proper type guards. The changes improve type safety and code maintainability without altering the core functionality.
Key Changes
- Converted
Contextanderrornamedtuples to typedNamedTupleclasses with explicit field annotations - Added type annotations to all function signatures including visitor methods and check functions
- Replaced
hasattrcalls withisinstancechecks for better type narrowing (e.g., incheck_for_b017,check_for_b022) - Fixed a bug in
check_for_b023to properly handle comprehensions when detecting reassigned loop variables
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
A lot of AI here, so happy to revert anything that does not make sense.
Test:
pre-commit run -atox -e mypyDoes more for #478 - Want to get to strict before closing that