Skip to content

Commit

Permalink
Merge pull request #5724 from microsoft/shem/fix_local_imports
Browse files Browse the repository at this point in the history
Fix python failing integration tests
  • Loading branch information
baywet authored Nov 11, 2024
2 parents 105123a + eea21e8 commit 5922724
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions it/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@
},
{
"Language": "python",
"Rationale": "https://github.com/microsoft/kiota/issues/5637"
"Rationale": "https://github.com/microsoft/kiota/issues/5744"
}
]
},
Expand All @@ -271,4 +271,4 @@
"Suppressions": [],
"IdempotencySuppressions": []
}
}
}
34 changes: 30 additions & 4 deletions src/Kiota.Builder/Writers/Python/CodeClassDeclarationWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 5922724

Please sign in to comment.