From 87d4002027a09aa566a3983b8c424f413a4f1a34 Mon Sep 17 00:00:00 2001 From: Joshua Bishop <13187637+MJoshuaB@users.noreply.github.com> Date: Thu, 1 Aug 2024 14:21:44 +1200 Subject: [PATCH 1/5] [pylint] forbid legacy typing --- .../pylint_guidelines_checker.py | 24 +++++++++++++++++++ .../tests/test_pylint_custom_plugins.py | 24 ++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/tools/pylint-extensions/azure-pylint-guidelines-checker/pylint_guidelines_checker.py b/tools/pylint-extensions/azure-pylint-guidelines-checker/pylint_guidelines_checker.py index f79af8f887a..3dadcde93a1 100644 --- a/tools/pylint-extensions/azure-pylint-guidelines-checker/pylint_guidelines_checker.py +++ b/tools/pylint-extensions/azure-pylint-guidelines-checker/pylint_guidelines_checker.py @@ -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): + """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)) @@ -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)) diff --git a/tools/pylint-extensions/azure-pylint-guidelines-checker/tests/test_pylint_custom_plugins.py b/tools/pylint-extensions/azure-pylint-guidelines-checker/tests/test_pylint_custom_plugins.py index dfdca1775bc..37ad77d5bba 100644 --- a/tools/pylint-extensions/azure-pylint-guidelines-checker/tests/test_pylint_custom_plugins.py +++ b/tools/pylint-extensions/azure-pylint-guidelines-checker/tests/test_pylint_custom_plugins.py @@ -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) \ No newline at end of file + 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( + pylint.testutils.MessageTest( + msg_id="do-not-use-legacy-typing", + node=fnt, + ) + ): + self.checker.visit_functiontype(fnt) \ No newline at end of file From f3d814b08c2997c99d9b90ca1bfb762fe7a5440c Mon Sep 17 00:00:00 2001 From: Joshua Bishop <13187637+MJoshuaB@users.noreply.github.com> Date: Fri, 2 Aug 2024 09:37:37 +1200 Subject: [PATCH 2/5] add rule to Rules List & fix table formatting --- .../azure-pylint-guidelines-checker/README.md | 81 ++++++++++--------- 1 file changed, 41 insertions(+), 40 deletions(-) diff --git a/tools/pylint-extensions/azure-pylint-guidelines-checker/README.md b/tools/pylint-extensions/azure-pylint-guidelines-checker/README.md index e2d5042c9ab..e6668cefb65 100644 --- a/tools/pylint-extensions/azure-pylint-guidelines-checker/README.md +++ b/tools/pylint-extensions/azure-pylint-guidelines-checker/README.md @@ -52,43 +52,44 @@ In the case of a false positive, use the disable command to remove the pylint er ## Rules List -| Pylint checker name | How to fix this | How to disable this rule | Link to python guideline | -|----------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------| -| client-method-should-not-use-static-method | Use module level functions instead. | # pylint:disable=client-method-should-not-use-static-method | [link](https://azure.github.io/azure-sdk/python_implementation.html#method-signatures) | -| missing-client-constructor-parameter-credential | Add a credential parameter to the client constructor. Do not use plural form "credentials". | # pylint:disable=missing-client-constructor-parameter-credential | [link](https://azure.github.io/azure-sdk/python_design.html#client-configuration) | -| missing-client-constructor-parameter-kwargs | Add a **kwargs parameter to the client constructor. | # pylint:disable=missing-client-constructor-parameter-kwargs | [link](https://azure.github.io/azure-sdk/python_design.html#client-configuration) | -| client-method-has-more-than-5-positional-arguments | Use keyword arguments to reduce number of positional arguments. | # pylint:disable=client-method-has-more-than-5-positional-arguments | [link](https://azure.github.io/azure-sdk/python_implementation.html#method-signatures) | -| client-method-missing-type-annotations | Check that param/return type comments are present or that param/return type annotations are present. Check that you did not mix type comments with type annotations. | # pylint:disable=client-method-missing-type-annotations | [link](https://azure.github.io/azure-sdk/python_implementation.html#types-or-not) | -| client-incorrect-naming-convention | Check that you use... snake_case for variable, function, and method names. Pascal case for types. ALL CAPS for constants. | # pylint:disable=client-incorrect-naming-convention | [link](https://azure.github.io/azure-sdk/python_implementation.html#naming-conventions) | -| client-method-missing-kwargs | Check that any methods that make network calls have a **kwargs parameter. | # pylint:disable=client-method-missing-kwargs | [link](https://azure.github.io/azure-sdk/python_design.html#constructors-and-factory-methods) | -| config-missing-kwargs-in-policy | Check that the policies in your configuration function contain a **kwargs parameter. | # pylint:disable=config-missing-kwargs-in-policy | [link](https://azure.github.io/azure-sdk/python_design.html#client-configuration) | -| async-client-bad-name | Remove "Async" from your service client's name. | # pylint:disable=async-client-bad-name | [link](https://azure.github.io/azure-sdk/python_design.html#async-support) | -| file-needs-copyright-header | Add a copyright header to the top of your file. | # pylint:disable=file-needs-copyright-header | [link](https://azure.github.io/azure-sdk/policies_opensource.html#) | -| client-method-name-no-double-underscore | Don't use method names prefixed with "__". | # pylint:disable=client-method-name-no-double-underscore | [link](https://azure.github.io/azure-sdk/python_implementation.html#public-vs-private) | -| specify-parameter-names-in-call | Specify the parameter names when calling methods with more than 2 required positional parameters. e.g. self.get_foo(one, two, three=three, four=four, five=five) | # pylint:disable=specify-parameter-names-in-call | [link](https://azure.github.io/azure-sdk/python_implementation.html#python-codestyle-positional-params) | -| connection-string-should-not-be-constructor-param | Remove connection string parameter from client constructor. Create a method that creates the client using a connection string. | # pylint:disable=connection-string-should-not-be-constructor-param | [link](https://azure.github.io/azure-sdk/python_design.html#python-client-connection-string) | -| package-name-incorrect | Change your distribution package name to only include dashes, e.g. azure-storage-file-share | # pylint:disable=package-name-incorrect | [link](https://azure.github.io/azure-sdk/python_design.html#packaging) | -| client-suffix-needed | Service client types should use a "Client" suffix, e.g. BlobClient. | # pylint:disable=client-suffix-needed | [link](https://azure.github.io/azure-sdk/python_design.html#service-client) | -| docstring-admonition-needs-newline | Add a blank newline above the .. literalinclude statement. | # pylint:disable=docstring-admonition-needs-newline | No guideline, just helps our docs get built correctly for microsoft docs. | -| naming-mismatch | Do not alias models imported from the generated code. | # pylint:disable=naming-mismatch | [link](https://github.com/Azure/autorest/blob/main/docs/generate/built-in-directives.md) | -| client-accepts-api-version-keyword | Ensure that the client constructor accepts a keyword-only api_version argument. | # pylint:disable=client-accepts-api-version-keyword | [link](https://azure.github.io/azure-sdk/python_design.html#specifying-the-service-version) | -| enum-must-be-uppercase | The enum name must be all uppercase. | # pylint:disable=enum-must-be-uppercase | [link](https://azure.github.io/azure-sdk/python_design.html#enumerations) | -| enum-must-inherit-case-insensitive-enum-meta | The enum should inherit from CaseInsensitiveEnumMeta. | # pylint:disable=enum-must-inherit-case-insensitive-enum-meta | [link](https://azure.github.io/azure-sdk/python_implementation.html#extensible-enumerations) | -| networking-import-outside-azure-core-transport | This import is only allowed in azure.core.pipeline.transport. | # pylint:disable=networking-import-outside-azure-core-transport | [link](https://github.com/Azure/azure-sdk-for-python/issues/24989) | -| non-abstract-transport-import | Only import abstract transports. Let core or end-user decide which transport to use. | # pylint:disable=non-abstract-transport-import | [link](https://github.com/Azure/azure-sdk-for-python/issues/25533) | -| no-raise-with-traceback | Check that raise_with_traceback from azure-core is replaced with python 3 'raise from' syntax. | # pylint:disable=no-raise-with-traceback | [link](https://github.com/Azure/azure-sdk-for-python/issues/26759) -| name-too-long | Check that the length of class names, function names, and variable names are under 40 characters. | # pylint:disable=name-too-long |[link](https://github.com/Azure/azure-sdk-for-python/issues/26640) -| delete-operation-wrong-return-type | Check that delete* or begin_delete* methods return None or LROPoller[None]. | # pylint:disable=delete-operation-wrong-return-type | [link](https://github.com/Azure/azure-sdk-for-python/issues/26662) -client-method-missing-tracing-decorator |pylint:disable=client-method-missing-tracing-decorator | Check that sync client methods that make network calls have the sync distributed tracing decorator. | [link](https://guidelinescollab.github.io/azure-sdk/python_implementation.html#distributed-tracing) | -client-method-missing-tracing-decorator-async | pylint:disable=client-method-missing-tracing-decorator-async | Check that async client methods that make network calls have the async distributed tracing decorator. | [link](https://guidelinescollab.github.io/azure-sdk/python_implementation.html#distributed-tracing) | -client-list-methods-use-paging | pylint:disable=client-list-methods-use-paging | Client methods that return collections should use the Paging protocol. | [link](https://azure.github.io/azure-sdk/python_design.html#response-formats) | -docstring-missing-param | pylint:disable=docstring-missing-param | Docstring missing for param. | [link](https://azure.github.io/azure-sdk/python_documentation.html#docstrings) | -docstring-missing-type | pylint:disable=docstring-missing-type | Docstring missing for param type. | [link](https://azure.github.io/azure-sdk/python_documentation.html#docstrings) | -docstring-missing-return | pylint:disable=docstring-missing-return | Docstring missing return. | [link](https://azure.github.io/azure-sdk/python_documentation.html#docstrings) | -docstring-missing-rtype | pylint:disable=docstring-missing-rtype | Docstring missing return type. | [link](https://azure.github.io/azure-sdk/python_documentation.html#docstrings) | -docstring-should-be-keyword | pylint:disable=docstring-should-be-keyword | Docstring should use keywords. | [link](https://azure.github.io/azure-sdk/python_documentation.html#docstrings) | -do-not-import-legacy-six | pylint:disable=do-not-import-legacy-six | Do not import six. | No Link. | -no-legacy-azure-core-http-response-import | pylint:disable=no-legacy-azure-core-http-response-import | Do not import HttpResponse from azure.core.pipeline.transport outside of Azure Core. You can import HttpResponse from azure.core.rest instead. | [link](https://github.com/Azure/azure-sdk-for-python/issues/30785) | -docstring-keyword-should-match-keyword-only | pylint:disable=docstring-keyword-should-match-keyword-only | Docstring keyword arguments and keyword-only method arguments should match. | [link](https://azure.github.io/azure-sdk/python_documentation.html#docstrings) | -docstring-type-do-not-use-class | pylint:disable=docstring-type-do-not-use-class | Docstring type is formatted incorrectly. Do not use `:class` in docstring type. | [link](https://sphinx-rtd-tutorial.readthedocs.io/en/latest/docstrings.html) | -no-typing-import-in-type-check | pylint:disable=no-typing-import-in-type-check | Do not import typing under TYPE_CHECKING. | No Link. | \ No newline at end of file +| Pylint checker name | How to fix this | How to disable this rule | Link to python guideline | +|----------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------| +| client-method-should-not-use-static-method | Use module level functions instead. | # pylint:disable=client-method-should-not-use-static-method | [link](https://azure.github.io/azure-sdk/python_implementation.html#method-signatures) | +| missing-client-constructor-parameter-credential | Add a credential parameter to the client constructor. Do not use plural form "credentials". | # pylint:disable=missing-client-constructor-parameter-credential | [link](https://azure.github.io/azure-sdk/python_design.html#client-configuration) | +| missing-client-constructor-parameter-kwargs | Add a **kwargs parameter to the client constructor. | # pylint:disable=missing-client-constructor-parameter-kwargs | [link](https://azure.github.io/azure-sdk/python_design.html#client-configuration) | +| client-method-has-more-than-5-positional-arguments | Use keyword arguments to reduce number of positional arguments. | # pylint:disable=client-method-has-more-than-5-positional-arguments | [link](https://azure.github.io/azure-sdk/python_implementation.html#method-signatures) | +| client-method-missing-type-annotations | Check that param/return type comments are present or that param/return type annotations are present. Check that you did not mix type comments with type annotations. | # pylint:disable=client-method-missing-type-annotations | [link](https://azure.github.io/azure-sdk/python_implementation.html#types-or-not) | +| client-incorrect-naming-convention | Check that you use... snake_case for variable, function, and method names. Pascal case for types. ALL CAPS for constants. | # pylint:disable=client-incorrect-naming-convention | [link](https://azure.github.io/azure-sdk/python_implementation.html#naming-conventions) | +| client-method-missing-kwargs | Check that any methods that make network calls have a **kwargs parameter. | # pylint:disable=client-method-missing-kwargs | [link](https://azure.github.io/azure-sdk/python_design.html#constructors-and-factory-methods) | +| config-missing-kwargs-in-policy | Check that the policies in your configuration function contain a **kwargs parameter. | # pylint:disable=config-missing-kwargs-in-policy | [link](https://azure.github.io/azure-sdk/python_design.html#client-configuration) | +| async-client-bad-name | Remove "Async" from your service client's name. | # pylint:disable=async-client-bad-name | [link](https://azure.github.io/azure-sdk/python_design.html#async-support) | +| file-needs-copyright-header | Add a copyright header to the top of your file. | # pylint:disable=file-needs-copyright-header | [link](https://azure.github.io/azure-sdk/policies_opensource.html#) | +| client-method-name-no-double-underscore | Don't use method names prefixed with "__". | # pylint:disable=client-method-name-no-double-underscore | [link](https://azure.github.io/azure-sdk/python_implementation.html#public-vs-private) | +| specify-parameter-names-in-call | Specify the parameter names when calling methods with more than 2 required positional parameters. e.g. self.get_foo(one, two, three=three, four=four, five=five) | # pylint:disable=specify-parameter-names-in-call | [link](https://azure.github.io/azure-sdk/python_implementation.html#python-codestyle-positional-params) | +| connection-string-should-not-be-constructor-param | Remove connection string parameter from client constructor. Create a method that creates the client using a connection string. | # pylint:disable=connection-string-should-not-be-constructor-param | [link](https://azure.github.io/azure-sdk/python_design.html#python-client-connection-string) | +| package-name-incorrect | Change your distribution package name to only include dashes, e.g. azure-storage-file-share | # pylint:disable=package-name-incorrect | [link](https://azure.github.io/azure-sdk/python_design.html#packaging) | +| client-suffix-needed | Service client types should use a "Client" suffix, e.g. BlobClient. | # pylint:disable=client-suffix-needed | [link](https://azure.github.io/azure-sdk/python_design.html#service-client) | +| docstring-admonition-needs-newline | Add a blank newline above the .. literalinclude statement. | # pylint:disable=docstring-admonition-needs-newline | No guideline, just helps our docs get built correctly for microsoft docs. | +| naming-mismatch | Do not alias models imported from the generated code. | # pylint:disable=naming-mismatch | [link](https://github.com/Azure/autorest/blob/main/docs/generate/built-in-directives.md) | +| client-accepts-api-version-keyword | Ensure that the client constructor accepts a keyword-only api_version argument. | # pylint:disable=client-accepts-api-version-keyword | [link](https://azure.github.io/azure-sdk/python_design.html#specifying-the-service-version) | +| enum-must-be-uppercase | The enum name must be all uppercase. | # pylint:disable=enum-must-be-uppercase | [link](https://azure.github.io/azure-sdk/python_design.html#enumerations) | +| enum-must-inherit-case-insensitive-enum-meta | The enum should inherit from CaseInsensitiveEnumMeta. | # pylint:disable=enum-must-inherit-case-insensitive-enum-meta | [link](https://azure.github.io/azure-sdk/python_implementation.html#extensible-enumerations) | +| networking-import-outside-azure-core-transport | This import is only allowed in azure.core.pipeline.transport. | # pylint:disable=networking-import-outside-azure-core-transport | [link](https://github.com/Azure/azure-sdk-for-python/issues/24989) | +| non-abstract-transport-import | Only import abstract transports. Let core or end-user decide which transport to use. | # pylint:disable=non-abstract-transport-import | [link](https://github.com/Azure/azure-sdk-for-python/issues/25533) | +| no-raise-with-traceback | Check that raise_with_traceback from azure-core is replaced with python 3 'raise from' syntax. | # pylint:disable=no-raise-with-traceback | [link](https://github.com/Azure/azure-sdk-for-python/issues/26759) | +| name-too-long | Check that the length of class names, function names, and variable names are under 40 characters. | # pylint:disable=name-too-long | [link](https://github.com/Azure/azure-sdk-for-python/issues/26640) | +| delete-operation-wrong-return-type | Check that delete* or begin_delete* methods return None or LROPoller[None]. | # pylint:disable=delete-operation-wrong-return-type | [link](https://github.com/Azure/azure-sdk-for-python/issues/26662) | +| client-method-missing-tracing-decorator | Check that sync client methods that make network calls have the sync distributed tracing decorator. | # pylint:disable=client-method-missing-tracing-decorator | [link](https://guidelinescollab.github.io/azure-sdk/python_implementation.html#distributed-tracing) | +| client-method-missing-tracing-decorator-async | Check that async client methods that make network calls have the async distributed tracing decorator. | # pylint:disable=client-method-missing-tracing-decorator-async | [link](https://guidelinescollab.github.io/azure-sdk/python_implementation.html#distributed-tracing) | +| client-list-methods-use-paging | Client methods that return collections should use the Paging protocol. | # pylint:disable=client-list-methods-use-paging | [link](https://azure.github.io/azure-sdk/python_design.html#response-formats) | +| docstring-missing-param | Docstring missing for param. | # pylint:disable=docstring-missing-param | [link](https://azure.github.io/azure-sdk/python_documentation.html#docstrings) | +| docstring-missing-type | Docstring missing for param type. | # pylint:disable=docstring-missing-type | [link](https://azure.github.io/azure-sdk/python_documentation.html#docstrings) | +| docstring-missing-return | Docstring missing return. | # pylint:disable=docstring-missing-return | [link](https://azure.github.io/azure-sdk/python_documentation.html#docstrings) | +| docstring-missing-rtype | Docstring missing return type. | # pylint:disable=docstring-missing-rtype | [link](https://azure.github.io/azure-sdk/python_documentation.html#docstrings) | +| docstring-should-be-keyword | Docstring should use keywords. | # pylint:disable=docstring-should-be-keyword | [link](https://azure.github.io/azure-sdk/python_documentation.html#docstrings) | +| do-not-import-legacy-six | Do not import six. | # pylint:disable=do-not-import-legacy-six | No Link. | +| no-legacy-azure-core-http-response-import | Do not import HttpResponse from azure.core.pipeline.transport outside of Azure Core. You can import HttpResponse from azure.core.rest instead. | # pylint:disable=no-legacy-azure-core-http-response-import | [link](https://github.com/Azure/azure-sdk-for-python/issues/30785) | +| 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 (<Python3.8) type hinting comments | # pylint:disable=do-not-use-legacy-typing | [link](https://azure.github.io/azure-sdk/python_design.html#supported-python-versions) | \ No newline at end of file From 8bec3fb00b4a0ceae0e4ec430daec8b0905c4f0b Mon Sep 17 00:00:00 2001 From: Joshua Bishop <13187637+MJoshuaB@users.noreply.github.com> Date: Thu, 8 Aug 2024 11:07:36 +1200 Subject: [PATCH 3/5] add do-not-use-legacy-typing to Rules List --- .../pylint-extensions/azure-pylint-guidelines-checker/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/pylint-extensions/azure-pylint-guidelines-checker/README.md b/tools/pylint-extensions/azure-pylint-guidelines-checker/README.md index 7d8449008b9..06880bef060 100644 --- a/tools/pylint-extensions/azure-pylint-guidelines-checker/README.md +++ b/tools/pylint-extensions/azure-pylint-guidelines-checker/README.md @@ -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. \ No newline at end of file From a371656ccd2bcc06a13104c95b828f7dfcc643d0 Mon Sep 17 00:00:00 2001 From: Joshua Bishop <13187637+MJoshuaB@users.noreply.github.com> Date: Wed, 14 Aug 2024 10:15:38 +1200 Subject: [PATCH 4/5] switch to using FunctionDef --- .../pylint_guidelines_checker.py | 13 ++++--- .../tests/test_pylint_custom_plugins.py | 38 ++++++++++++++++--- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/tools/pylint-extensions/azure-pylint-guidelines-checker/pylint_guidelines_checker.py b/tools/pylint-extensions/azure-pylint-guidelines-checker/pylint_guidelines_checker.py index 3dadcde93a1..4b71f3c1e2f 100644 --- a/tools/pylint-extensions/azure-pylint-guidelines-checker/pylint_guidelines_checker.py +++ b/tools/pylint-extensions/azure-pylint-guidelines-checker/pylint_guidelines_checker.py @@ -2764,13 +2764,14 @@ class DoNotUseLegacyTyping(BaseChecker): ), } - def visit_functiontype(self, node): + def visit_functiondef(self, node): """Check that we aren't using legacy typing.""" - self.add_message( - msgid=f"do-not-use-legacy-typing", - node=node, - confidence=None - ) + 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): diff --git a/tools/pylint-extensions/azure-pylint-guidelines-checker/tests/test_pylint_custom_plugins.py b/tools/pylint-extensions/azure-pylint-guidelines-checker/tests/test_pylint_custom_plugins.py index 37ad77d5bba..9627cbad1b1 100644 --- a/tools/pylint-extensions/azure-pylint-guidelines-checker/tests/test_pylint_custom_plugins.py +++ b/tools/pylint-extensions/azure-pylint-guidelines-checker/tests/test_pylint_custom_plugins.py @@ -4838,17 +4838,45 @@ class TestCheckDoNotUseLegacyTyping(pylint.testutils.CheckerTestCase): def test_disallowed_typing(self): """Check that illegal method typing comments raise warnings""" - fnt = astroid.extract_node( + fdef = astroid.extract_node( """ - def function(x): - # type: (str) -> str #@ + def function(x): #@ + # type: (str) -> str pass """ ) + with self.assertAddsMessages( pylint.testutils.MessageTest( msg_id="do-not-use-legacy-typing", - node=fnt, + line=2, + node=fdef, + col_offset=0, + end_line=2, + end_col_offset=12, ) ): - self.checker.visit_functiontype(fnt) \ No newline at end of file + 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) \ No newline at end of file From 09cc40e187613f0714623c2952b81a8199706771 Mon Sep 17 00:00:00 2001 From: Joshua Bishop <13187637+MJoshuaB@users.noreply.github.com> Date: Sat, 17 Aug 2024 10:21:47 +1200 Subject: [PATCH 5/5] remove redundant condition --- .../pylint_guidelines_checker.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/pylint-extensions/azure-pylint-guidelines-checker/pylint_guidelines_checker.py b/tools/pylint-extensions/azure-pylint-guidelines-checker/pylint_guidelines_checker.py index 4b71f3c1e2f..dfcb7e6dc07 100644 --- a/tools/pylint-extensions/azure-pylint-guidelines-checker/pylint_guidelines_checker.py +++ b/tools/pylint-extensions/azure-pylint-guidelines-checker/pylint_guidelines_checker.py @@ -2766,7 +2766,7 @@ class DoNotUseLegacyTyping(BaseChecker): 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): + if node.type_comment_args or node.type_comment_returns: self.add_message( msgid=f"do-not-use-legacy-typing", node=node,