Skip to content

Commit

Permalink
Merge pull request #28 from tonybaloney/test_typing
Browse files Browse the repository at this point in the history
Support the old-style types for List, Tuple and Dict
  • Loading branch information
tonybaloney authored Jul 19, 2024
2 parents e511ab7 + c37f8e5 commit 90bcd2b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 66 deletions.
3 changes: 0 additions & 3 deletions Integration.Tests/BasicTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Python.Generated;
using Python.Runtime;
using System.Collections.Immutable;
using System.Reflection;

namespace Integration.Tests;
Expand All @@ -22,8 +21,6 @@ public void TestBasic()
Assert.Equal([1, 2, 3], testModule.TestListOfInts([1, 2, 3]));
Assert.Equal("hello world", testModule.TestTwoStrings("hello ", "world"));
Assert.Equal(["hello", "world", "this", "is", "a", "test"], testModule.TestTwoListsOfStrings(["hello", "world"], ["this", "is", "a", "test"]));


}

[Fact]
Expand Down
1 change: 1 addition & 0 deletions PythonSourceGenerator.Tests/BasicSmokeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public BasicSmokeTest(TestEnvironment testEnv)
[InlineData("def hello_world(name: str) -> int:\n ...\n", "long HelloWorld(string name)")]
[InlineData("def hello_world(name: str, age: int) -> str:\n ...\n", "string HelloWorld(string name, long age)")]
[InlineData("def hello_world(numbers: list[float]) -> list[int]:\n ...\n", "IEnumerable<long> HelloWorld(IEnumerable<double> numbers)")]
[InlineData("def hello_world(numbers: List[float]) -> List[int]:\n ...\n", "IEnumerable<long> HelloWorld(IEnumerable<double> numbers)")]
[InlineData("def hello_world(a: bool, b: str, c: list[tuple[int, float]]) -> bool: \n ...\n", "bool HelloWorld(bool a, string b, IEnumerable<Tuple<long, double>> c)")]
[InlineData("def hello_world(a: bool = True, b: str = None) -> bool: \n ...\n", "bool HelloWorld(bool a = true, string b = null)")]
[InlineData("def hello_world(a: str, *args) -> None: \n ...\n", "void HelloWorld(string a, Tuple<PyObject> args = null)")]
Expand Down
62 changes: 0 additions & 62 deletions PythonSourceGenerator.Tests/IntegrationTests.cs

This file was deleted.

23 changes: 23 additions & 0 deletions PythonSourceGenerator.Tests/TypeReflectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,27 @@ public void AsPredefinedType(string pythonType, string expectedType)
var reflectedType = TypeReflection.AsPredefinedType(result.Value);
Assert.Equal(expectedType, reflectedType.ToString());
}

[Theory]
[InlineData("List[int]", "IEnumerable<long>")]
[InlineData("List[str]", "IEnumerable<string>")]
[InlineData("List[float]", "IEnumerable<double>")]
[InlineData("List[bool]", "IEnumerable<bool>")]
[InlineData("List[object]", "IEnumerable<PyObject>")]
[InlineData("Tuple[int, int]", "Tuple<long,long>")]
[InlineData("Tuple[str, str]", "Tuple<string,string>")]
[InlineData("Tuple[float, float]", "Tuple<double,double>")]
[InlineData("Tuple[bool, bool]", "Tuple<bool,bool>")]
[InlineData("Tuple[str, Any]", "Tuple<string,PyObject>")]
[InlineData("Tuple[str, list[int]]", "Tuple<string,IEnumerable<long>>")]
[InlineData("Dict[str, int]", "IReadOnlyDictionary<string,long>")]
[InlineData("Tuple[int, int, Tuple[int, int]]", "Tuple<long,long,Tuple<long,long>>")]
public void AsPredefinedTypeOldTypeNames(string pythonType, string expectedType)
{
var tokens = PythonSignatureTokenizer.Instance.Tokenize(pythonType);
var result = PythonSignatureParser.PythonTypeDefinitionTokenizer.TryParse(tokens);
Assert.True(result.HasValue);
var reflectedType = TypeReflection.AsPredefinedType(result.Value);
Assert.Equal(expectedType, reflectedType.ToString());
}
}
2 changes: 1 addition & 1 deletion PythonSourceGenerator/Reflection/TypeReflection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static TypeSyntax AsPredefinedType(PythonTypeSpec pythonType)
// If type is an alias, e.g. "list[int]", "list[float]", etc.
if (pythonType.HasArguments())
{
var genericName = pythonType.Name;
var genericName = pythonType.Name.ToLowerInvariant();
// Get last occurrence of ] in pythonType
return genericName switch
{
Expand Down

0 comments on commit 90bcd2b

Please sign in to comment.