Skip to content

Commit 93fbd66

Browse files
authored
[Pylint] bug fix typing (#8104)
* recognize else * check string of the if * update * pass * Update tools/pylint-extensions/azure-pylint-guidelines-checker/CHANGELOG.md
1 parent e7fb87c commit 93fbd66

4 files changed

Lines changed: 43 additions & 13 deletions

File tree

tools/pylint-extensions/azure-pylint-guidelines-checker/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Release History
22

3+
## 0.4.1 (2024-04-17)
4+
5+
- Bug fix for typing under TYPE_CHECKING block.
6+
37
## 0.4.0 (2024-04-15)
48

59
- Checker to enforce no importing typing under TYPE_CHECKING block.

tools/pylint-extensions/azure-pylint-guidelines-checker/pylint_guidelines_checker.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2725,23 +2725,30 @@ class NoImportTypingFromTypeCheck(BaseChecker):
27252725

27262726
def visit_importfrom(self, node):
27272727
"""Check that we aren't importing from typing under if TYPE_CHECKING."""
2728-
if isinstance(node.parent, astroid.If) and (node.modname == "typing" or node.modname == "typing_extensions"):
2729-
self.add_message(
2730-
msgid=f"no-typing-import-in-type-check",
2731-
node=node,
2732-
confidence=None,
2733-
)
2734-
2735-
def visit_import(self, node):
2736-
"""Check that we aren't importing from typing under if TYPE_CHECKING."""
2737-
if isinstance(node.parent, astroid.If):
2738-
for name, _ in node.names:
2739-
if name == "typing" or name == "typing_extensions":
2728+
try:
2729+
if isinstance(node.parent, astroid.If) and "TYPE_CHECKING" in node.parent.as_string():
2730+
if node.modname == "typing" or node.modname == "typing_extensions":
27402731
self.add_message(
27412732
msgid=f"no-typing-import-in-type-check",
27422733
node=node,
27432734
confidence=None,
27442735
)
2736+
except:
2737+
pass
2738+
2739+
def visit_import(self, node):
2740+
"""Check that we aren't importing from typing under if TYPE_CHECKING."""
2741+
try:
2742+
if isinstance(node.parent, astroid.If) and "TYPE_CHECKING" in node.parent.as_string():
2743+
for name, _ in node.names:
2744+
if name == "typing" or name == "typing_extensions":
2745+
self.add_message(
2746+
msgid=f"no-typing-import-in-type-check",
2747+
node=node,
2748+
confidence=None,
2749+
)
2750+
except:
2751+
pass
27452752

27462753
# if a linter is registered in this function then it will be checked with pylint
27472754
def register(linter):

tools/pylint-extensions/azure-pylint-guidelines-checker/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
setup(
88
name="azure-pylint-guidelines-checker",
9-
version="0.4.0",
9+
version="0.4.1",
1010
url="http://github.com/Azure/azure-sdk-for-python",
1111
license="MIT License",
1212
description="A pylint plugin which enforces azure sdk guidelines.",

tools/pylint-extensions/azure-pylint-guidelines-checker/tests/test_pylint_custom_plugins.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4811,3 +4811,22 @@ def test_allowed_imports(self):
48114811
)
48124812
with self.assertNoMessages():
48134813
self.checker.visit_importfrom(importfrom_node)
4814+
4815+
def test_allowed_import_else(self):
4816+
"""Check that illegal imports raise warnings"""
4817+
ima, imb, imc, imd = astroid.extract_node(
4818+
"""
4819+
if sys.version_info >= (3, 9):
4820+
from collections.abc import MutableMapping
4821+
else:
4822+
from typing import MutableMapping #@
4823+
import typing #@
4824+
import typing_extensions #@
4825+
from typing_extensions import Protocol #@
4826+
"""
4827+
)
4828+
with self.assertNoMessages():
4829+
self.checker.visit_importfrom(ima)
4830+
self.checker.visit_import(imb)
4831+
self.checker.visit_import(imc)
4832+
self.checker.visit_importfrom(imd)

0 commit comments

Comments
 (0)