Skip to content
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
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
5 changes: 5 additions & 0 deletions doc/whatsnew/2.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
10 changes: 8 additions & 2 deletions pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
15 changes: 15 additions & 0 deletions tests/functional/u/undefined/undefined_variable_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
2 changes: 2 additions & 0 deletions tests/functional/u/undefined/undefined_variable_typing.rc
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
[testoptions]
except_implementations=PyPy
[VARIABLES]
additional-builtins=__additional_builtin__