diff --git a/ChangeLog b/ChangeLog index f837483dad..b129d6720d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -220,6 +220,11 @@ Release date: TBA Closes #5815 Closes #5406 +* Fixed a false positive for ``unused-variable`` when a builtin specified in + ``--additional-builtins`` is given a type annotation. + + Closes #6388 + * Fixed an ``AstroidError`` in 2.13.0 raised by the ``duplicate-code`` checker with ``ignore-imports`` or ``ignore-signatures`` enabled. diff --git a/doc/whatsnew/2.13.rst b/doc/whatsnew/2.13.rst index dc0f9e6b25..e3476a0c33 100644 --- a/doc/whatsnew/2.13.rst +++ b/doc/whatsnew/2.13.rst @@ -592,6 +592,11 @@ Other Changes Closes #5394 +* Fixed a false positive for ``unused-variable`` when a builtin specified in + ``--additional-builtins`` is given a type annotation. + + Closes #6388 + * Fix false positive for 'nonexistent-operator' when repeated '-' are separated (e.g. by parens). diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index a4858ec91e..33743598b0 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -2059,12 +2059,18 @@ def _maybe_used_and_assigned_at_once(defstmt: nodes.Statement) -> bool: or any(isinstance(arg, nodes.IfExp) for arg in value.args) ) - @staticmethod - def _is_only_type_assignment(node: nodes.Name, defstmt: nodes.Statement) -> bool: + def _is_only_type_assignment( + self, node: nodes.Name, defstmt: nodes.Statement + ) -> bool: """Check if variable only gets assigned a type and never a value.""" if not isinstance(defstmt, nodes.AnnAssign) or defstmt.value: return False + if node.name in self.linter.config.additional_builtins or utils.is_builtin( + node.name + ): + return False + defstmt_frame = defstmt.frame(future=True) node_frame = node.frame(future=True) diff --git a/tests/functional/u/undefined/undefined_variable_typing.py b/tests/functional/u/undefined/undefined_variable_typing.py index aa24362d30..7ef4d85998 100644 --- a/tests/functional/u/undefined/undefined_variable_typing.py +++ b/tests/functional/u/undefined/undefined_variable_typing.py @@ -4,9 +4,24 @@ # Ensure attribute lookups in type comments are accounted for. # Reported in https://github.com/PyCQA/pylint/issues/4603 +from typing import TYPE_CHECKING, Any, Dict + import foo from foo import Bar, Boo a = ... # type: foo.Bar b = ... # type: foo.Bar[Boo] c = ... # type: Bar.Boo + + +if TYPE_CHECKING: + __additional_builtin__: Dict[str, Any] + # For why this would emit redefined-builtin: https://github.com/PyCQA/pylint/pull/3643 + # pylint: disable-next=redefined-builtin + repr: Any + + +def run(): + """https://github.com/PyCQA/pylint/issues/6388""" + print(repr) + return __additional_builtin__["test"] diff --git a/tests/functional/u/undefined/undefined_variable_typing.rc b/tests/functional/u/undefined/undefined_variable_typing.rc index b47a745259..2579c91dc5 100644 --- a/tests/functional/u/undefined/undefined_variable_typing.rc +++ b/tests/functional/u/undefined/undefined_variable_typing.rc @@ -1,2 +1,4 @@ [testoptions] except_implementations=PyPy +[VARIABLES] +additional-builtins=__additional_builtin__