diff --git a/CHANGELOG.md b/CHANGELOG.md index 86859b2723..ae196fe262 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- Fixed Python error when a class inherits from a base class and implements an interface. [5637](https://github.com/microsoft/kiota/issues/5637) + ## [1.20.0] - 2024-11-07 ### Added diff --git a/it/config.json b/it/config.json index 1e7120f803..0f0115e8a3 100644 --- a/it/config.json +++ b/it/config.json @@ -245,7 +245,7 @@ }, { "Language": "python", - "Rationale": "https://github.com/microsoft/kiota/issues/5637" + "Rationale": "https://github.com/microsoft/kiota/issues/5744" } ] }, @@ -262,4 +262,4 @@ "Suppressions": [], "IdempotencySuppressions": [] } -} +} \ No newline at end of file diff --git a/src/Kiota.Builder/Writers/Python/CodeClassDeclarationWriter.cs b/src/Kiota.Builder/Writers/Python/CodeClassDeclarationWriter.cs index 765dc76942..89aec8f6dd 100644 --- a/src/Kiota.Builder/Writers/Python/CodeClassDeclarationWriter.cs +++ b/src/Kiota.Builder/Writers/Python/CodeClassDeclarationWriter.cs @@ -22,7 +22,16 @@ public override void WriteCodeElement(ClassDeclaration codeElement, LanguageWrit _codeUsingWriter.WriteConditionalInternalImports(codeElement, writer, parentNamespace); } + WriteParentClassImportsAndDecorators(codeElement, writer); + var derivation = GetDerivation(codeElement); + writer.WriteLine($"class {codeElement.Name}({derivation}):"); + writer.IncreaseIndent(); + WriteInnerClassImportsAndDescriptions(codeElement, writer, parentNamespace); + } + + private void WriteParentClassImportsAndDecorators(ClassDeclaration codeElement, LanguageWriter writer) + { if (codeElement.Parent is CodeClass parentClass) { if (codeElement.Inherits != null) @@ -34,15 +43,32 @@ public override void WriteCodeElement(ClassDeclaration codeElement, LanguageWrit writer.WriteLine("@dataclass"); } } + } + private string GetDerivation(ClassDeclaration codeElement) + { var abcClass = !codeElement.Implements.Any() ? string.Empty : $"{codeElement.Implements.Select(static x => x.Name).Aggregate((x, y) => x + ", " + y)}"; - var derivation = codeElement.Inherits is CodeType inheritType && + var baseClass = codeElement.Inherits is CodeType inheritType && conventions.GetTypeString(inheritType, codeElement) is string inheritSymbol && !string.IsNullOrEmpty(inheritSymbol) ? inheritSymbol : - abcClass; - writer.WriteLine($"class {codeElement.Name}({derivation}):"); - writer.IncreaseIndent(); + string.Empty; + if (string.IsNullOrEmpty(baseClass)) + { + return abcClass; + } + else if (string.IsNullOrEmpty(abcClass)) + { + return baseClass; + } + else + { + return $"{baseClass}, {abcClass}"; + } + } + + private void WriteInnerClassImportsAndDescriptions(ClassDeclaration codeElement, LanguageWriter writer, CodeNamespace parentNamespace) + { if (codeElement.Parent is CodeClass parent) { if (parent.Parent is CodeClass) // write imports for inner classes