Skip to content
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

Fix optionality of function parameters & return #299

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

atifaziz
Copy link
Contributor

This PR fixes #295. It does this primarily by moving the decision of nullability to TypeReflection.AsPredefinedType, where the Optional type hint is considered. As a result of this, the literalExpressionSyntax in ArgumentReflection.ArgumentSyntax also becomes a simple switch expression that's solely based on the PythonConstant of the parameter's default value.

This also fixes some tests that were incorrect previously, like:

[InlineData("def hello_world(a: bool = True, b: str = None) -> bool: \n ...\n", "bool HelloWorld(bool a = true, string? b = null)")]

The b parameter cannot have a default value of None per the type hint of str. The C# signature is correct, but because it was based on the presence of None 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 of T and isn't aware of nullability. As a result, MethodReflection.ProcessMethodWithReturnType generates code to check for None and return null instead when the return type is Optional. I've added integration tests for this (see test_optional.py), where you can see this in action along the lines of:

public long? TestInt(long? n)
{
    using (GIL.Acquire())
    {
        // ...
        return __result_pyObject.IsNone() ? null : __result_pyObject.As<long>();
    }
}

@tonybaloney
Copy link
Owner

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

@atifaziz
Copy link
Contributor Author

atifaziz commented Nov 8, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Optional args/return either doesn't compile or throws at runtime
2 participants