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

investigate __future__ import bug #4802

Draft
wants to merge 3 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
3 changes: 3 additions & 0 deletions src/Kiota.Builder/Writers/Python/CodeUsingWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public void WriteExternalImports(ClassDeclaration codeElement, LanguageWriter wr
.Select(static x => (x.Name, string.Empty, x.Declaration?.Name))
.GroupBy(static x => x.Item3)
.OrderBy(static x => x.Key)
// TODO: figure out why __future__ imports are not being sorted correctly
// .OrderBy(static x => !x.Key!.StartsWith("__future__", StringComparison.OrdinalIgnoreCase))
// .ThenBy(static x => x.Key)
.ToArray();
if (externalImportSymbolsAndPaths.Length != 0)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.IO;
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using Kiota.Builder.CodeDOM;
using Kiota.Builder.Writers;
using Kiota.Builder.Writers.Python;
Expand All @@ -16,11 +19,12 @@ public class CodeUsingWriterTests
private readonly CodeNamespace root;
public CodeUsingWriterTests()
{
writer = LanguageWriter.GetLanguageWriter(GenerationLanguage.TypeScript, DefaultPath, DefaultName);
writer = LanguageWriter.GetLanguageWriter(GenerationLanguage.Python, DefaultPath, DefaultName);
tw = new StringWriter();
writer.SetTextWriter(tw);
root = CodeNamespace.InitRootNamespace();
}

[Fact]
public void WritesAliasedSymbol()
{
Expand Down Expand Up @@ -89,4 +93,93 @@ public void DoesntAliasRegularSymbols()
var result = tw.ToString();
Assert.Contains("from .bar import Bar", result);
}

[Theory]
[InlineData("en-US")]
[InlineData("en-GB")]
[InlineData("fr-FR")]
[InlineData("de-DE")]
[InlineData("es-ES")]
[InlineData("it-IT")]
[InlineData("ja-JP")]
[InlineData("ko-KR")]
[InlineData("pt-BR")]
[InlineData("ru-RU")]
[InlineData("zh-CN")]
[InlineData("zh-TW")]
public void FutureShouldBeSortedBeforeDatetimeInDifferentCultures(string cultureName)
{
var cultureInfo = new CultureInfo(cultureName);
var compareInfo = cultureInfo.CompareInfo;

var sorted = new string[] { null, "__future__" }.OrderBy(x => x, StringComparer.Create(cultureInfo, CompareOptions.IgnoreCase)).ToArray();

// Half pass, half fail
// I know in the CodeUsingWriter - We are grouping on x.Declaration?.Name - Which can be null?
Assert.True(sorted[0] == "__future__");
}

[Theory]
[InlineData("en-US")]
[InlineData("en-GB")]
[InlineData("fr-FR")]
[InlineData("de-DE")]
[InlineData("es-ES")]
[InlineData("it-IT")]
[InlineData("ja-JP")]
[InlineData("ko-KR")]
[InlineData("pt-BR")]
[InlineData("ru-RU")]
[InlineData("zh-CN")]
[InlineData("zh-TW")]
public void WritesFutureImportsFirst(string cultureName)
{
// Generated with Kiota mcr.microsoft.com/openapi/kiota:1.15.0

// import datetime
// from __future__ import annotations
// from dataclasses import dataclass, field
// from kiota_abstractions.serialization import AdditionalDataHolder, Parsable, ParseNode, SerializationWriter
// from typing import Any, Callable, Dict, List, Optional, TYPE_CHECKING, Union

Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureName);

var writer2 = LanguageWriter.GetLanguageWriter(GenerationLanguage.Python, DefaultPath, DefaultName);
var tw2 = new StringWriter();
writer2.SetTextWriter(tw2);
var root2 = CodeNamespace.InitRootNamespace();

var usingWriter = new CodeUsingWriter("foo");

var codeClass = new ClassDeclaration
{
Name = "Test",
};

codeClass.AddUsings(new CodeUsing { Name = "annotations", Declaration = new CodeType { Name = "__future__", IsExternal = true } });
codeClass.AddUsings(new CodeUsing { Name = "dataclass", Declaration = new CodeType { Name = "dataclasses", IsExternal = true } });
codeClass.AddUsings(new CodeUsing { Name = "field", Declaration = new CodeType { Name = "dataclasses", IsExternal = true } });
codeClass.AddUsings(new CodeUsing { Name = "AdditionalDataHolder", Declaration = new CodeType { Name = "kiota_abstractions.serialization", IsExternal = true } });
codeClass.AddUsings(new CodeUsing { Name = "Parsable", Declaration = new CodeType { Name = "kiota_abstractions.serialization", IsExternal = true } });
codeClass.AddUsings(new CodeUsing { Name = "ParseNode", Declaration = new CodeType { Name = "kiota_abstractions.serialization", IsExternal = true } });
codeClass.AddUsings(new CodeUsing { Name = "SerializationWriter", Declaration = new CodeType { Name = "kiota_abstractions.serialization", IsExternal = true } });
codeClass.AddUsings(new CodeUsing { Name = "Any", Declaration = new CodeType { Name = "typing", IsExternal = true } });
codeClass.AddUsings(new CodeUsing { Name = "Callable", Declaration = new CodeType { Name = "typing", IsExternal = true } });
codeClass.AddUsings(new CodeUsing { Name = "Dict", Declaration = new CodeType { Name = "typing", IsExternal = true } });
codeClass.AddUsings(new CodeUsing { Name = "List", Declaration = new CodeType { Name = "typing", IsExternal = true } });
codeClass.AddUsings(new CodeUsing { Name = "Optional", Declaration = new CodeType { Name = "typing", IsExternal = true } });
codeClass.AddUsings(new CodeUsing { Name = "TYPE_CHECKING", Declaration = new CodeType { Name = "typing", IsExternal = true } });
codeClass.AddUsings(new CodeUsing { Name = "Union", Declaration = new CodeType { Name = "typing", IsExternal = true } });
codeClass.AddUsings(new CodeUsing { Name = "datetime", Declaration = new CodeType { Name = "-", IsExternal = true } });

usingWriter.WriteExternalImports(codeClass, writer2);

string[] usings = tw2.ToString().Split(tw2.NewLine, StringSplitOptions.RemoveEmptyEntries);

Assert.Equal("from __future__ import annotations", usings.First());
Assert.Equal("import datetime", usings[1]);
Assert.Equal("from dataclasses import dataclass, field", usings[2]);
Assert.Equal("from kiota_abstractions.serialization import AdditionalDataHolder, Parsable, ParseNode, SerializationWriter", usings[3]);
Assert.Equal("from typing import Any, Callable, Dict, List, Optional, TYPE_CHECKING, Union", usings[4]);
}
}
Loading