Skip to content

Commit

Permalink
[Pylint] bug fix typing (#8104)
Browse files Browse the repository at this point in the history
* recognize else

* check string of the if

* update

* pass

* Update tools/pylint-extensions/azure-pylint-guidelines-checker/CHANGELOG.md
  • Loading branch information
l0lawrence authored Apr 17, 2024
1 parent e7fb87c commit 93fbd66
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release History

## 0.4.1 (2024-04-17)

- Bug fix for typing under TYPE_CHECKING block.

## 0.4.0 (2024-04-15)

- Checker to enforce no importing typing under TYPE_CHECKING block.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2725,23 +2725,30 @@ class NoImportTypingFromTypeCheck(BaseChecker):

def visit_importfrom(self, node):
"""Check that we aren't importing from typing under if TYPE_CHECKING."""
if isinstance(node.parent, astroid.If) and (node.modname == "typing" or node.modname == "typing_extensions"):
self.add_message(
msgid=f"no-typing-import-in-type-check",
node=node,
confidence=None,
)

def visit_import(self, node):
"""Check that we aren't importing from typing under if TYPE_CHECKING."""
if isinstance(node.parent, astroid.If):
for name, _ in node.names:
if name == "typing" or name == "typing_extensions":
try:
if isinstance(node.parent, astroid.If) and "TYPE_CHECKING" in node.parent.as_string():
if node.modname == "typing" or node.modname == "typing_extensions":
self.add_message(
msgid=f"no-typing-import-in-type-check",
node=node,
confidence=None,
)
except:
pass

def visit_import(self, node):
"""Check that we aren't importing from typing under if TYPE_CHECKING."""
try:
if isinstance(node.parent, astroid.If) and "TYPE_CHECKING" in node.parent.as_string():
for name, _ in node.names:
if name == "typing" or name == "typing_extensions":
self.add_message(
msgid=f"no-typing-import-in-type-check",
node=node,
confidence=None,
)
except:
pass

# if a linter is registered in this function then it will be checked with pylint
def register(linter):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name="azure-pylint-guidelines-checker",
version="0.4.0",
version="0.4.1",
url="http://github.com/Azure/azure-sdk-for-python",
license="MIT License",
description="A pylint plugin which enforces azure sdk guidelines.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4811,3 +4811,22 @@ def test_allowed_imports(self):
)
with self.assertNoMessages():
self.checker.visit_importfrom(importfrom_node)

def test_allowed_import_else(self):
"""Check that illegal imports raise warnings"""
ima, imb, imc, imd = astroid.extract_node(
"""
if sys.version_info >= (3, 9):
from collections.abc import MutableMapping
else:
from typing import MutableMapping #@
import typing #@
import typing_extensions #@
from typing_extensions import Protocol #@
"""
)
with self.assertNoMessages():
self.checker.visit_importfrom(ima)
self.checker.visit_import(imb)
self.checker.visit_import(imc)
self.checker.visit_importfrom(imd)

0 comments on commit 93fbd66

Please sign in to comment.