- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 1.2k
 
Fix a false positive for class attribute typed with Final #10712
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
| 
           Do you have any thoughts on the expected messages Jacob ?  | 
    
| 
               | 
          ||
| @dataclass | ||
| class Class: | ||
| class_snake_case_constant: Final[int] = 42 # [invalid-name] | 
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.
This line should not raise [invalid-name], because Final attributes can still be re-assigned a new value that is different from the default value in __init__(). Final only protects the value after the instance is created.
Final is like const in C/C++. UPPER_CASE is like the marco which gets expanded during static interpretation of the code.
So UPPER_CASE is more immutable than Final.
So marking a dataclass attribute Final is not the sufficient condition for UPPER_CASE.
| @dataclass | ||
| class Class: | ||
| class_snake_case_constant: Final[int] = 42 # [invalid-name] | ||
| CLASS_UPPER_CASE_CONSTANT: Final[int] = 42 | 
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.
This line should be OK. Stacking them together should not raise any [invalid-name].
| @dataclass | ||
| class Class: | ||
| class_snake_case_constant: Final[int] = 42 # [invalid-name] | ||
| CLASS_UPPER_CASE_CONSTANT: Final[int] = 42 | 
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.
I suggest adding a just UPPER_CASE attribute test case like:
CLASS_UPPER_CASE_ATTRIBUTE: int = 42This line should not raise [invalid-name] here, because UPPER_CASE can be used alone to identify a static constant.
But later on if it gets re-assigned new value in the below method, then [invalid-name] should be raised there to ask for snake_case because it is no longer a constant.
| CLASS_UPPER_CASE_CONSTANT: Final[int] = 42 | ||
| 
               | 
          ||
| def method(self) -> None: | ||
| method_snake_case_constant: Final[int] = 42 # [invalid-name] | 
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.
I suggest adding a test case for
self.CLASS_UPPER_CASE_ATTRIBUTE = 36 # [invalid-name]
Because this attribute is named with UPPER_CASE letters, it should not be re-assigned a new value by any means. If it is re-assigned a new value, we can safely raise [invalid-name] on the spot and asking for snake_case names instead.
| 
           I'll aim to take a look this week at fixing this.  | 
    
Type of Changes
Description
Closes #10711