Fix optionality of function parameters & return #299
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes #295. It does this primarily by moving the decision of nullability to
TypeReflection.AsPredefinedType
, where theOptional
type hint is considered. As a result of this, theliteralExpressionSyntax
inArgumentReflection.ArgumentSyntax
also becomes a simple switch expression that's solely based on thePythonConstant
of the parameter's default value.This also fixes some tests that were incorrect previously, like:
CSnakes/src/CSnakes.Tests/GeneratedSignatureTests.cs
Line 30 in 048e011
The
b
parameter cannot have a default value ofNone
per the type hint ofstr
. The C# signature is correct, but because it was based on the presence ofNone
as the default value.MethodReflection.ProcessMethodWithReturnType
needed some bigger changes because the return type's nullability needed to be handled with some extra care.PyObject.As<T>
isn't good enough since it uses reflection based on type ofT
and isn't aware of nullability. As a result,MethodReflection.ProcessMethodWithReturnType
generates code to check forNone
and returnnull
instead when the return type isOptional
. I've added integration tests for this (seetest_optional.py
), where you can see this in action along the lines of: