From 331e48f746543c948bc24f08f4696a667ca0f549 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Tue, 6 Jun 2023 15:19:41 -0400 Subject: [PATCH] Fix a crash when `__all__` exists but cannot be inferred (#8741) --- doc/whatsnew/fragments/8740.bugfix | 3 +++ pylint/checkers/variables.py | 5 ++++- .../u/undefined/undefined_all_variable_edge_case.py | 5 +++++ .../u/undefined/undefined_all_variable_edge_case.txt | 1 + 4 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 doc/whatsnew/fragments/8740.bugfix create mode 100644 tests/functional/u/undefined/undefined_all_variable_edge_case.py create mode 100644 tests/functional/u/undefined/undefined_all_variable_edge_case.txt diff --git a/doc/whatsnew/fragments/8740.bugfix b/doc/whatsnew/fragments/8740.bugfix new file mode 100644 index 0000000000..dfa0d24929 --- /dev/null +++ b/doc/whatsnew/fragments/8740.bugfix @@ -0,0 +1,3 @@ +Fix a crash when ``__all__`` exists but cannot be inferred. + +Closes #8740 diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index 5037b9229d..9cdec100cc 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -3046,7 +3046,10 @@ def _check_module_attrs( def _check_all( self, node: nodes.Module, not_consumed: dict[str, list[nodes.NodeNG]] ) -> None: - assigned = next(node.igetattr("__all__")) + try: + assigned = next(node.igetattr("__all__")) + except astroid.InferenceError: + return if isinstance(assigned, util.UninferableBase): return if assigned.pytype() not in {"builtins.list", "builtins.tuple"}: diff --git a/tests/functional/u/undefined/undefined_all_variable_edge_case.py b/tests/functional/u/undefined/undefined_all_variable_edge_case.py new file mode 100644 index 0000000000..647025f8d6 --- /dev/null +++ b/tests/functional/u/undefined/undefined_all_variable_edge_case.py @@ -0,0 +1,5 @@ +"""Edge case: __all__ exists in module's locals, but cannot be inferred. + +Other tests for undefined-all-variable in tests/functional/n/names_in__all__.py""" + +__all__ += [] # [undefined-variable] diff --git a/tests/functional/u/undefined/undefined_all_variable_edge_case.txt b/tests/functional/u/undefined/undefined_all_variable_edge_case.txt new file mode 100644 index 0000000000..54202984b6 --- /dev/null +++ b/tests/functional/u/undefined/undefined_all_variable_edge_case.txt @@ -0,0 +1 @@ +undefined-variable:5:0:5:7::Undefined variable '__all__':UNDEFINED