10000 Fix decimal default parameters (#1773) · pythonnet/pythonnet@7247da5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7247da5

Browse files
authored
Fix decimal default parameters (#1773)
* Drop outdated work around to fix decimal default parameters * Add test for Decimal default parameter * Update changelog
1 parent 090ff9f commit 7247da5

File tree

5 files changed

+14
-5
lines changed

5 files changed

+14
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ Instead, `PyIterable` does that.
113113
- Unicode strings with surrogates were truncated when converting from Python
114114
- `Reload` mode now supports generic methods (previously Python would stop seeing them after reload)
115115
- Temporarily fixed issue resolving method overload when method signature has `out` parameters ([#1672](i1672))
116+
- Decimal default parameters are now correctly taken into account
116117

117118
### Removed
118119

src/runtime/MethodBinder.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,11 +1072,7 @@ static internal class ParameterInfoExtensions
10721072
{
10731073
public static object? GetDefaultValue(this ParameterInfo parameterInfo)
10741074
{
1075-
// parameterInfo.HasDefaultValue is preferable but doesn't exist in .NET 4.0
1076-
bool hasDefaultValue = (parameterInfo.Attributes & ParameterAttributes.HasDefault) ==
1077-
ParameterAttributes.HasDefault;
1078-
1079-
if (hasDefaultValue)
1075+
if (parameterInfo.HasDefaultValue)
10801076
{
10811077
return parameterInfo.DefaultValue;
10821078
}

src/testing/Python.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
66
<AssemblyOriginatorKeyFile>..\pythonnet.snk</AssemblyOriginatorKeyFile>
77
<SignAssembly>true</SignAssembly>
8+
<NoWarn>IDE0051;IDE0060</NoWarn>
89
</PropertyGroup>
910
<ItemGroup>
1011
<ProjectReference Include="..\runtime\Python.Runtime.csproj" />

src/testing/methodtest.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,11 @@ public static int TestSingleDefaultParam(int i = 5)
258258
return i;
259259
}
260260

261+
public static decimal TestDecimalDefaultParam(decimal n = 1m)
262+
{
263+
return n;
264+
}
265+
261266
public static int TestTwoDefaultParam(int i = 5, int j = 6)
262267
{
263268
return i + j;

tests/test_method.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,12 @@ def test_single_default_param():
441441
assert result == 5
442442

443443

444+
def test_decimal_default_param():
445+
"""Test that decimal default parameters work."""
446+
result = MethodTest.TestDecimalDefaultParam()
447+
assert result == System.Decimal(1)
448+
449+
444450
def test_one_arg_and_two_default_param():
445451
"""Test void method with single ref-parameter."""
446452
result = MethodTest.TestOneArgAndTwoDefaultParam(11)

0 commit comments

Comments
 (0)
0