Skip to content

Commit

Permalink
Merge pull request #5974 from microsoft/andrueastman/python3.9
Browse files Browse the repository at this point in the history
[Python] Remove deprecated type aliases from generated code for python 3.9+.
  • Loading branch information
andrueastman authored Jan 13, 2025
2 parents d24e5bd + 8edd5ca commit 1e59299
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 26 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Drops Python 3.8 support by removing deprecated type aliases from generated code. [microsoft/kiota-python#349](https://github.com/microsoft/kiota-python/issues/349)
- Removes superfluous inline imports in serializer methods in Python Generation.

## [1.22.2]

### Added

### Changed

- Fixed a bug showing an error popup in the VS Code extension after generation [5988](https://github.com/microsoft/kiota/issues/5988)

## [1.22.1] - 2025-01-10

### Added

### Changed

- Fixed a bug in the VS Code extension plugin generation [#5978](https://github.com/microsoft/kiota/issues/5978)

## [1.22.0] - 2025-01-09
Expand Down
23 changes: 14 additions & 9 deletions src/Kiota.Builder/Refiners/PythonRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ public override Task RefineAsync(CodeNamespace generatedCode, CancellationToken
private const string StoreModuleName = $"{AbstractionsPackageName}.store";
private static readonly AdditionalUsingEvaluator[] defaultUsingEvaluators = {
new (static x => x is CodeClass, "__future__", "annotations"),
new (static x => x is CodeClass, "typing", "Any, Callable, Dict, List, Optional, TYPE_CHECKING, Union"),
new (static x => x is CodeClass, "typing", "Any, Optional, TYPE_CHECKING, Union"),
new (static x => x is CodeClass, "collections.abc", "Callable"),
new (static x => x is CodeProperty prop && prop.IsOfKind(CodePropertyKind.RequestAdapter),
$"{AbstractionsPackageName}.request_adapter", "RequestAdapter"),
new (static x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.RequestGenerator),
Expand Down Expand Up @@ -271,36 +272,40 @@ private static void CorrectPropertyType(CodeProperty currentProperty)
if (currentProperty.IsOfKind(CodePropertyKind.RequestAdapter))
currentProperty.Type.Name = "RequestAdapter";
else if (currentProperty.IsOfKind(CodePropertyKind.BackingStore))
currentProperty.Type.Name = currentProperty.Type.Name[1..]; // removing the "I"
currentProperty.Type.Name = currentProperty.Type.Name[1..].ToFirstCharacterUpperCase(); // removing the "I"
else if (currentProperty.IsOfKind(CodePropertyKind.Options))
currentProperty.Type.Name = "List[RequestOption]";
currentProperty.Type.Name = "list[RequestOption]";
else if (currentProperty.IsOfKind(CodePropertyKind.Headers))
currentProperty.Type.Name = "Dict[str, Union[str, List[str]]]";
currentProperty.Type.Name = "dict[str, Union[str, list[str]]]";
else if (currentProperty.IsOfKind(CodePropertyKind.AdditionalData))
{
currentProperty.Type.Name = "Dict[str, Any]";
currentProperty.Type.Name = "dict[str, Any]";
currentProperty.DefaultValue = "field(default_factory=dict)";
}
else if (currentProperty.IsOfKind(CodePropertyKind.PathParameters))
{
currentProperty.Type.IsNullable = false;
currentProperty.Type.Name = "Union[str, Dict[str, Any]]";
currentProperty.Type.Name = "Union[str, dict[str, Any]]";
if (!string.IsNullOrEmpty(currentProperty.DefaultValue))
currentProperty.DefaultValue = "{}";
}
else if (currentProperty.Kind is CodePropertyKind.Custom && currentProperty.Type.IsNullable && string.IsNullOrEmpty(currentProperty.DefaultValue))
{
currentProperty.DefaultValue = "None";
currentProperty.Type.Name = currentProperty.Type.Name.ToFirstCharacterUpperCase();
}
else
{
currentProperty.Type.Name = currentProperty.Type.Name.ToFirstCharacterUpperCase();
}
currentProperty.Type.Name = currentProperty.Type.Name.ToFirstCharacterUpperCase();
CorrectCoreTypes(currentProperty.Parent as CodeClass, DateTypesReplacements, currentProperty.Type);
}
private static void CorrectMethodType(CodeMethod currentMethod)
{
if (currentMethod.IsOfKind(CodeMethodKind.Serializer))
currentMethod.Parameters.Where(x => x.IsOfKind(CodeParameterKind.Serializer) && x.Type.Name.StartsWith('I')).ToList().ForEach(x => x.Type.Name = x.Type.Name[1..]);
else if (currentMethod.IsOfKind(CodeMethodKind.Deserializer))
currentMethod.ReturnType.Name = "Dict[str, Callable[[ParseNode], None]]";
currentMethod.ReturnType.Name = "dict[str, Callable[[ParseNode], None]]";
else if (currentMethod.IsOfKind(CodeMethodKind.ClientConstructor, CodeMethodKind.Constructor, CodeMethodKind.Factory))
{
currentMethod.Parameters.Where(x => x.IsOfKind(CodeParameterKind.RequestAdapter, CodeParameterKind.BackingStore, CodeParameterKind.ParseNode))
Expand All @@ -311,7 +316,7 @@ private static void CorrectMethodType(CodeMethod currentMethod)
if (urlTplParams != null &&
urlTplParams.Type is CodeType originalType)
{
originalType.Name = "Union[str, Dict[str, Any]]";
originalType.Name = "Union[str, dict[str, Any]]";
urlTplParams.Documentation.DescriptionTemplate = "The raw url or the url-template parameters for the request.";
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ private void WriteDeserializerBodyForIntersectionModel(CodeClass parentClass, La
private void WriteDeserializerBodyForInheritedModel(bool inherits, CodeMethod codeElement, CodeClass parentClass, LanguageWriter writer)
{
_codeUsingWriter.WriteInternalImports(parentClass, writer);
writer.StartBlock($"fields: Dict[str, Callable[[Any], {NoneKeyword}]] = {{");
writer.StartBlock($"fields: dict[str, Callable[[Any], {NoneKeyword}]] = {{");
foreach (var otherProp in parentClass
.GetPropertiesOfKind(CodePropertyKind.Custom)
.Where(static x => !x.ExistsInBaseType)
Expand Down Expand Up @@ -599,7 +599,7 @@ private void WriteRequestExecutorBody(CodeMethod codeElement, RequestParams requ
{
_codeUsingWriter.WriteInternalErrorMappingImports(parentClass, writer);
errorMappingVarName = "error_mapping";
writer.StartBlock($"{errorMappingVarName}: Dict[str, type[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 @@ -656,7 +656,6 @@ private void WriteSerializerBodyForInheritedModel(bool inherits, CodeClass paren
{
if (inherits)
writer.WriteLine("super().serialize(writer)");
_codeUsingWriter.WriteInternalImports(parentClass, writer);
foreach (var otherProp in parentClass
.GetPropertiesOfKind(CodePropertyKind.Custom)
.Where(static x => !x.ExistsInBaseType && !x.ReadOnly)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public override string GetTypeString(CodeTypeBase code, CodeElement targetElemen
ArgumentNullException.ThrowIfNull(targetElement);
if (code is null)
return string.Empty;
var collectionPrefix = code.CollectionKind == CodeTypeCollectionKind.None && includeCollectionInformation ? string.Empty : "List[";
var collectionPrefix = code.CollectionKind == CodeTypeCollectionKind.None && includeCollectionInformation ? string.Empty : "list[";
var collectionSuffix = code.CollectionKind == CodeTypeCollectionKind.None && includeCollectionInformation ? string.Empty : "]";
if (code is CodeComposedTypeBase currentUnion && currentUnion.Types.Any())
return currentUnion.Types.Select(x => GetTypeString(x, targetElement, true, writer)).Aggregate((x, y) => $"Union[{x}, {TranslateAllTypes(y)}]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,12 @@ private static void ValidateExportPython(string[] exportContents)
Assert.Contains("exportNamespace.models.microsoft.graph.User~~>AdditionalDataHolder; Parsable", exportContents);// captures implemented interfaces
Assert.Contains("exportNamespace.models.microsoft.graph.User::|public|id():str", exportContents);// captures property getter location,type and access inheritance
Assert.Contains("exportNamespace.models.microsoft.graph.User::|public|id(value:str):None", exportContents);// captures property setter location,type and access inheritance
Assert.Contains("exportNamespace.me.MeRequestBuilder::|public|constructor(path_parameters:Union[str, Dict[str, Any]]; request_adapter:RequestAdapter):None", exportContents); // captures constructors, their parameters(name and types), return and access
Assert.Contains("exportNamespace.me.MeRequestBuilder::|public|constructor(path_parameters:Union[str, dict[str, Any]]; request_adapter:RequestAdapter):None", exportContents); // captures constructors, their parameters(name and types), return and access
Assert.Contains("exportNamespace.me.get.GetRequestBuilder::|public|to_get_request_information(request_configuration?:RequestConfiguration[QueryParameters]):RequestInformation", exportContents);// captures methods, their parameters(name and types), return and access
Assert.Contains("exportNamespace.models.microsoft.graph.User::|static|public|create_from_discriminator_value(parse_node:ParseNode):User", exportContents);// captures static methods too :)
Assert.Contains("exportNamespace.models.microsoft.graph.Importance::0000-Low", exportContents);// captures enum members
Assert.Contains("exportNamespace.models.microsoft.graph.User::|public|other_names():List[str]", exportContents);// captures collection info in language specific format
Assert.Contains("exportNamespace.models.microsoft.graph.User::|public|other_names(value:List[str]):None", exportContents);// captures collection info in language specific format
Assert.Contains("exportNamespace.models.microsoft.graph.User::|public|other_names():list[str]", exportContents);// captures collection info in language specific format
Assert.Contains("exportNamespace.models.microsoft.graph.User::|public|other_names(value:list[str]):None", exportContents);// captures collection info in language specific format
}

private static void ValidateExportTypeScript(string[] exportContents)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public async Task AddsUsingsForErrorTypesForRequestExecutorAsync()
#region python
private const string HttpCoreDefaultName = "IRequestAdapter";
private const string FactoryDefaultName = "ISerializationWriterFactory";
private const string DeserializeDefaultName = "Dict[str, Callable[[ParseNode], None]]";
private const string DeserializeDefaultName = "dict[str, Callable[[ParseNode], None]]";
private const string PathParametersDefaultName = "Dictionary<string, object>";
private const string PathParametersDefaultValue = "new Dictionary<string, object>";
private const string DateTimeOffsetDefaultName = "DateTimeOffset";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,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, type[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 All @@ -643,7 +643,7 @@ public void DoesntCreateDictionaryOnEmptyErrorMapping()
AddRequestBodyParameters();
writer.Write(method);
var result = tw.ToString();
Assert.DoesNotContain("error_mapping: Dict[str, ParsableFactory]", result);
Assert.DoesNotContain("error_mapping: dict[str, ParsableFactory]", result);
Assert.Contains("cannot be null", result);
}
[Fact]
Expand Down Expand Up @@ -807,7 +807,7 @@ public void WritesUnionDeSerializerBody()
IsAsync = false,
ReturnType = new CodeType
{
Name = "Dict[str, Callable[[ParseNode], None]]",
Name = "dict[str, Callable[[ParseNode], None]]",
},
}).First();
writer.Write(deserializationMethod);
Expand Down Expand Up @@ -851,7 +851,7 @@ public void WritesIntersectionDeSerializerBody()
IsAsync = false,
ReturnType = new CodeType
{
Name = "Dict[str, Callable[[ParseNode], None]]",
Name = "dict[str, Callable[[ParseNode], None]]",
},
}).First();
writer.Write(deserializationMethod);
Expand All @@ -875,7 +875,7 @@ public void WritesDeSerializerBody()
writer.Write(method);
var result = tw.ToString();
Assert.Contains("from .somecustomtype import Somecustomtype", result);
Assert.Contains("fields: Dict[str, Callable[[Any], None]] =", result);
Assert.Contains("fields: dict[str, Callable[[Any], None]] =", result);
Assert.Contains("get_str_value()", result);
Assert.Contains("get_int_value()", result);
Assert.Contains("get_float_value()", result);
Expand Down Expand Up @@ -1748,7 +1748,7 @@ public void WritesConstructorForRequestBuilderWithRequestAdapterAndPathParameter
Kind = CodeParameterKind.PathParameters,
Type = new CodeType
{
Name = "Union[Dict[str, Any], str]",
Name = "Union[dict[str, Any], str]",
IsNullable = true,
},
});
Expand All @@ -1766,7 +1766,7 @@ public void WritesConstructorForRequestBuilderWithRequestAdapterAndPathParameter
writer.Write(method);
var result = tw.ToString();
Assert.DoesNotContain("super().__init__(self)", result);
Assert.Contains("def __init__(self,request_adapter: RequestAdapter, path_parameters: Union[Dict[str, Any], str],", result);
Assert.Contains("def __init__(self,request_adapter: RequestAdapter, path_parameters: Union[dict[str, Any], str],", result);
Assert.Contains("username: Optional[str] = None", result);
Assert.Contains("if isinstance(path_parameters, dict):", result);
Assert.Contains("path_parameters['username'] = username", result);
Expand Down Expand Up @@ -1959,7 +1959,7 @@ public void WritesConstructorWithInheritance()
Kind = CodeParameterKind.PathParameters,
Type = new CodeType
{
Name = "Union[Dict[str, Any], str]",
Name = "Union[dict[str, Any], str]",
IsNullable = true,
}
});
Expand All @@ -1983,7 +1983,7 @@ public void WritesApiConstructor()
Kind = CodePropertyKind.PathParameters,
Type = new CodeType
{
Name = "Dict[str, str]",
Name = "dict[str, str]",
IsExternal = true,
}
});
Expand Down

0 comments on commit 1e59299

Please sign in to comment.