Skip to content

Commit

Permalink
Suppress unsubscriptable-object warnings in type-checking blocks (#…
Browse files Browse the repository at this point in the history
…5720)

* Add a regression test for issue #3979

* Don't emit `unsubscriptable-object` for statements in type-checking blocks

Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
  • Loading branch information
Pierre-Sassoulas and jacobtylerwalls committed Apr 9, 2022
1 parent a1e7afb commit 620347a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions ChangeLog
Expand Up @@ -62,6 +62,11 @@ Release date: TBA

Closes #4525

* Fix false positive for ``unsubscriptable-object`` in Python 3.8 and below for
statements guarded by ``if TYPE_CHECKING``.

Closes #3979

* Fix false negative for ``no-member`` when attempting to assign an instance
attribute to itself without any prior assignment.

Expand Down
5 changes: 5 additions & 0 deletions doc/whatsnew/2.14.rst
Expand Up @@ -81,6 +81,11 @@ Other Changes

Closes #5205

* Fix false positive for ``unsubscriptable-object`` in Python 3.8 and below for
statements guarded by ``if TYPE_CHECKING``.

Closes #3979

* Fix false negative for ``no-member`` when attempting to assign an instance
attribute to itself without any prior assignment.

Expand Down
6 changes: 5 additions & 1 deletion pylint/checkers/typecheck.py
Expand Up @@ -1951,7 +1951,11 @@ def visit_subscript(self, node: nodes.Subscript) -> None:
return # It would be better to handle function
# decorators, but let's start slow.

if supported_protocol and not supported_protocol(inferred, node):
if (
supported_protocol
and not supported_protocol(inferred, node)
and not utils.in_type_checking_block(node)
):
self.add_message(msg, args=node.value.as_string(), node=node.value)

@check_messages("dict-items-missing-iter")
Expand Down
13 changes: 13 additions & 0 deletions tests/functional/r/regression_02/regression_3979.py
@@ -0,0 +1,13 @@
"""
Regression test for https://github.com/PyCQA/pylint/issues/3979
"""

import os
from typing import TYPE_CHECKING, Any, Union

if TYPE_CHECKING:
BasePathLike = os.PathLike[Any] # <-- pylint used to emit E1136 here
else:
BasePathLike = os.PathLike

foo: Union[str, BasePathLike] = "bar"

0 comments on commit 620347a

Please sign in to comment.