From 9986c04d4c4c9d068d6f76710b6bc0358bb63329 Mon Sep 17 00:00:00 2001 From: Libba Lawrence Date: Mon, 7 Mar 2022 11:17:39 -0800 Subject: [PATCH 01/23] intro patch for aliasing - needs better way to grab __all__ info --- .../pylint_guidelines_checker.py | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/scripts/pylint_custom_plugin/pylint_guidelines_checker.py b/scripts/pylint_custom_plugin/pylint_guidelines_checker.py index e62a68296254..921e31dc2a44 100644 --- a/scripts/pylint_custom_plugin/pylint_guidelines_checker.py +++ b/scripts/pylint_custom_plugin/pylint_guidelines_checker.py @@ -1705,6 +1705,58 @@ def visit_functiondef(self, node): # this line makes it work for async functions visit_asyncfunctiondef = visit_functiondef +class CheckNoAliasGeneratedCode(BaseChecker): + __implements__ = IAstroidChecker + + name = "check-alias" + priority = -1 + msgs = { + "C4745": ( + "The generated code is aliased", + "generated-code-does-not-need-alias", + "Do not alias models imported from the generated code.", + ), + } + options = ( + ( + "ignore-generated-code-does-not-need-alias", + { + "default": False, + "type": "yn", + "metavar": "", + "help": "Allow generated code to be aliased.", + }, + ), + ) + + def __init__(self, linter=None): + super(CheckNoAliasGeneratedCode, self).__init__(linter) + + def visit_module(self, node): + """Visits __init__.py and checks to see that any aliased names do not appear in __all__""" + try: + if node.file.endswith("__init__.py"): + #Node Body is the number of import statements from X import + aliased = [] + for nod in node.body: + + if isinstance(nod, astroid.ImportFrom): + # If the model has been aliased + for name in nod.names: + if name[1] != None: + aliased.append(name[1]) + + if isinstance(nod, astroid.Assign): + for i in nod.assigned_stmts(): + for j in i.elts: + if j.value in aliased: + self.add_message( + msgid="generated-code-does-not-need-alias", node=node, confidence=None + ) + except Exception: + logger.debug("Pylint custom checker failed to check if package is aliased.") + pass + # if a linter is registered in this function then it will be checked with pylint def register(linter): @@ -1723,6 +1775,7 @@ def register(linter): linter.register_checker(PackageNameDoesNotUseUnderscoreOrPeriod(linter)) linter.register_checker(ServiceClientUsesNameWithClientSuffix(linter)) linter.register_checker(CheckDocstringAdmonitionNewline(linter)) + linter.register_checker(CheckNoAliasGeneratedCode(linter)) # disabled by default, use pylint --enable=check-docstrings if you want to use it linter.register_checker(CheckDocstringParameters(linter)) From d2e9a77a228634b7365a61e13ac3fcea95f19584 Mon Sep 17 00:00:00 2001 From: Libba Lawrence Date: Mon, 7 Mar 2022 12:00:52 -0800 Subject: [PATCH 02/23] updated alias message --- .../pylint_guidelines_checker.py | 50 ++++++++++--------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/scripts/pylint_custom_plugin/pylint_guidelines_checker.py b/scripts/pylint_custom_plugin/pylint_guidelines_checker.py index 921e31dc2a44..ff145630fc0d 100644 --- a/scripts/pylint_custom_plugin/pylint_guidelines_checker.py +++ b/scripts/pylint_custom_plugin/pylint_guidelines_checker.py @@ -1705,6 +1705,7 @@ def visit_functiondef(self, node): # this line makes it work for async functions visit_asyncfunctiondef = visit_functiondef + class CheckNoAliasGeneratedCode(BaseChecker): __implements__ = IAstroidChecker @@ -1712,14 +1713,16 @@ class CheckNoAliasGeneratedCode(BaseChecker): priority = -1 msgs = { "C4745": ( - "The generated code is aliased", - "generated-code-does-not-need-alias", + "Aliasing and exposing generated code." + "This messes up sphinx, intellisense, and apiview, so please modify the name of the generated code through" + " the swagger / directives, or code customizations", + "aliasing-generated-code", "Do not alias models imported from the generated code.", ), } options = ( ( - "ignore-generated-code-does-not-need-alias", + "ignore-aliasing-generated-code", { "default": False, "type": "yn", @@ -1733,27 +1736,26 @@ def __init__(self, linter=None): super(CheckNoAliasGeneratedCode, self).__init__(linter) def visit_module(self, node): - """Visits __init__.py and checks to see that any aliased names do not appear in __all__""" - try: - if node.file.endswith("__init__.py"): - #Node Body is the number of import statements from X import - aliased = [] - for nod in node.body: - - if isinstance(nod, astroid.ImportFrom): - # If the model has been aliased - for name in nod.names: - if name[1] != None: - aliased.append(name[1]) - - if isinstance(nod, astroid.Assign): - for i in nod.assigned_stmts(): - for j in i.elts: - if j.value in aliased: - self.add_message( - msgid="generated-code-does-not-need-alias", node=node, confidence=None - ) - except Exception: + """Visits __init__.py and checks to see that any aliased names do not appear in __all__""" + try: + if node.file.endswith("__init__.py"): + aliased = [] + for nod in node.body: + + if isinstance(nod, astroid.ImportFrom): + # If the model has been aliased + for name in nod.names: + if name[1] != None: + aliased.append(name[1]) + + if isinstance(nod, astroid.Assign): + for i in nod.assigned_stmts(): + for j in i.elts: + if j.value in aliased: + self.add_message( + msgid="aliasing-generated-code", node=node, confidence=None + ) + except Exception: logger.debug("Pylint custom checker failed to check if package is aliased.") pass From a4c837f305d7e726e5ae1c35ce835c19fb198927 Mon Sep 17 00:00:00 2001 From: Libba Lawrence Date: Mon, 7 Mar 2022 12:04:21 -0800 Subject: [PATCH 03/23] line number shows up with alias warning --- scripts/pylint_custom_plugin/pylint_guidelines_checker.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/pylint_custom_plugin/pylint_guidelines_checker.py b/scripts/pylint_custom_plugin/pylint_guidelines_checker.py index ff145630fc0d..9694e3f39cae 100644 --- a/scripts/pylint_custom_plugin/pylint_guidelines_checker.py +++ b/scripts/pylint_custom_plugin/pylint_guidelines_checker.py @@ -1749,11 +1749,11 @@ def visit_module(self, node): aliased.append(name[1]) if isinstance(nod, astroid.Assign): - for i in nod.assigned_stmts(): - for j in i.elts: - if j.value in aliased: + for models in nod.assigned_stmts(): + for model_name in models.elts: + if model_name.value in aliased: self.add_message( - msgid="aliasing-generated-code", node=node, confidence=None + msgid="aliasing-generated-code", node=model_name, confidence=None ) except Exception: logger.debug("Pylint custom checker failed to check if package is aliased.") From 3b8f1ae19ae10239e859b0d8956a3b99a792ba74 Mon Sep 17 00:00:00 2001 From: Libba Lawrence Date: Mon, 7 Mar 2022 15:06:30 -0800 Subject: [PATCH 04/23] working on testing this, slight mod to isinstance --- scripts/pylint_custom_plugin/pylint_guidelines_checker.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/scripts/pylint_custom_plugin/pylint_guidelines_checker.py b/scripts/pylint_custom_plugin/pylint_guidelines_checker.py index 9694e3f39cae..5f39e520def9 100644 --- a/scripts/pylint_custom_plugin/pylint_guidelines_checker.py +++ b/scripts/pylint_custom_plugin/pylint_guidelines_checker.py @@ -1738,11 +1738,12 @@ def __init__(self, linter=None): def visit_module(self, node): """Visits __init__.py and checks to see that any aliased names do not appear in __all__""" try: + if node.file.endswith("__init__.py"): aliased = [] + for nod in node.body: - - if isinstance(nod, astroid.ImportFrom): + if isinstance(nod, astroid.ImportFrom) or isinstance(nod, astroid.Import): # If the model has been aliased for name in nod.names: if name[1] != None: @@ -1755,6 +1756,7 @@ def visit_module(self, node): self.add_message( msgid="aliasing-generated-code", node=model_name, confidence=None ) + except Exception: logger.debug("Pylint custom checker failed to check if package is aliased.") pass From b5dd0f967d1ebf2fee83b007c69f2ebc2652fb16 Mon Sep 17 00:00:00 2001 From: Libba Lawrence Date: Mon, 7 Mar 2022 15:46:47 -0800 Subject: [PATCH 05/23] tests for aliasing --- .../tests/test_pylint_custom_plugins.py | 71 ++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/scripts/pylint_custom_plugin/tests/test_pylint_custom_plugins.py b/scripts/pylint_custom_plugin/tests/test_pylint_custom_plugins.py index ef54894d996d..d2724993f22e 100644 --- a/scripts/pylint_custom_plugin/tests/test_pylint_custom_plugins.py +++ b/scripts/pylint_custom_plugin/tests/test_pylint_custom_plugins.py @@ -3,6 +3,7 @@ # Licensed under the MIT License. # ------------------------------------ +from msilib.schema import File import astroid import pylint.testutils @@ -2568,4 +2569,72 @@ def __init__(self): msg_id="docstring-admonition-needs-newline", node=class_node ) ): - self.checker.visit_classdef(class_node) \ No newline at end of file + self.checker.visit_classdef(class_node) + +class TestCheckNoAliasGeneratedCode(pylint.testutils.CheckerTestCase): + CHECKER_CLASS = checker.CheckNoAliasGeneratedCode + + def test_ignores_correct_alias_code(self): + module_node = astroid.extract_node( + """ + import something as somethingElse + """ + ) + + with self.assertNoMessages(): + self.checker.visit_module(module_node) + + def test_catches_incorrect_alias_code(self): + import_one = astroid.extract_node( + 'import Something' + + ) + import_two = astroid.extract_node( + 'import Something2 as SomethingTwo' + + ) + assign_one = astroid.extract_node( + """ + __all__ =( + "Something", + "SomethingTwo", + ) + """ + ) + + module_node = astroid.Module(name = "node", file="__init__.py", doc = """ """) + module_node.body = [import_one,import_two,assign_one] + + for name in module_node.body[-1].assigned_stmts(): + err_node = name.elts[1] + + with self.assertAddsMessages( + pylint.testutils.Message( + msg_id="aliasing-generated-code", node=err_node ,confidence=None + ) + ): + self.checker.visit_module(module_node) + + def test_ignores_import_init(self): + import_one = astroid.extract_node( + 'import Something' + + ) + import_two = astroid.extract_node( + 'import Something2 as SomethingTwo' + + ) + assign_one = astroid.extract_node( + """ + __all__ =( + "Something", + "Something2", + ) + """ + ) + + module_node = astroid.Module(name = "node", file="__init__.py", doc = """ """) + module_node.body = [import_one,import_two,assign_one] + + with self.assertNoMessages(): + self.checker.visit_module(module_node) From ae83069044d397f75d7c35867865e410df408274 Mon Sep 17 00:00:00 2001 From: Libba Lawrence Date: Mon, 7 Mar 2022 15:48:27 -0800 Subject: [PATCH 06/23] updated docstring --- scripts/pylint_custom_plugin/pylint_guidelines_checker.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/scripts/pylint_custom_plugin/pylint_guidelines_checker.py b/scripts/pylint_custom_plugin/pylint_guidelines_checker.py index 5f39e520def9..14d2780bcb9c 100644 --- a/scripts/pylint_custom_plugin/pylint_guidelines_checker.py +++ b/scripts/pylint_custom_plugin/pylint_guidelines_checker.py @@ -1736,7 +1736,12 @@ def __init__(self, linter=None): super(CheckNoAliasGeneratedCode, self).__init__(linter) def visit_module(self, node): - """Visits __init__.py and checks to see that any aliased names do not appear in __all__""" + """Visits __init__.py and checks that there are not aliased models. + + :param node: module node + :type node: ast.Module + :return: None + """ try: if node.file.endswith("__init__.py"): From 1741ebbb193a113dd24d7278e07cf4e49db21152 Mon Sep 17 00:00:00 2001 From: Libba Lawrence Date: Mon, 7 Mar 2022 15:52:26 -0800 Subject: [PATCH 07/23] removing random import --- scripts/pylint_custom_plugin/tests/test_pylint_custom_plugins.py | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/pylint_custom_plugin/tests/test_pylint_custom_plugins.py b/scripts/pylint_custom_plugin/tests/test_pylint_custom_plugins.py index d2724993f22e..364877035964 100644 --- a/scripts/pylint_custom_plugin/tests/test_pylint_custom_plugins.py +++ b/scripts/pylint_custom_plugin/tests/test_pylint_custom_plugins.py @@ -3,7 +3,6 @@ # Licensed under the MIT License. # ------------------------------------ -from msilib.schema import File import astroid import pylint.testutils From 244750e113e3f8842e35c1d9fe1129f5d74c66e2 Mon Sep 17 00:00:00 2001 From: Libba Lawrence Date: Mon, 7 Mar 2022 15:55:27 -0800 Subject: [PATCH 08/23] adding in a newline --- scripts/pylint_custom_plugin/tests/test_pylint_custom_plugins.py | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/pylint_custom_plugin/tests/test_pylint_custom_plugins.py b/scripts/pylint_custom_plugin/tests/test_pylint_custom_plugins.py index 364877035964..8d0f3bca8bdc 100644 --- a/scripts/pylint_custom_plugin/tests/test_pylint_custom_plugins.py +++ b/scripts/pylint_custom_plugin/tests/test_pylint_custom_plugins.py @@ -2569,6 +2569,7 @@ def __init__(self): ) ): self.checker.visit_classdef(class_node) + class TestCheckNoAliasGeneratedCode(pylint.testutils.CheckerTestCase): CHECKER_CLASS = checker.CheckNoAliasGeneratedCode From d2c9640f3d9562f8bfde5462b94836ff5750aff1 Mon Sep 17 00:00:00 2001 From: Libba Lawrence Date: Mon, 7 Mar 2022 15:56:46 -0800 Subject: [PATCH 09/23] adding in a newline --- .../pylint_custom_plugin/tests/test_pylint_custom_plugins.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/pylint_custom_plugin/tests/test_pylint_custom_plugins.py b/scripts/pylint_custom_plugin/tests/test_pylint_custom_plugins.py index 8d0f3bca8bdc..7fdd1e8bdb18 100644 --- a/scripts/pylint_custom_plugin/tests/test_pylint_custom_plugins.py +++ b/scripts/pylint_custom_plugin/tests/test_pylint_custom_plugins.py @@ -2568,8 +2568,8 @@ def __init__(self): msg_id="docstring-admonition-needs-newline", node=class_node ) ): - self.checker.visit_classdef(class_node) - + self.checker.visit_classdef(class_node) + class TestCheckNoAliasGeneratedCode(pylint.testutils.CheckerTestCase): CHECKER_CLASS = checker.CheckNoAliasGeneratedCode From 3113dc126e11c98fc7afa6792d270e8b247e85af Mon Sep 17 00:00:00 2001 From: Libba Lawrence Date: Tue, 8 Mar 2022 10:52:55 -0800 Subject: [PATCH 10/23] changing naming of error message --- scripts/pylint_custom_plugin/pylint_guidelines_checker.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/pylint_custom_plugin/pylint_guidelines_checker.py b/scripts/pylint_custom_plugin/pylint_guidelines_checker.py index 14d2780bcb9c..b1c0acb7c5ba 100644 --- a/scripts/pylint_custom_plugin/pylint_guidelines_checker.py +++ b/scripts/pylint_custom_plugin/pylint_guidelines_checker.py @@ -1713,7 +1713,7 @@ class CheckNoAliasGeneratedCode(BaseChecker): priority = -1 msgs = { "C4745": ( - "Aliasing and exposing generated code." + "Exposing aliased generated code." "This messes up sphinx, intellisense, and apiview, so please modify the name of the generated code through" " the swagger / directives, or code customizations", "aliasing-generated-code", From 1ccaba5af5d5bd13885967a58f744351fced7ec9 Mon Sep 17 00:00:00 2001 From: Libba Lawrence Date: Tue, 8 Mar 2022 10:55:10 -0800 Subject: [PATCH 11/23] changing package to model in except --- scripts/pylint_custom_plugin/pylint_guidelines_checker.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/pylint_custom_plugin/pylint_guidelines_checker.py b/scripts/pylint_custom_plugin/pylint_guidelines_checker.py index b1c0acb7c5ba..85c9d7e835dd 100644 --- a/scripts/pylint_custom_plugin/pylint_guidelines_checker.py +++ b/scripts/pylint_custom_plugin/pylint_guidelines_checker.py @@ -1763,7 +1763,7 @@ def visit_module(self, node): ) except Exception: - logger.debug("Pylint custom checker failed to check if package is aliased.") + logger.debug("Pylint custom checker failed to check if model is aliased.") pass From cf8c01fbc7f0a2869131b84db69e060bd89e6758 Mon Sep 17 00:00:00 2001 From: Libba Lawrence Date: Tue, 8 Mar 2022 12:09:21 -0800 Subject: [PATCH 12/23] checking for only __all__ assign Node (cat) --- .../pylint_guidelines_checker.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/scripts/pylint_custom_plugin/pylint_guidelines_checker.py b/scripts/pylint_custom_plugin/pylint_guidelines_checker.py index 85c9d7e835dd..8c9f089d6fd9 100644 --- a/scripts/pylint_custom_plugin/pylint_guidelines_checker.py +++ b/scripts/pylint_custom_plugin/pylint_guidelines_checker.py @@ -1754,13 +1754,14 @@ def visit_module(self, node): if name[1] != None: aliased.append(name[1]) - if isinstance(nod, astroid.Assign): - for models in nod.assigned_stmts(): - for model_name in models.elts: - if model_name.value in aliased: - self.add_message( - msgid="aliasing-generated-code", node=model_name, confidence=None - ) + if isinstance(nod, astroid.Assign): + if nod.targets[0].as_string() == "__all__": + for models in nod.assigned_stmts(): + for model_name in models.elts: + if model_name.value in aliased: + self.add_message( + msgid="aliasing-generated-code", node=model_name, confidence=None + ) except Exception: logger.debug("Pylint custom checker failed to check if model is aliased.") From a8088efebdccfb14e622682f1e64bcfc0e7b2168 Mon Sep 17 00:00:00 2001 From: Libba Lawrence Date: Tue, 8 Mar 2022 12:14:56 -0800 Subject: [PATCH 13/23] added alias checker to README --- scripts/pylint_custom_plugin/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/pylint_custom_plugin/README.md b/scripts/pylint_custom_plugin/README.md index 3d01aab8447e..d9ce89045a5a 100644 --- a/scripts/pylint_custom_plugin/README.md +++ b/scripts/pylint_custom_plugin/README.md @@ -58,4 +58,5 @@ In the case of a false positive, use the disable command to remove the pylint er | 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#constructors-and-factory-methods) | | 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_implementation.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#clients) | -| 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. | \ No newline at end of file +| 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. | +| aliasing-generated-code | Do not alias models imported from the generated code. | # pylint:disable=aliasing-generated-code | No guideline, just helps to not expose aliased generated code. | \ No newline at end of file From 240b7cb5a554a15beedb8db76563a87dee335193 Mon Sep 17 00:00:00 2001 From: Libba Lawrence Date: Tue, 8 Mar 2022 13:13:31 -0800 Subject: [PATCH 14/23] added test file to test disable pylint warning --- .../tests/test_files/__init__.py | 7 +++++++ .../tests/test_pylint_custom_plugins.py | 12 +++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 scripts/pylint_custom_plugin/tests/test_files/__init__.py diff --git a/scripts/pylint_custom_plugin/tests/test_files/__init__.py b/scripts/pylint_custom_plugin/tests/test_files/__init__.py new file mode 100644 index 000000000000..3f7e99ed1fee --- /dev/null +++ b/scripts/pylint_custom_plugin/tests/test_files/__init__.py @@ -0,0 +1,7 @@ +from something import Something +from something2 import something2 as somethingTwo + +__all__ = ( + Something, + somethingTwo, #pylint: disable=aliasing-generated-code +) \ No newline at end of file diff --git a/scripts/pylint_custom_plugin/tests/test_pylint_custom_plugins.py b/scripts/pylint_custom_plugin/tests/test_pylint_custom_plugins.py index 7fdd1e8bdb18..eca26b23993f 100644 --- a/scripts/pylint_custom_plugin/tests/test_pylint_custom_plugins.py +++ b/scripts/pylint_custom_plugin/tests/test_pylint_custom_plugins.py @@ -3,14 +3,15 @@ # Licensed under the MIT License. # ------------------------------------ +import argparse import astroid +import configparser import pylint.testutils from azure.core import PipelineClient from azure.core.configuration import Configuration from pylint_custom_plugin import pylint_guidelines_checker as checker - class TestClientMethodsHaveTracingDecorators(pylint.testutils.CheckerTestCase): CHECKER_CLASS = checker.ClientMethodsHaveTracingDecorators @@ -2638,3 +2639,12 @@ def test_ignores_import_init(self): with self.assertNoMessages(): self.checker.visit_module(module_node) + + def test_disable_pylint(self): + + file = open("./test_files/__init__.py") + node = astroid.parse(file.read()) + file.close() + + with self.assertNoMessages(): + self.checker.visit_module(node) From f331124ddbcab232ea2061c455dc5cadfb09bb3d Mon Sep 17 00:00:00 2001 From: Libba Lawrence Date: Tue, 8 Mar 2022 13:17:17 -0800 Subject: [PATCH 15/23] add from import test --- .../tests/test_pylint_custom_plugins.py | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/scripts/pylint_custom_plugin/tests/test_pylint_custom_plugins.py b/scripts/pylint_custom_plugin/tests/test_pylint_custom_plugins.py index eca26b23993f..02ebd02e129f 100644 --- a/scripts/pylint_custom_plugin/tests/test_pylint_custom_plugins.py +++ b/scripts/pylint_custom_plugin/tests/test_pylint_custom_plugins.py @@ -2648,3 +2648,34 @@ def test_disable_pylint(self): with self.assertNoMessages(): self.checker.visit_module(node) + + def test_from_import_alias(self): + import_one = astroid.extract_node( + 'import Something' + + ) + import_two = astroid.extract_node( + 'from Something2 import SomethingToo as SomethingTwo' + + ) + assign_one = astroid.extract_node( + """ + __all__ =( + "Something", + "SomethingTwo", + ) + """ + ) + + module_node = astroid.Module(name = "node", file="__init__.py", doc = """ """) + module_node.body = [import_one,import_two,assign_one] + + for name in module_node.body[-1].assigned_stmts(): + err_node = name.elts[1] + + with self.assertAddsMessages( + pylint.testutils.Message( + msg_id="aliasing-generated-code", node=err_node ,confidence=None + ) + ): + self.checker.visit_module(module_node) From 7e22b452594fd76dd6a69d7e35ff81124c86f8db Mon Sep 17 00:00:00 2001 From: Libba Lawrence Date: Tue, 8 Mar 2022 13:21:14 -0800 Subject: [PATCH 16/23] removed unused imports --- .../pylint_custom_plugin/tests/test_pylint_custom_plugins.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/scripts/pylint_custom_plugin/tests/test_pylint_custom_plugins.py b/scripts/pylint_custom_plugin/tests/test_pylint_custom_plugins.py index 02ebd02e129f..b0501d2b44ac 100644 --- a/scripts/pylint_custom_plugin/tests/test_pylint_custom_plugins.py +++ b/scripts/pylint_custom_plugin/tests/test_pylint_custom_plugins.py @@ -3,9 +3,7 @@ # Licensed under the MIT License. # ------------------------------------ -import argparse import astroid -import configparser import pylint.testutils from azure.core import PipelineClient From 741acb1ac19cf296378892ee8962a19b98f50dc5 Mon Sep 17 00:00:00 2001 From: Libba Lawrence Date: Tue, 8 Mar 2022 13:26:36 -0800 Subject: [PATCH 17/23] changing test names for clarity --- .../tests/test_pylint_custom_plugins.py | 50 +++++++++---------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/scripts/pylint_custom_plugin/tests/test_pylint_custom_plugins.py b/scripts/pylint_custom_plugin/tests/test_pylint_custom_plugins.py index b0501d2b44ac..e5dcddeb6e62 100644 --- a/scripts/pylint_custom_plugin/tests/test_pylint_custom_plugins.py +++ b/scripts/pylint_custom_plugin/tests/test_pylint_custom_plugins.py @@ -2583,7 +2583,7 @@ def test_ignores_correct_alias_code(self): with self.assertNoMessages(): self.checker.visit_module(module_node) - def test_catches_incorrect_alias_code(self): + def test_catches_incorrect_import_alias_code(self): import_one = astroid.extract_node( 'import Something' @@ -2614,20 +2614,20 @@ def test_catches_incorrect_alias_code(self): ): self.checker.visit_module(module_node) - def test_ignores_import_init(self): + def test_catches_incorrect_from_import_alias_code(self): import_one = astroid.extract_node( 'import Something' ) import_two = astroid.extract_node( - 'import Something2 as SomethingTwo' + 'from Something2 import SomethingToo as SomethingTwo' ) assign_one = astroid.extract_node( """ __all__ =( "Something", - "Something2", + "SomethingTwo", ) """ ) @@ -2635,32 +2635,30 @@ def test_ignores_import_init(self): module_node = astroid.Module(name = "node", file="__init__.py", doc = """ """) module_node.body = [import_one,import_two,assign_one] - with self.assertNoMessages(): + for name in module_node.body[-1].assigned_stmts(): + err_node = name.elts[1] + + with self.assertAddsMessages( + pylint.testutils.Message( + msg_id="aliasing-generated-code", node=err_node ,confidence=None + ) + ): self.checker.visit_module(module_node) - - def test_disable_pylint(self): - - file = open("./test_files/__init__.py") - node = astroid.parse(file.read()) - file.close() - - with self.assertNoMessages(): - self.checker.visit_module(node) - def test_from_import_alias(self): + def test_ignores_unaliased_import_init(self): import_one = astroid.extract_node( 'import Something' ) import_two = astroid.extract_node( - 'from Something2 import SomethingToo as SomethingTwo' + 'import Something2 as SomethingTwo' ) assign_one = astroid.extract_node( """ __all__ =( "Something", - "SomethingTwo", + "Something2", ) """ ) @@ -2668,12 +2666,14 @@ def test_from_import_alias(self): module_node = astroid.Module(name = "node", file="__init__.py", doc = """ """) module_node.body = [import_one,import_two,assign_one] - for name in module_node.body[-1].assigned_stmts(): - err_node = name.elts[1] - - with self.assertAddsMessages( - pylint.testutils.Message( - msg_id="aliasing-generated-code", node=err_node ,confidence=None - ) - ): + with self.assertNoMessages(): self.checker.visit_module(module_node) + + def test_disable_pylint_alias(self): + + file = open("./test_files/__init__.py") + node = astroid.parse(file.read()) + file.close() + + with self.assertNoMessages(): + self.checker.visit_module(node) From b574a63dc77e7c5f0346ea6fe57c18fe603f802f Mon Sep 17 00:00:00 2001 From: Libba Lawrence Date: Wed, 9 Mar 2022 10:02:24 -0800 Subject: [PATCH 18/23] added newline --- scripts/pylint_custom_plugin/tests/test_files/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/pylint_custom_plugin/tests/test_files/__init__.py b/scripts/pylint_custom_plugin/tests/test_files/__init__.py index 3f7e99ed1fee..5ca40377ea8d 100644 --- a/scripts/pylint_custom_plugin/tests/test_files/__init__.py +++ b/scripts/pylint_custom_plugin/tests/test_files/__init__.py @@ -4,4 +4,4 @@ __all__ = ( Something, somethingTwo, #pylint: disable=aliasing-generated-code -) \ No newline at end of file +) From ded86777e20a70df0876f9eb857a95123ae6a31d Mon Sep 17 00:00:00 2001 From: Libba Lawrence Date: Wed, 9 Mar 2022 10:28:37 -0800 Subject: [PATCH 19/23] added link from Izzy --- scripts/pylint_custom_plugin/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/pylint_custom_plugin/README.md b/scripts/pylint_custom_plugin/README.md index d9ce89045a5a..13bba8706442 100644 --- a/scripts/pylint_custom_plugin/README.md +++ b/scripts/pylint_custom_plugin/README.md @@ -59,4 +59,4 @@ In the case of a false positive, use the disable command to remove the pylint er | 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_implementation.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#clients) | | 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. | -| aliasing-generated-code | Do not alias models imported from the generated code. | # pylint:disable=aliasing-generated-code | No guideline, just helps to not expose aliased generated code. | \ No newline at end of file +| aliasing-generated-code | Do not alias models imported from the generated code. | # pylint:disable=aliasing-generated-code | [link](https://github.com/Azure/autorest/blob/main/docs/generate/built-in-directives.md) | \ No newline at end of file From 0b69f3a36b84e63e2509bcaa15372ea5b2b4543c Mon Sep 17 00:00:00 2001 From: Libba Lawrence Date: Wed, 9 Mar 2022 10:31:53 -0800 Subject: [PATCH 20/23] fixed some issues with the links --- scripts/pylint_custom_plugin/README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/pylint_custom_plugin/README.md b/scripts/pylint_custom_plugin/README.md index 13bba8706442..cb8515e38e9b 100644 --- a/scripts/pylint_custom_plugin/README.md +++ b/scripts/pylint_custom_plugin/README.md @@ -46,14 +46,14 @@ In the case of a false positive, use the disable command to remove the pylint er | client-method-should-not-use-static-method | Use module level functions instead. | # pylint:disable=connection-string-should-not-be-constructor-param | [link](https://azure.github.io/azure-sdk/python_design.html#constructors-and-factory-methods) | | 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#constructors-and-factory-methods) | | 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#constructors-and-factory-methods) | -| 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_design.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_design.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_design.html#naming-conventions) | +| 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_design.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_design.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_design.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#constructors-and-factory-methods) | | 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_design.html#public-vs-private) | +| 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_design.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_design.html#method-signatures) | | 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#constructors-and-factory-methods) | | 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_implementation.html#packaging) | From 2698c99875102257d86749d991d3aa2db361a03a Mon Sep 17 00:00:00 2001 From: Libba Lawrence Date: Wed, 9 Mar 2022 10:32:24 -0800 Subject: [PATCH 21/23] fixed some issues with the links2 --- scripts/pylint_custom_plugin/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/pylint_custom_plugin/README.md b/scripts/pylint_custom_plugin/README.md index cb8515e38e9b..0fa8d4f4b39d 100644 --- a/scripts/pylint_custom_plugin/README.md +++ b/scripts/pylint_custom_plugin/README.md @@ -54,7 +54,7 @@ In the case of a false positive, use the disable command to remove the pylint er | 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_design.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_design.html#method-signatures) | +| 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_design.html#method-signatures) | | 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#constructors-and-factory-methods) | | 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_implementation.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#clients) | From 0501c817ecad46780ff39c23c8909d52c669d0c6 Mon Sep 17 00:00:00 2001 From: Libba Lawrence Date: Wed, 9 Mar 2022 12:14:34 -0800 Subject: [PATCH 22/23] fix for running core on pylint pr --- sdk/core/ci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sdk/core/ci.yml b/sdk/core/ci.yml index 3f029f1d8a48..126ff1db012c 100644 --- a/sdk/core/ci.yml +++ b/sdk/core/ci.yml @@ -12,6 +12,7 @@ trigger: - sdk/core/ - eng/ - tools/ + - scripts/pylint_custom_plugin/ pr: branches: @@ -26,6 +27,7 @@ pr: - sdk/core/ - eng/ - tools/ + - scripts/pylint_custom_plugin/ extends: template: ../../eng/pipelines/templates/stages/archetype-sdk-client.yml From d7fccddd0ac5580e9df6c4308b3b7ea125144ea8 Mon Sep 17 00:00:00 2001 From: Libba Lawrence Date: Wed, 9 Mar 2022 15:06:30 -0800 Subject: [PATCH 23/23] removed cr --- sdk/core/ci.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/sdk/core/ci.yml b/sdk/core/ci.yml index 126ff1db012c..3f029f1d8a48 100644 --- a/sdk/core/ci.yml +++ b/sdk/core/ci.yml @@ -12,7 +12,6 @@ trigger: - sdk/core/ - eng/ - tools/ - - scripts/pylint_custom_plugin/ pr: branches: @@ -27,7 +26,6 @@ pr: - sdk/core/ - eng/ - tools/ - - scripts/pylint_custom_plugin/ extends: template: ../../eng/pipelines/templates/stages/archetype-sdk-client.yml