Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Pylint] Disallow using legacy type hinting #8801

Merged
merged 6 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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,28 @@ 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_functiontype(self, node):
l0lawrence marked this conversation as resolved.
Show resolved Hide resolved
"""Check that we aren't using legacy typing."""
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 +2804,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,26 @@ 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"""
fnt = astroid.extract_node(
"""
def function(x):
# type: (str) -> str #@
pass
"""
)
with self.assertAddsMessages(
annatisch marked this conversation as resolved.
Show resolved Hide resolved
pylint.testutils.MessageTest(
msg_id="do-not-use-legacy-typing",
node=fnt,
)
):
self.checker.visit_functiontype(fnt)