Skip to content

Apply const-naming-style to module const annotated with Final #4280

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

Merged
merged 1 commit into from
Apr 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ Release date: Undefined
..
Put new features here and also in 'doc/whatsnew/2.8.rst'

* Apply ``const-naming-style`` to module constants annotated with
``typing.Final``
Comment on lines +12 to +13
Copy link
Member Author

@cdce8p cdce8p Apr 1, 2021

Choose a reason for hiding this comment

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

Should it be a 2.8 or a 2.7.5 feature?

Copy link
Member

Choose a reason for hiding this comment

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

Depends on the end result of the discussion in #4263 :)



What's New in Pylint 2.7.5?
===========================
Expand Down
4 changes: 4 additions & 0 deletions pylint/checkers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1983,6 +1983,10 @@ def visit_assignname(self, node):
self._check_name("const", node.name, node)
elif isinstance(assign_type, astroid.ExceptHandler):
self._check_name("variable", node.name, node)
elif isinstance(
assign_type, astroid.AnnAssign
) and utils.is_assign_name_annotated_with(node, "Final"):
self._check_name("const", node.name, node)
elif isinstance(frame, astroid.FunctionDef):
# global introduced variable aren't in the function locals
if node.name in frame and node.name not in frame.argnames():
Expand Down
3 changes: 3 additions & 0 deletions tests/functional/n/name/name_final.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ class Foo:
CLASS_CONST3: typing.Final
variable2: typing.Final[int] # [invalid-name]
CLASS_CONST4: Final[typing.ClassVar[str]] = "valid"

MODULE_CONST: Final = 1
module_var: typing.Final[str] = "const" # [invalid-name]
1 change: 1 addition & 0 deletions tests/functional/n/name/name_final.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
invalid-name:10:4:Foo:"Class constant name ""variable"" doesn't conform to UPPER_CASE naming style"
invalid-name:12:4:Foo:"Class constant name ""variable2"" doesn't conform to UPPER_CASE naming style"
invalid-name:16:0::"Constant name ""module_var"" doesn't conform to UPPER_CASE naming style"
3 changes: 3 additions & 0 deletions tests/functional/n/name/name_final_snake_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ class Foo:
CLASS_CONST3: typing.Final # [invalid-name]
variable2: typing.Final[int]
CLASS_CONST4: Final[typing.ClassVar[str]] = "invalid name" # [invalid-name]

MODULE_CONST: Final = 1 # [invalid-name]
module_var: typing.Final[str] = "const"
1 change: 1 addition & 0 deletions tests/functional/n/name/name_final_snake_case.rc
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ min_pyver=3.8

[BASIC]
class-const-naming-style=snake_case
const-naming-style=snake_case
1 change: 1 addition & 0 deletions tests/functional/n/name/name_final_snake_case.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ invalid-name:8:4:Foo:"Class constant name ""CLASS_CONST"" doesn't conform to sna
invalid-name:9:4:Foo:"Class constant name ""CLASS_CONST2"" doesn't conform to snake_case naming style"
invalid-name:11:4:Foo:"Class constant name ""CLASS_CONST3"" doesn't conform to snake_case naming style"
invalid-name:13:4:Foo:"Class constant name ""CLASS_CONST4"" doesn't conform to snake_case naming style"
invalid-name:15:0::"Constant name ""MODULE_CONST"" doesn't conform to snake_case naming style"