Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Python] Remove deprecated type aliases from generated code for python 3.9+. #5974

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed a bug in generation when a referenced schema in an allOf was a primitive [#5701](https://github.com/microsoft/kiota/issues/5701).
- Fixed a bug where inherited error models would be missing interface declarations. [#5888](https://github.com/microsoft/kiota/issues/5888)
- Fixed a bug where oneOf/anyOf schemas with single references to inheritance or intersections would be missing properties. [#5921](https://github.com/microsoft/kiota/issues/5921)
- 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after the release, this needs to be moved


## [1.21.0] - 2024-12-05

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
4 changes: 2 additions & 2 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
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
Loading