Repository: pythonnet/pythonnet (language: python)

Issue:
decimal default parameter not being taken into account
### Environment

-   Pythonnet version: 3.0.0a2
-   Python version: 3.10.4
-   Operating System: Windows
-   .NET Runtime: 5.0.1

### Details

-   use a method with default decimal parameter from python. I created a method with optional parameters, one of which is a decimal parameter "decimal sampling_time = 1", then in python I tried using this method with the default parameter, however, the value of the default parameter was 0 instead of the 1 that I set.
Note: the other default parameters worked fine (uint, and int parameters)


Discussion / hints:
I can reproduce this, will have a look.

Fix patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1e706b866..766258c5d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -113,6 +113,7 @@ Instead, `PyIterable` does that.
 -   Unicode strings with surrogates were truncated when converting from Python
 -   `Reload` mode now supports generic methods (previously Python would stop seeing them after reload)
 -   Temporarily fixed issue resolving method overload when method signature has `out` parameters ([#1672](i1672))
+-   Decimal default parameters are now correctly taken into account
 
 ### Removed
 
diff --git a/src/runtime/MethodBinder.cs b/src/runtime/MethodBinder.cs
index 8b9ee9c00..be4e8d0e5 100644
--- a/src/runtime/MethodBinder.cs
+++ b/src/runtime/MethodBinder.cs
@@ -1072,11 +1072,7 @@ static internal class ParameterInfoExtensions
     {
         public static object? GetDefaultValue(this ParameterInfo parameterInfo)
         {
-            // parameterInfo.HasDefaultValue is preferable but doesn't exist in .NET 4.0
-            bool hasDefaultValue = (parameterInfo.Attributes & ParameterAttributes.HasDefault) ==
-                ParameterAttributes.HasDefault;
-
-            if (hasDefaultValue)
+            if (parameterInfo.HasDefaultValue)
             {
                 return parameterInfo.DefaultValue;
             }


Test patch:
diff --git a/src/testing/Python.Test.csproj b/src/testing/Python.Test.csproj
index 1f40f4518..3adc5c0c6 100644
--- a/src/testing/Python.Test.csproj
+++ b/src/testing/Python.Test.csproj
@@ -5,6 +5,7 @@
     <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
     <AssemblyOriginatorKeyFile>..\pythonnet.snk</AssemblyOriginatorKeyFile>
     <SignAssembly>true</SignAssembly>
+    <NoWarn>IDE0051;IDE0060</NoWarn>
   </PropertyGroup>
   <ItemGroup>
     <ProjectReference Include="..\runtime\Python.Runtime.csproj" />
diff --git a/src/testing/methodtest.cs b/src/testing/methodtest.cs
index fe49de88d..ec05fef72 100644
--- a/src/testing/methodtest.cs
+++ b/src/testing/methodtest.cs
@@ -258,6 +258,11 @@ public static int TestSingleDefaultParam(int i = 5)
             return i;
         }
 
+        public static decimal TestDecimalDefaultParam(decimal n = 1m)
+        {
+            return n;
+        }
+
         public static int TestTwoDefaultParam(int i = 5, int j = 6)
         {
             return i + j;
diff --git a/tests/test_method.py b/tests/test_method.py
index e2d8d5b06..b24b525aa 100644
--- a/tests/test_method.py
+++ b/tests/test_method.py
@@ -441,6 +441,12 @@ def test_single_default_param():
     assert result == 5
 
 
+def test_decimal_default_param():
+    """Test that decimal default parameters work."""
+    result = MethodTest.TestDecimalDefaultParam()
+    assert result == System.Decimal(1)
+
+
 def test_one_arg_and_two_default_param():
     """Test void method with single ref-parameter."""
     result = MethodTest.TestOneArgAndTwoDefaultParam(11)