Skip to content

Fix decimal default parameters #1773

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

Merged
merged 3 commits into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 1 addition & 5 deletions src/runtime/MethodBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/testing/Python.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
5 changes: 5 additions & 0 deletions src/testing/methodtest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions tests/test_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down