Skip to content

Commit 69ff96c

Browse files
authored
[Pylint] Disallow using legacy type hinting (#8801)
* [pylint] forbid legacy typing * add rule to Rules List & fix table formatting * add do-not-use-legacy-typing to Rules List * switch to using FunctionDef * remove redundant condition
1 parent a434ce6 commit 69ff96c

3 files changed

Lines changed: 77 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,4 @@ In the case of a false positive, use the disable command to remove the pylint er
9292
| docstring-keyword-should-match-keyword-only | Docstring keyword arguments and keyword-only method arguments should match. | pylint:disable=docstring-keyword-should-match-keyword-only | [link](https://azure.github.io/azure-sdk/python_documentation.html#docstrings) |
9393
| docstring-type-do-not-use-class | Docstring type is formatted incorrectly. Do not use `:class` in docstring type. | pylint:disable=docstring-type-do-not-use-class | [link](https://sphinx-rtd-tutorial.readthedocs.io/en/latest/docstrings.html) |
9494
| no-typing-import-in-type-check | Do not import typing under TYPE_CHECKING. | pylint:disable=no-typing-import-in-type-check | No Link. |
95+
| do-not-use-legacy-typing | Do not use legacy (<Python 3.8) type hinting comments | pylint:disable=do-not-use-legacy-typing | No Link.

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2750,6 +2750,29 @@ def visit_import(self, node):
27502750
except:
27512751
pass
27522752

2753+
class DoNotUseLegacyTyping(BaseChecker):
2754+
2755+
""" Rule to check that we aren't using legacy typing using comments. """
2756+
2757+
name = "do-not-use-legacy-typing"
2758+
priority = -1
2759+
msgs = {
2760+
"C4761": (
2761+
"Do not use legacy typing using comments.",
2762+
"do-not-use-legacy-typing",
2763+
"Do not use legacy typing using comments. Python 2 is no longer supported, use Python 3.9+ type hints instead.",
2764+
),
2765+
}
2766+
2767+
def visit_functiondef(self, node):
2768+
"""Check that we aren't using legacy typing."""
2769+
if node.type_comment_args or node.type_comment_returns:
2770+
self.add_message(
2771+
msgid=f"do-not-use-legacy-typing",
2772+
node=node,
2773+
confidence=None,
2774+
)
2775+
27532776
# if a linter is registered in this function then it will be checked with pylint
27542777
def register(linter):
27552778
linter.register_checker(ClientsDoNotUseStaticMethods(linter))
@@ -2782,6 +2805,8 @@ def register(linter):
27822805
linter.register_checker(DoNotImportLegacySix(linter))
27832806
linter.register_checker(NoLegacyAzureCoreHttpResponseImport(linter))
27842807
linter.register_checker(NoImportTypingFromTypeCheck(linter))
2808+
linter.register_checker(DoNotUseLegacyTyping(linter))
2809+
27852810

27862811
# disabled by default, use pylint --enable=check-docstrings if you want to use it
27872812
linter.register_checker(CheckDocstringParameters(linter))

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

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4829,4 +4829,54 @@ def test_allowed_import_else(self):
48294829
self.checker.visit_importfrom(ima)
48304830
self.checker.visit_import(imb)
48314831
self.checker.visit_import(imc)
4832-
self.checker.visit_importfrom(imd)
4832+
self.checker.visit_importfrom(imd)
4833+
4834+
class TestCheckDoNotUseLegacyTyping(pylint.testutils.CheckerTestCase):
4835+
"""Test that we are blocking disallowed legacy typing practices"""
4836+
4837+
CHECKER_CLASS = checker.DoNotUseLegacyTyping
4838+
4839+
def test_disallowed_typing(self):
4840+
"""Check that illegal method typing comments raise warnings"""
4841+
fdef = astroid.extract_node(
4842+
"""
4843+
def function(x): #@
4844+
# type: (str) -> str
4845+
pass
4846+
"""
4847+
)
4848+
4849+
with self.assertAddsMessages(
4850+
pylint.testutils.MessageTest(
4851+
msg_id="do-not-use-legacy-typing",
4852+
line=2,
4853+
node=fdef,
4854+
col_offset=0,
4855+
end_line=2,
4856+
end_col_offset=12,
4857+
)
4858+
):
4859+
self.checker.visit_functiondef(fdef)
4860+
4861+
def test_allowed_typing(self):
4862+
"""Check that allowed method typing comments don't raise warnings"""
4863+
fdef = astroid.extract_node(
4864+
"""
4865+
def function(x: str) -> str: #@
4866+
pass
4867+
"""
4868+
)
4869+
with self.assertNoMessages():
4870+
self.checker.visit_functiondef(fdef)
4871+
4872+
def test_arbitrary_comments(self):
4873+
"""Check that arbitrary comments don't raise warnings"""
4874+
fdef = astroid.extract_node(
4875+
"""
4876+
def function(x): #@
4877+
# This is a comment
4878+
pass
4879+
"""
4880+
)
4881+
with self.assertNoMessages():
4882+
self.checker.visit_functiondef(fdef)

0 commit comments

Comments
 (0)