Skip to content

Commit

Permalink
Merge pull request #5453 from microsoft/andrueastman/generationTyping…
Browse files Browse the repository at this point in the history
…Python

Fixes incosistent typing information in python generation
  • Loading branch information
andrueastman authored Sep 23, 2024
2 parents 514770a + ff986f0 commit 04f2076
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed a bug where the description special characters are encoded. [5286](https://github.com/microsoft/kiota/issues/5286)
- Fixed a bug where python constructor parameters are being cast to strings leading to bugs as the types is unknown on graph call. [microsoftgraph/msgraph-sdk-python#165](https://github.com/microsoftgraph/msgraph-sdk-python/issues/165)
- Fixed a bug where child path segment from single parameter path segment would be incorrectly escaped. [#5433](https://github.com/microsoft/kiota/issues/5433)
- Fixed inconsistent typing information generated for `ParsableFactory` and stream return types in python [kiota-abstractions-python#533](https://github.com/microsoft/kiota-abstractions-python/issues/333)

## [1.18.0] - 2024-09-05

Expand Down
7 changes: 4 additions & 3 deletions src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ private void WriteRequestExecutorBody(CodeMethod codeElement, RequestParams requ
{
_codeUsingWriter.WriteInternalErrorMappingImports(parentClass, writer);
errorMappingVarName = "error_mapping";
writer.StartBlock($"{errorMappingVarName}: Dict[str, ParsableFactory] = {{");
writer.StartBlock($"{errorMappingVarName}: Dict[str, type[ParsableFactory]] = {{");
foreach (var errorMapping in codeElement.ErrorMappings)
{
writer.WriteLine($"\"{errorMapping.Key.ToUpperInvariant()}\": {errorMapping.Value.Name},");
Expand Down Expand Up @@ -736,8 +736,9 @@ private void WriteMethodPrototype(CodeMethod code, LanguageWriter writer, string
.Select(p => new PythonConventionService() // requires a writer instance because method parameters use inline type definitions
.GetParameterSignature(p, code, writer))
.ToList());
var nullablePrefix = code.ReturnType.IsNullable && !isVoid ? "Optional[" : string.Empty;
var nullableSuffix = code.ReturnType.IsNullable && !isVoid ? "]" : string.Empty;
var isStreamType = conventions.StreamTypeName.Equals(returnType, StringComparison.OrdinalIgnoreCase);
var nullablePrefix = (code.ReturnType.IsNullable || isStreamType) && !isVoid ? "Optional[" : string.Empty;
var nullableSuffix = (code.ReturnType.IsNullable || isStreamType) && !isVoid ? "]" : string.Empty;
var propertyDecorator = code.Kind switch
{
CodeMethodKind.Getter => "@property",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ public void WritesRequestExecutorBody()
Assert.Contains("from .error401 import Error401", result);
Assert.Contains("from .error4_x_x import Error4XX", result);
Assert.Contains("from .error5_x_x import Error5XX", result);
Assert.Contains("error_mapping: Dict[str, ParsableFactory] =", result);
Assert.Contains("error_mapping: Dict[str, type[ParsableFactory]] =", result);
Assert.Contains("\"4XX\": Error4XX", result);
Assert.Contains("\"5XX\": Error5XX", result);
Assert.Contains("\"401\": Error401", result);
Expand Down

0 comments on commit 04f2076

Please sign in to comment.