Skip to content

Commit

Permalink
investigate __future__ import bug
Browse files Browse the repository at this point in the history
  • Loading branch information
pjmagee committed Jun 10, 2024
1 parent c48c7f5 commit 455d74b
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 1 deletion.
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
100 changes: 99 additions & 1 deletion tests/Kiota.Builder.Tests/Writers/Python/CodeUsingWriterTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;
using System.Linq;
using Kiota.Builder.CodeDOM;
using Kiota.Builder.Writers;
Expand All @@ -21,6 +22,7 @@ public CodeUsingWriterTests()
writer.SetTextWriter(tw);
root = CodeNamespace.InitRootNamespace();
}

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

[Fact]
public void WritesFutureImportsFirst()
{
var usingWriter = new CodeUsingWriter("foo");

var cd = new ClassDeclaration
{
Name = "bar",
};

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

cd.AddUsings(new CodeUsing
{
Name = "datetime",
Declaration = new CodeType
{
Name = "-",
IsExternal = true
},

});

cd.AddUsings(new CodeUsing
{
Name = "annotations",
Declaration = new CodeType
{
Name = "__future__",
IsExternal = true
},

});

cd.AddUsings(new CodeUsing
{
Name = "dataclass",
Declaration = new CodeType
{
Name = "dataclasses",
IsExternal = true
}
});

cd.AddUsings(new CodeUsing
{
Name = "field",
Declaration = new CodeType
{
Name = "dataclasses",
IsExternal = true
}
});

cd.AddUsings(new CodeUsing[]
{
new CodeUsing
{
Name = "Parsable",
Declaration = new CodeType
{
Name = "kiota_abstractions.serialization",
IsExternal = true
}
}, new CodeUsing
{
Name = "ParseNode",
Declaration = new CodeType
{
Name = "kiota_abstractions.serialization",
IsExternal = true
}
},
new CodeUsing
{
Name = "SerializationWriter",
Declaration = new CodeType
{
Name = "kiota_abstractions.serialization",
IsExternal = true
}
}}
);

usingWriter.WriteExternalImports(cd, writer);
var result = tw.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);

Assert.Equal("from __future__ import annotations", result[0]);
Assert.Equal("import datetime", result[1]);
}
}

0 comments on commit 455d74b

Please sign in to comment.