- 
                Notifications
    
You must be signed in to change notification settings  - Fork 110
 
Add B043: Do not call delattr with constant #514
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
base: main
Are you sure you want to change the base?
Conversation
| 
           @cooperlees check my PR please  | 
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds a new linting rule B043 that detects when delattr() is called with a constant string attribute name, suggesting to use direct attribute deletion instead.
- Adds rule B043 to detect 
delattr(obj, "constant")usage patterns - Provides test cases demonstrating valid and invalid usage patterns
 - Updates documentation to explain the rule and its rationale
 
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description | 
|---|---|
| tests/eval_files/b043.py | Test cases showing valid dynamic usage vs invalid constant usage of delattr | 
| bugbear.py | Core logic to detect delattr calls with constant string arguments and error definition | 
| README.rst | Documentation explaining B043 rule and recommending del x.attr over delattr(x, 'attr') | 
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| "B043": Error( | ||
| message=( | ||
| "B043 Do not call delattr with a constant attribute value, " | ||
| "it is not any safer than normal property access." | 
    
      
    
      Copilot
AI
    
    
    
      Sep 8, 2025 
    
  
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message mentions 'normal property access' but the recommended alternative is attribute deletion (del x.attr), not property access. Consider changing to 'it is not any safer than normal attribute deletion.'
| "it is not any safer than normal property access." | |
| "it is not any safer than normal attribute deletion." | 
| 
               | 
          ||
| .. _B043: | ||
| 
               | 
          ||
| **B043**: Do not call ``delattr(x, 'attr')``, instead use ``del x.attr``. | 
    
      
    
      Copilot
AI
    
    
    
      Sep 8, 2025 
    
  
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a trailing space after the period on line 293. Remove the trailing whitespace.
| **B043**: Do not call ``delattr(x, 'attr')``, instead use ``del x.attr``. | |
| **B043**: Do not call ``delattr(x, 'attr')``, instead use ``del x.attr``. | 
close #513
I used B043 because I see PR with B042