From 7119dc60ebbd03e181d6d4923394de469289eb13 Mon Sep 17 00:00:00 2001 From: Guillaume Dequenne Date: Tue, 26 Nov 2024 09:41:53 +0100 Subject: [PATCH] SONARPY-2362 Document no issue on S6542 when inheriting from annotated class member (#2174) --- .../checks/useOfAnyAsTypeHintCheck/useOfAnyAsTypeHint.py | 8 ++++++++ .../useOfAnyAsTypeHintCheck/useOfAnyAsTypeHintImported.py | 4 ++++ .../useOfAnyAsTypeHintImporting.py | 6 +++++- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/python-checks/src/test/resources/checks/useOfAnyAsTypeHintCheck/useOfAnyAsTypeHint.py b/python-checks/src/test/resources/checks/useOfAnyAsTypeHintCheck/useOfAnyAsTypeHint.py index 58d9cb62f5..1571aef0b1 100644 --- a/python-checks/src/test/resources/checks/useOfAnyAsTypeHintCheck/useOfAnyAsTypeHint.py +++ b/python-checks/src/test/resources/checks/useOfAnyAsTypeHintCheck/useOfAnyAsTypeHint.py @@ -118,3 +118,11 @@ def return_type_check(self, text) -> Any: # Compliant @my_annotation[42] def some_annotated_method(self, text: Any): # Noncompliant ... + +from typing import Callable +class LocalClassWithAnnotatedMember: + my_member: Callable[[Any],Any] # No issue on nested values of "Callable" + +class LocalClassChild(LocalClassWithAnnotatedMember): + def my_member(self, param: Any) -> Any: # OK, defined in parent + ... diff --git a/python-checks/src/test/resources/checks/useOfAnyAsTypeHintCheck/useOfAnyAsTypeHintImported.py b/python-checks/src/test/resources/checks/useOfAnyAsTypeHintCheck/useOfAnyAsTypeHintImported.py index d7054a2333..d2d8ef7176 100644 --- a/python-checks/src/test/resources/checks/useOfAnyAsTypeHintCheck/useOfAnyAsTypeHintImported.py +++ b/python-checks/src/test/resources/checks/useOfAnyAsTypeHintCheck/useOfAnyAsTypeHintImported.py @@ -3,3 +3,7 @@ class ImportedParentWithoutMetaClass: ... class ImportedParentWithMetaClass(metaclass=ABCMeta): ... + +from typing import Callable +class MyClassWithAnnotatedMember: + my_member: Callable[[Any],Any] # No issue on nested values of "Callable" diff --git a/python-checks/src/test/resources/checks/useOfAnyAsTypeHintCheck/useOfAnyAsTypeHintImporting.py b/python-checks/src/test/resources/checks/useOfAnyAsTypeHintCheck/useOfAnyAsTypeHintImporting.py index 597b555f7a..62eeb6e681 100644 --- a/python-checks/src/test/resources/checks/useOfAnyAsTypeHintCheck/useOfAnyAsTypeHintImporting.py +++ b/python-checks/src/test/resources/checks/useOfAnyAsTypeHintCheck/useOfAnyAsTypeHintImporting.py @@ -1,6 +1,6 @@ from typing import Any from abc import ABCMeta -from useOfAnyAsTypeHintImported import ImportedParentWithoutMetaClass, ImportedParentWithMetaClass +from useOfAnyAsTypeHintImported import ImportedParentWithoutMetaClass, ImportedParentWithMetaClass, MyClassWithAnnotatedMember class LocalParentWithMetaClass(metaclass=ABCMeta): ... @@ -14,3 +14,7 @@ def imported_inherited_foo(self) -> Any: # Noncompliant class ImportedWithMetaClassInherited(ImportedParentWithMetaClass): def imported_inherited_foo(self) -> Any: # Noncompliant ... + +class MyChild(MyClassWithAnnotatedMember): + def my_member(self, param: Any) -> Any: # OK, defined in parent + ...