From d69535d30f8872a40279434ed94cf8b8cfae91cf Mon Sep 17 00:00:00 2001 From: Maksim Grebeniuk Date: Wed, 13 Nov 2024 14:37:18 +0100 Subject: [PATCH] SONARPY-2330 Add unit test case on S6542 when a class is inherited from an imported class with a metaclass --- .../checks/UseOfAnyAsTypeHintCheckTest.java | 12 ++++++++++++ .../checks/useOfAnyAsTypeHintImported.py | 3 +++ .../checks/useOfAnyAsTypeHintImporting.py | 15 +++++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 python-checks/src/test/resources/checks/useOfAnyAsTypeHintImported.py create mode 100644 python-checks/src/test/resources/checks/useOfAnyAsTypeHintImporting.py diff --git a/python-checks/src/test/java/org/sonar/python/checks/UseOfAnyAsTypeHintCheckTest.java b/python-checks/src/test/java/org/sonar/python/checks/UseOfAnyAsTypeHintCheckTest.java index abc837667c..d1bf2d0f94 100644 --- a/python-checks/src/test/java/org/sonar/python/checks/UseOfAnyAsTypeHintCheckTest.java +++ b/python-checks/src/test/java/org/sonar/python/checks/UseOfAnyAsTypeHintCheckTest.java @@ -28,6 +28,7 @@ class UseOfAnyAsTypeHintCheckTest { void useOfAny() { PythonCheckVerifier.verify("src/test/resources/checks/useOfAnyAsTypeHint.py", new UseOfAnyAsTypeHintCheck()); } + @Test void useOfTypingAny() { PythonCheckVerifier.verify("src/test/resources/checks/useOftypingAnyAsTypeHint.py", new UseOfAnyAsTypeHintCheck()); @@ -42,4 +43,15 @@ void useOfUserDefinedTypeCalledAny() { void useOfOverrideOrOverloadDecorator() { PythonCheckVerifier.verify(List.of("src/test/resources/checks/useOfOverrideOrOverloadDecorator.py", "src/test/resources/checks/reexport_typing_overload_override.py"), new UseOfAnyAsTypeHintCheck()); } + + @Test + void useOfAnyImpored() { + PythonCheckVerifier.verify( + List.of( + "src/test/resources/checks/useOfAnyAsTypeHintImported.py", + "src/test/resources/checks/useOfAnyAsTypeHintImporting.py" + ), + new UseOfAnyAsTypeHintCheck() + ); + } } diff --git a/python-checks/src/test/resources/checks/useOfAnyAsTypeHintImported.py b/python-checks/src/test/resources/checks/useOfAnyAsTypeHintImported.py new file mode 100644 index 0000000000..402727afa6 --- /dev/null +++ b/python-checks/src/test/resources/checks/useOfAnyAsTypeHintImported.py @@ -0,0 +1,3 @@ +from abc import ABCMeta + +class ImportedParentWithMetaClass(metaclass=ABCMeta): ... diff --git a/python-checks/src/test/resources/checks/useOfAnyAsTypeHintImporting.py b/python-checks/src/test/resources/checks/useOfAnyAsTypeHintImporting.py new file mode 100644 index 0000000000..554453bcb4 --- /dev/null +++ b/python-checks/src/test/resources/checks/useOfAnyAsTypeHintImporting.py @@ -0,0 +1,15 @@ +from typing import Any +from abc import ABCMeta +from useOfAnyAsTypeHintImported import ImportedParentWithMetaClass + +class LocalParent: ... + +class LocalParentWithMetaClass(metaclass=ABCMeta): ... + +class LocalWithMetaClassInherited(LocalParentWithMetaClass): + def local_inherited_foo(self) -> Any: # Noncompliant + ... + +class ImportedWithMetaClassInherited(ImportedParentWithMetaClass): + def imported_inherited_foo(self) -> Any: # FN SONARPY-2331 + ...