Skip to content

Linting and Formatting #2353

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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
Add formatting and linting for C# files
  • Loading branch information
filmor committed Mar 31, 2024
commit 76857c01cd05042a0205eba593e4e0a90a01a1c9
30 changes: 30 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Linting

on:
push:
branches:
- master
pull_request:

jobs:
lint-dotnet:
name: Lint .NET
runs-on: ubuntu-latest
timeout-minutes: 15

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: '7.0.x'

- name: Lint
run: dotnet format -v diag --verify-no-changes --report=format.json

- uses: actions/upload-artifact@v4
with:
name: format-report
path: format.json
1 change: 1 addition & 0 deletions src/console/pythonconsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Reflection;

using Python.Runtime;

namespace Python.Runtime
Expand Down
4 changes: 3 additions & 1 deletion src/embed_tests/CodecGroups.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ namespace Python.EmbeddingTest
{
using System;
using System.Linq;

using NUnit.Framework;

using Python.Runtime;
using Python.Runtime.Codecs;

Expand All @@ -20,7 +22,7 @@ public void GetEncodersByType()
};

var got = group.GetEncoders(typeof(Uri)).ToArray();
CollectionAssert.AreEqual(new[]{encoder1, encoder2}, got);
CollectionAssert.AreEqual(new[] { encoder1, encoder2 }, got);
}

[Test]
Expand Down
19 changes: 13 additions & 6 deletions src/embed_tests/Codecs.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
namespace Python.EmbeddingTest {
namespace Python.EmbeddingTest
{
using System;
using System.Collections.Generic;
using System.Linq;

using NUnit.Framework;

using Python.Runtime;
using Python.Runtime.Codecs;

Expand Down Expand Up @@ -169,7 +172,8 @@ public void SequenceDecoderTest()
ICollection<string> stringCollection = null;
Assert.DoesNotThrow(() => { codec.TryDecode(pyList, out stringCollection); });
Assert.AreEqual(3, stringCollection.Count());
Assert.Throws(typeof(InvalidCastException), () => {
Assert.Throws(typeof(InvalidCastException), () =>
{
string[] array = new string[3];
stringCollection.CopyTo(array, 0);
});
Expand Down Expand Up @@ -206,7 +210,8 @@ public void SequenceDecoderTest()
ICollection<string> stringCollection2 = null;
Assert.DoesNotThrow(() => { codec.TryDecode(pyTuple, out stringCollection2); });
Assert.AreEqual(3, stringCollection2.Count());
Assert.Throws(typeof(InvalidCastException), () => {
Assert.Throws(typeof(InvalidCastException), () =>
{
string[] array = new string[3];
stringCollection2.CopyTo(array, 0);
});
Expand Down Expand Up @@ -255,13 +260,15 @@ public void IterableDecoderTest()
IEnumerable<string> stringEnumerable = null;
Assert.DoesNotThrow(() => { codec.TryDecode(pyList, out stringEnumerable); });

Assert.Throws(typeof(InvalidCastException), () => {
Assert.Throws(typeof(InvalidCastException), () =>
{
foreach (string item in stringEnumerable)
{
var x = item;
}
});
Assert.Throws(typeof(InvalidCastException), () => {
Assert.Throws(typeof(InvalidCastException), () =>
{
stringEnumerable.Count();
});

Expand Down Expand Up @@ -390,7 +397,7 @@ public void ExceptionDecodedNoInstance()
}
}

public static void AcceptsDateTime(DateTime v) {}
public static void AcceptsDateTime(DateTime v) { }

[Test]
public void As_Object_AffectedByDecoders()
Expand Down
1 change: 1 addition & 0 deletions src/embed_tests/GlobalTestsSetup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NUnit.Framework;

using Python.Runtime;

namespace Python.EmbeddingTest
Expand Down
6 changes: 3 additions & 3 deletions src/embed_tests/Inheritance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,9 @@ public class PropertyAccessorBase
public virtual string VirtualProp { get; set; }
}

public class PropertyAccessorIntermediate: PropertyAccessorBase { }
public class PropertyAccessorIntermediate : PropertyAccessorBase { }

public class PropertyAccessorDerived: PropertyAccessorIntermediate
public class PropertyAccessorDerived : PropertyAccessorIntermediate
{
public override string VirtualProp { set => base.VirtualProp = value.ToUpperInvariant(); }
}
Expand All @@ -217,7 +217,7 @@ public class ContainerClass
{
public void BaseMethod() { }

public class InnerClass: ContainerClass
public class InnerClass : ContainerClass
{

}
Expand Down
6 changes: 4 additions & 2 deletions src/embed_tests/Modules.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Threading;

using NUnit.Framework;

using Python.Runtime;

namespace Python.EmbeddingTest
Expand Down Expand Up @@ -385,8 +387,8 @@ public void TestThread()
//add function to the scope
//can be call many times, more efficient than ast
ps.Exec(
"import threading\n"+
"lock = threading.Lock()\n"+
"import threading\n" +
"lock = threading.Lock()\n" +
"def update():\n" +
" global res, th_cnt\n" +
" with lock:\n" +
Expand Down
2 changes: 1 addition & 1 deletion src/embed_tests/NumPyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void VarArg()
{
dynamic zX = np.array(new[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 8, 9, 0 } });
dynamic grad = np.gradient(zX, 4.0, 5.0);
dynamic grad2 = np.InvokeMethod("gradient", new PyObject[] {zX, new PyFloat(4.0), new PyFloat(5.0)});
dynamic grad2 = np.InvokeMethod("gradient", new PyObject[] { zX, new PyFloat(4.0), new PyFloat(5.0) });

Assert.AreEqual(4.125, grad[0].sum().__float__().As<double>(), 0.001);
Assert.AreEqual(-1.2, grad[1].sum().__float__().As<double>(), 0.001);
Expand Down
1 change: 1 addition & 0 deletions src/embed_tests/References.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Python.EmbeddingTest
{
using NUnit.Framework;

using Python.Runtime;

public class References
Expand Down
21 changes: 14 additions & 7 deletions src/embed_tests/TestCallbacks.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
using System;

using NUnit.Framework;

using Python.Runtime;

namespace Python.EmbeddingTest {
public class TestCallbacks {
namespace Python.EmbeddingTest
{
public class TestCallbacks
{
[OneTimeSetUp]
public void SetUp() {
public void SetUp()
{
PythonEngine.Initialize();
}

[OneTimeTearDown]
public void Dispose() {
public void Dispose()
{
PythonEngine.Shutdown();
}

[Test]
public void TestNoOverloadException() {
public void TestNoOverloadException()
{
int passed = 0;
var aFunctionThatCallsIntoPython = new Action<int>(value => passed = value);
using (Py.GIL()) {
using (Py.GIL())
{
using dynamic callWith42 = PythonEngine.Eval("lambda f: f([42])");
using var pyFunc = aFunctionThatCallsIntoPython.ToPython();
var error = Assert.Throws<PythonException>(() => callWith42(pyFunc));
var error = Assert.Throws<PythonException>(() => callWith42(pyFunc));
Assert.AreEqual("TypeError", error.Type.Name);
string expectedArgTypes = "(<class 'list'>)";
StringAssert.EndsWith(expectedArgTypes, error.Message);
Expand Down
6 changes: 3 additions & 3 deletions src/embed_tests/TestConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void TestConvertSingleToManaged(
var converted = Converter.ToManaged(pyFloat, typeof(float), out convertedValue, false);

Assert.IsTrue(converted);
Assert.IsTrue(((float) convertedValue).Equals(testValue));
Assert.IsTrue(((float)convertedValue).Equals(testValue));
}

[Test]
Expand All @@ -60,7 +60,7 @@ public void TestConvertDoubleToManaged(
var converted = Converter.ToManaged(pyFloat, typeof(double), out convertedValue, false);

Assert.IsTrue(converted);
Assert.IsTrue(((double) convertedValue).Equals(testValue));
Assert.IsTrue(((double)convertedValue).Equals(testValue));
}

[Test]
Expand Down Expand Up @@ -164,7 +164,7 @@ public void ToPyList()
[Test]
public void RawListProxy()
{
var list = new List<string> {"hello", "world"};
var list = new List<string> { "hello", "world" };
var listProxy = PyObject.FromManagedObject(list);
var clrObject = (CLRObject)ManagedType.GetManagedObject(listProxy);
Assert.AreSame(list, clrObject.inst);
Expand Down
2 changes: 2 additions & 0 deletions src/embed_tests/TestCustomMarshal.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;

using NUnit.Framework;

using Python.Runtime;

namespace Python.EmbeddingTest
Expand Down
2 changes: 2 additions & 0 deletions src/embed_tests/TestDomainReload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;

using NUnit.Framework;

using Python.Runtime;

using PyRuntime = Python.Runtime.Runtime;
Expand Down
8 changes: 5 additions & 3 deletions src/embed_tests/TestFinalizer.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using NUnit.Framework;
using Python.Runtime;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;

using NUnit.Framework;

using Python.Runtime;

namespace Python.EmbeddingTest
{
public class TestFinalizer
Expand Down Expand Up @@ -237,7 +239,7 @@ private static IntPtr CreateStringGarbage()
PyString s1 = new PyString("test_string");
// s2 steal a reference from s1
IntPtr address = s1.Reference.DangerousGetAddress();
PyString s2 = new (StolenReference.DangerousFromPointer(address));
PyString s2 = new(StolenReference.DangerousFromPointer(address));
return address;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/embed_tests/TestGILState.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Python.EmbeddingTest
{
using NUnit.Framework;

using Python.Runtime;

public class TestGILState
Expand All @@ -13,7 +14,7 @@ public void CanDisposeMultipleTimes()
{
using (var gilState = Py.GIL())
{
for(int i = 0; i < 50; i++)
for (int i = 0; i < 50; i++)
gilState.Dispose();
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/embed_tests/TestInstanceWrapping.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Globalization;

using NUnit.Framework;

using Python.Runtime;

namespace Python.EmbeddingTest
Expand Down Expand Up @@ -44,10 +46,10 @@ public void WeakRefIsNone_AfterObjectIsGone()
Assert.IsTrue(weakref.Invoke().IsNone());
}

class Base {}
class Derived: Base { }
class Base { }
class Derived : Base { }

class Overloaded: Derived
class Overloaded : Derived
{
public int Value { get; set; }
public void IntOrStr(int arg) => this.Value = arg;
Expand Down
2 changes: 2 additions & 0 deletions src/embed_tests/TestNamedArguments.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;

using NUnit.Framework;

using Python.Runtime;

namespace Python.EmbeddingTest
Expand Down
18 changes: 9 additions & 9 deletions src/embed_tests/TestOperator.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System;
using System.Linq;
using System.Reflection;

using NUnit.Framework;

using Python.Runtime;
using Python.Runtime.Codecs;

using System;
using System.Linq;
using System.Reflection;

namespace Python.EmbeddingTest
{
public class TestOperator
Expand Down Expand Up @@ -541,12 +541,12 @@ public void ForwardOperatorOverloads()
[Test]
public void TupleComparisonOperatorOverloads()
{
TupleCodec<ValueTuple>.Register();
string name = string.Format("{0}.{1}",
typeof(OperableObject).DeclaringType.Name,
typeof(OperableObject).Name);
TupleCodec<ValueTuple>.Register();
string name = string.Format("{0}.{1}",
typeof(OperableObject).DeclaringType.Name,
typeof(OperableObject).Name);
string module = MethodBase.GetCurrentMethod().DeclaringType.Namespace;
PythonEngine.Exec($@"
PythonEngine.Exec($@"
from {module} import *
cls = {name}
a = cls(2)
Expand Down
Loading