From 55fc8b1db6c81cb4c17fba910ac5a6135a0e14a3 Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Tue, 25 Jan 2022 19:56:13 +0100 Subject: [PATCH 1/3] Add a regression test for issue #3979 --- tests/functional/r/regression_02/regression_3979.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 tests/functional/r/regression_02/regression_3979.py diff --git a/tests/functional/r/regression_02/regression_3979.py b/tests/functional/r/regression_02/regression_3979.py new file mode 100644 index 0000000000..b48661bc3f --- /dev/null +++ b/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] # <-- that is where pylint identifies E1136 +else: + BasePathLike = os.PathLike + +foo : Union[str, BasePathLike] = "bar" From abc50da4274af97edbaa5ee45fbb3553c3101e78 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Sat, 9 Apr 2022 12:17:25 -0400 Subject: [PATCH 2/3] Don't emit `unsubscriptable-object` for statements in type-checking blocks --- ChangeLog | 5 +++++ doc/whatsnew/2.14.rst | 5 +++++ pylint/checkers/typecheck.py | 6 +++++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 52007414b8..b8c1f647c2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -59,6 +59,11 @@ Release date: TBA Closes #4525 +* Fix false positive for ``unsubscriptable-object`` in Python 3.8 and below for + statements guarded by an ``if TYPE_CHECKING`` guard. + + Closes #3979 + * Fix false negative for ``no-member`` when attempting to assign an instance attribute to itself without any prior assignment. diff --git a/doc/whatsnew/2.14.rst b/doc/whatsnew/2.14.rst index dfe548f4bd..811f1935c8 100644 --- a/doc/whatsnew/2.14.rst +++ b/doc/whatsnew/2.14.rst @@ -77,6 +77,11 @@ Other Changes Closes #5205 +* Fix false positive for ``unsubscriptable-object`` in Python 3.8 and below for + statements guarded by an ``if TYPE_CHECKING`` guard. + + Closes #3979 + * Fix false negative for ``no-member`` when attempting to assign an instance attribute to itself without any prior assignment. diff --git a/pylint/checkers/typecheck.py b/pylint/checkers/typecheck.py index 2ab5ced330..5b3c6a372e 100644 --- a/pylint/checkers/typecheck.py +++ b/pylint/checkers/typecheck.py @@ -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") From 141d8f00e17f05f5db480fa5277c87fc8f1fdfa8 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Sat, 9 Apr 2022 12:35:08 -0400 Subject: [PATCH 3/3] cosmetic --- ChangeLog | 2 +- doc/whatsnew/2.14.rst | 2 +- tests/functional/r/regression_02/regression_3979.py | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index b8c1f647c2..bef0c44164 100644 --- a/ChangeLog +++ b/ChangeLog @@ -60,7 +60,7 @@ Release date: TBA Closes #4525 * Fix false positive for ``unsubscriptable-object`` in Python 3.8 and below for - statements guarded by an ``if TYPE_CHECKING`` guard. + statements guarded by ``if TYPE_CHECKING``. Closes #3979 diff --git a/doc/whatsnew/2.14.rst b/doc/whatsnew/2.14.rst index 811f1935c8..fd9a156982 100644 --- a/doc/whatsnew/2.14.rst +++ b/doc/whatsnew/2.14.rst @@ -78,7 +78,7 @@ Other Changes Closes #5205 * Fix false positive for ``unsubscriptable-object`` in Python 3.8 and below for - statements guarded by an ``if TYPE_CHECKING`` guard. + statements guarded by ``if TYPE_CHECKING``. Closes #3979 diff --git a/tests/functional/r/regression_02/regression_3979.py b/tests/functional/r/regression_02/regression_3979.py index b48661bc3f..4fb0c5a051 100644 --- a/tests/functional/r/regression_02/regression_3979.py +++ b/tests/functional/r/regression_02/regression_3979.py @@ -6,8 +6,8 @@ from typing import TYPE_CHECKING, Any, Union if TYPE_CHECKING: - BasePathLike = os.PathLike[Any] # <-- that is where pylint identifies E1136 + BasePathLike = os.PathLike[Any] # <-- pylint used to emit E1136 here else: BasePathLike = os.PathLike -foo : Union[str, BasePathLike] = "bar" +foo: Union[str, BasePathLike] = "bar"