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

removes implicit default of trailing optional arguments (see #2935) #4729

Merged
merged 1 commit into from
Nov 28, 2024

Conversation

Icxolu
Copy link
Contributor

@Icxolu Icxolu commented Nov 25, 2024

This continues #2935 and makes trailing optional arguments a hard error (for this release) removes implicit default of trailing optional arguments. This also already makes Option<T> args required, removing the ambiguity for this case already:

#[pyfunction]
fn function_with_required_after_option(_opt: Option<i32>, _x: i32) {}

Closes #2935

@Icxolu Icxolu force-pushed the trailing-options branch 3 times, most recently from 635bffa to 9c728f5 Compare November 25, 2024 19:56
Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for driving this forward, looks good!

I wonder, could we get away without the hard error and just start with the new behaviour immediately? I guess it increases the chances users get silent behaviour change if they didn't notice the deprecation, so maybe it's safer to have it for 1-2 releases, even if a bit verbose... 🤔

@Icxolu
Copy link
Contributor Author

Icxolu commented Nov 26, 2024

I wonder, could we get away without the hard error and just start with the new behaviour immediately?

We probably can. I initially thought we would do 1 cycle of deprecation and then 1 cycle hard error. Now that we had 2 cycles with deprecation already, it's probably fine to just switch. (Also considering that it's not really super easy to suppress the deprecation. So the vast majority of users should have reacted already.) If you're fine with that, I'll update this and remove the error case.

@davidhewitt
Copy link
Member

Yeah, I think it's probably better to skip the hard error phase. If users update more than 2 PyO3 versions in a single hop they are walking into breaking changes without deprecations anyway. 👍

@Icxolu Icxolu changed the title makes trailing optional arguments a hard error (see #2935) removes implicit default of trailing optional arguments (see #2935) Nov 26, 2024
Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect, thanks very much 👍

python_signature.required_positional_parameters == python_signature.positional_parameters.len(),
ty.span() => "required arguments after an `Option<_>` argument are ambiguous\n\
= help: add a `#[pyo3(signature)]` annotation on this function to unambiguously specify the default values for all optional parameters"
assert_eq!(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable to leave this as an assert 👍

|| quote!(::std::option::Option::None),
|tokens| some_wrap(tokens, ctx),
));
default = default.map(|tokens| some_wrap(tokens, ctx));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that the presence of optionality doesn't potentially insert a default, I hope that one day as a follow up we might be able to move the logic for adding some wrapping from the macro into the generated code / type system.

Would be quite the magical code but it'd be more robust than having to parse Option as a name.

@davidhewitt davidhewitt added this pull request to the merge queue Nov 28, 2024
Merged via the queue into PyO3:main with commit 6abb69d Nov 28, 2024
45 of 46 checks passed
@Icxolu Icxolu deleted the trailing-options branch November 28, 2024 23:11
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.

RFC: remove default of None for trailing optional arguments
2 participants