Skip to content

Commit ec2eee8

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 135477a commit ec2eee8

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

ChangeLog

+5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ Release date: TBA
4444

4545
Closes #4020
4646

47+
* Fix false positive for ``unsubscriptable-object`` in Python 3.8 and below for
48+
statements guarded by ``if TYPE_CHECKING``.
49+
50+
Closes #3979
51+
4752

4853
What's New in Pylint 2.13.7?
4954
============================

doc/whatsnew/2.13.rst

+5
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,11 @@ Other Changes
630630

631631
Closes #6419
632632

633+
* Fix false positive for ``unsubscriptable-object`` in Python 3.8 and below for
634+
statements guarded by ``if TYPE_CHECKING``.
635+
636+
Closes #3979
637+
633638
* Fix a crash when linting a file that passes an integer ``mode=`` to
634639
``open``
635640

pylint/checkers/typecheck.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1915,7 +1915,11 @@ def visit_subscript(self, node: nodes.Subscript) -> None:
19151915
return # It would be better to handle function
19161916
# decorators, but let's start slow.
19171917

1918-
if supported_protocol and not supported_protocol(inferred, node):
1918+
if (
1919+
supported_protocol
1920+
and not supported_protocol(inferred, node)
1921+
and not utils.in_type_checking_block(node)
1922+
):
19191923
self.add_message(msg, args=node.value.as_string(), node=node.value)
19201924

19211925
@check_messages("dict-items-missing-iter")
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)