Skip to content

Commit

Permalink
Fix bad node given to a message and false positive invalide format fo…
Browse files Browse the repository at this point in the history
…r ``__all__`` (#4953)

* Fix bad node being given as context for message

See #4711

* Add a functional test for issue #4711

* Fix false positive invalid-all-format

Closes #4711
  • Loading branch information
Pierre-Sassoulas committed Sep 13, 2021
1 parent a4ee38e commit 9ec8cb9
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Expand Up @@ -83,6 +83,10 @@ Release date: TBA
Closes #1375
Closes #330

* Fix false positives for invalid-all-format that are lists or tuples at runtime

Closes #4711

* Fix ``no-self-use`` and ``docparams extension`` for async functions and methods.

* Add documentation for ``pyreverse`` and ``symilar``
Expand Down
9 changes: 4 additions & 5 deletions pylint/checkers/variables.py
Expand Up @@ -2013,15 +2013,14 @@ def _check_module_attrs(self, node, module, module_names):
return module
return None

def _check_all(self, node, not_consumed):
def _check_all(self, node: nodes.Module, not_consumed):
assigned = next(node.igetattr("__all__"))
if assigned is astroid.Uninferable:
return

if not isinstance(assigned, (nodes.Tuple, nodes.List, list, tuple)):
self.add_message("invalid-all-format", node=assigned)
if not assigned.pytype() in ["builtins.list", "builtins.tuple"]:
line, col = assigned.tolineno, assigned.col_offset
self.add_message("invalid-all-format", line=line, col_offset=col, node=node)
return

for elt in getattr(assigned, "elts", ()):
try:
elt_name = next(elt.infer())
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/i/invalid/invalid_all_format.txt
@@ -1 +1 @@
invalid-all-format:5:11::Invalid format for __all__, must be tuple or list
invalid-all-format:5:11::Invalid format for __all__, must be tuple or list:HIGH
7 changes: 7 additions & 0 deletions tests/functional/i/invalid/invalid_all_format_valid_5.py
@@ -0,0 +1,7 @@
"""Test valid __all__ format."""

# pylint: disable=import-error, unused-import

from foo import bar

__all__ = list(globals().keys())
7 changes: 7 additions & 0 deletions tests/functional/i/invalid/invalid_all_format_valid_6.py
@@ -0,0 +1,7 @@
"""Test valid __all__ format."""

# pylint: disable=import-error, unused-import

from foo import bar

__all__ = tuple(globals().keys())

0 comments on commit 9ec8cb9

Please sign in to comment.