Open atifaziz opened 1 month ago
The b parameter cannot have a default value of None per the type hint of str.
I added that test because type hints are just hints. Python developers often (although it is wrong) use default of None
with a parameter that is not marked as optional either via Optional[str]
or Union[str, None]
or str | None
type hints are just hints
I understand, but they're fairly critical for the source generator to produce sensible wrappers.
Python developers often (although it is wrong) use default of
None
with a parameter that is
Doesn't that leave the door open for other forms of type hint sloppiness? It could be the start of a slippery slope. Where do you draw the line?
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:
https://github.com/tonybaloney/CSnakes/blob/048e011b5d70849edb1d0c26fa9b985f8a48f6dc/src/CSnakes.Tests/GeneratedSignatureTests.cs#L30
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: