Skip to content

Commit 620347a

Browse files
Suppress unsubscriptable-object warnings in type-checking blocks (#5720)
* Add a regression test for issue #3979 * Don't emit `unsubscriptable-object` for statements in type-checking blocks Co-authored-by: Jacob Walls <[email protected]>
1 parent a1e7afb commit 620347a

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ Release date: TBA
6262

6363
Closes #4525
6464

65+
* Fix false positive for ``unsubscriptable-object`` in Python 3.8 and below for
66+
statements guarded by ``if TYPE_CHECKING``.
67+
68+
Closes #3979
69+
6570
* Fix false negative for ``no-member`` when attempting to assign an instance
6671
attribute to itself without any prior assignment.
6772

doc/whatsnew/2.14.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ Other Changes
8181

8282
Closes #5205
8383

84+
* Fix false positive for ``unsubscriptable-object`` in Python 3.8 and below for
85+
statements guarded by ``if TYPE_CHECKING``.
86+
87+
Closes #3979
88+
8489
* Fix false negative for ``no-member`` when attempting to assign an instance
8590
attribute to itself without any prior assignment.
8691

pylint/checkers/typecheck.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1951,7 +1951,11 @@ def visit_subscript(self, node: nodes.Subscript) -> None:
19511951
return # It would be better to handle function
19521952
# decorators, but let's start slow.
19531953

1954-
if supported_protocol and not supported_protocol(inferred, node):
1954+
if (
1955+
supported_protocol
1956+
and not supported_protocol(inferred, node)
1957+
and not utils.in_type_checking_block(node)
1958+
):
19551959
self.add_message(msg, args=node.value.as_string(), node=node.value)
19561960

19571961
@check_messages("dict-items-missing-iter")
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""
2+
Regression test for https://github.com/PyCQA/pylint/issues/3979
3+
"""
4+
5+
import os
6+
from typing import TYPE_CHECKING, Any, Union
7+
8+
if TYPE_CHECKING:
9+
BasePathLike = os.PathLike[Any] # <-- pylint used to emit E1136 here
10+
else:
11+
BasePathLike = os.PathLike
12+
13+
foo: Union[str, BasePathLike] = "bar"

0 commit comments

Comments
 (0)