-
-
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?
Changes from all commits
d5070be
6e9c650
a6912ba
dc85a9e
01a94d9
5fc003c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Fix a false positive when an UPPER_CASED class attribute was raising an | ||
| ``invalid-name`` when typed with ``Final``. | ||
|
|
||
| Closes #10711 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| """Regression test for https://github.com/pylint-dev/pylint/issues/10711.""" | ||
|
|
||
| # pylint: disable=missing-class-docstring, missing-function-docstring | ||
|
|
||
| from dataclasses import dataclass | ||
| from typing import Final | ||
|
|
||
| module_snake_case_constant: Final[int] = 42 # [invalid-name] | ||
| MODULE_UPPER_CASE_CONSTANT: Final[int] = 42 | ||
|
|
||
|
|
||
| def function() -> None: | ||
| function_snake_case_constant: Final[int] = 42 | ||
| FUNCTION_UPPER_CASE_CONSTANT: Final[int] = 42 # [invalid-name] | ||
| print(function_snake_case_constant, FUNCTION_UPPER_CASE_CONSTANT) | ||
|
|
||
|
|
||
| @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 commentThe reason will be displayed to describe this comment to others. Learn more. This line should be OK. Stacking them together should not raise any
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ... right here. (I agree with you) Everything else in this file should be like Final doesn't even exist. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest adding a just CLASS_UPPER_CASE_ATTRIBUTE: int = 42This line should not raise But later on if it gets re-assigned new value in the below |
||
|
|
||
| def method(self) -> None: | ||
| method_snake_case_constant: Final[int] = 42 | ||
| METHOD_UPPER_CASE_CONSTANT: Final[int] = 42 # [invalid-name] | ||
| print(method_snake_case_constant, METHOD_UPPER_CASE_CONSTANT) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| invalid-name:8:0:8:26::"Constant name ""module_snake_case_constant"" doesn't conform to UPPER_CASE naming style":HIGH | ||
| invalid-name:14:4:14:32:function:"Variable name ""FUNCTION_UPPER_CASE_CONSTANT"" doesn't conform to snake_case naming style":HIGH | ||
| invalid-name:20:4:20:29:Class:"Class constant name ""class_snake_case_constant"" doesn't conform to UPPER_CASE naming style":HIGH | ||
| invalid-name:25:8:25:34:Class.method:"Variable name ""METHOD_UPPER_CASE_CONSTANT"" doesn't conform to snake_case naming style":HIGH |
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], becauseFinalattributes can still be re-assigned a new value that is different from the default value in__init__().Finalonly protects the value after the instance is created.Finalis likeconstin C/C++.UPPER_CASEis like the marco which gets expanded during static interpretation of the code.So
UPPER_CASEis more immutable thanFinal.So marking a dataclass attribute
Finalis not the sufficient condition forUPPER_CASE.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.
Let's discuss that somewhere else. I don't want to make
Finalmore special than it already is. The only thing we grok about Final is ...