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
Changes from 1 commit
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
Next Next commit
investigate __future__ import bug
pjmagee committed Jun 10, 2024
commit 455d74b3dcc91b8deb976726c0ea6ffb24be6981
3 changes: 3 additions & 0 deletions src/Kiota.Builder/Writers/Python/CodeUsingWriter.cs
Original file line number Diff line number Diff line change
@@ -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)
{
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;
@@ -21,6 +22,7 @@ public CodeUsingWriterTests()
writer.SetTextWriter(tw);
root = CodeNamespace.InitRootNamespace();
}

[Fact]
public void WritesAliasedSymbol()
{
@@ -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]);
}
}
Loading