Skip to content
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

Ensure naming-convention-violation does not check variables in main block #989

Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

- Fix bug in ending location setting for `Attribute` and `DelAttr` nodes when the same attribute
was accessed twice on the same line.
- Fix bug where the `naming-convention-violation` checker was checking variables defined in a module's main block. This was inconsistent with the `forbidden-global-variables` checker.

## [2.6.4] - 2024-11-10

Expand Down
4 changes: 3 additions & 1 deletion python_ta/checkers/invalid_name_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from pylint.checkers.utils import only_required_for_messages
from pylint.lint import PyLinter

from python_ta.utils import _is_in_main

# Bad variable names.
BAD_NAMES = {"l", "I", "O"}

Expand Down Expand Up @@ -334,7 +336,7 @@ def visit_assignname(self, node: nodes.AssignName) -> None:
self._check_name("argument", node.name, node)

# Check names defined in module scope
elif isinstance(frame, nodes.Module):
elif isinstance(frame, nodes.Module) and not _is_in_main(node):
# Check names defined in Assign nodes
if isinstance(assign_type, nodes.Assign):
inferred_assign_type = utils.safe_infer(assign_type.value)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_custom_checkers/test_invalid_name_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,18 @@ def test_typealias_name_underscore(self) -> None:
with self.assertNoMessages():
self.checker.visit_assignname(assignname_node)

def test_name_in_main_block(self) -> None:
"""Test that the checker does not report a top-level variable that is assigned within
a main block."""
src = """
if __name__ == '__main__':
const_not_upper = "it is not"
"""
mod = astroid.parse(src)
assignname_node, *_ = mod.nodes_of_class(nodes.AssignName)
with self.assertNoMessages():
self.checker.visit_assignname(assignname_node)


def test_module_name_no_snippet() -> None:
"""Test that PythonTA does not build a snippet for the message added by this checker."""
Expand Down
Loading