Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,4 @@ In the case of a false positive, use the disable command to remove the pylint er
| 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) |
| 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) |
| no-typing-import-in-type-check | Do not import typing under TYPE_CHECKING. | pylint:disable=no-typing-import-in-type-check | No Link. |
| do-not-use-legacy-typing | Do not use legacy (<Python 3.8) type hinting comments | pylint:disable=do-not-use-legacy-typing | No Link.
Original file line number Diff line number Diff line change
Expand Up @@ -2750,6 +2750,29 @@ def visit_import(self, node):
except:
pass

class DoNotUseLegacyTyping(BaseChecker):

""" Rule to check that we aren't using legacy typing using comments. """

name = "do-not-use-legacy-typing"
priority = -1
msgs = {
"C4761": (
"Do not use legacy typing using comments.",
"do-not-use-legacy-typing",
"Do not use legacy typing using comments. Python 2 is no longer supported, use Python 3.9+ type hints instead.",
),
}

def visit_functiondef(self, node):
"""Check that we aren't using legacy typing."""
if node.is_function and (node.type_comment_args or node.type_comment_returns):
self.add_message(
msgid=f"do-not-use-legacy-typing",
node=node,
confidence=None,
)

# if a linter is registered in this function then it will be checked with pylint
def register(linter):
linter.register_checker(ClientsDoNotUseStaticMethods(linter))
Expand Down Expand Up @@ -2782,6 +2805,8 @@ def register(linter):
linter.register_checker(DoNotImportLegacySix(linter))
linter.register_checker(NoLegacyAzureCoreHttpResponseImport(linter))
linter.register_checker(NoImportTypingFromTypeCheck(linter))
linter.register_checker(DoNotUseLegacyTyping(linter))


# disabled by default, use pylint --enable=check-docstrings if you want to use it
linter.register_checker(CheckDocstringParameters(linter))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4829,4 +4829,54 @@ def test_allowed_import_else(self):
self.checker.visit_importfrom(ima)
self.checker.visit_import(imb)
self.checker.visit_import(imc)
self.checker.visit_importfrom(imd)
self.checker.visit_importfrom(imd)

class TestCheckDoNotUseLegacyTyping(pylint.testutils.CheckerTestCase):
"""Test that we are blocking disallowed legacy typing practices"""

CHECKER_CLASS = checker.DoNotUseLegacyTyping

def test_disallowed_typing(self):
"""Check that illegal method typing comments raise warnings"""
fdef = astroid.extract_node(
"""
def function(x): #@
# type: (str) -> str
pass
"""
)

with self.assertAddsMessages(
pylint.testutils.MessageTest(
msg_id="do-not-use-legacy-typing",
line=2,
node=fdef,
col_offset=0,
end_line=2,
end_col_offset=12,
)
):
self.checker.visit_functiondef(fdef)

def test_allowed_typing(self):
"""Check that allowed method typing comments don't raise warnings"""
fdef = astroid.extract_node(
"""
def function(x: str) -> str: #@
pass
"""
)
with self.assertNoMessages():
self.checker.visit_functiondef(fdef)

def test_arbitrary_comments(self):
"""Check that arbitrary comments don't raise warnings"""
fdef = astroid.extract_node(
"""
def function(x): #@
# This is a comment
pass
"""
)
with self.assertNoMessages():
self.checker.visit_functiondef(fdef)