Skip to content

allow path in from_py_with and deprecate string literal#4860

Merged
Icxolu merged 1 commit intoPyO3:mainfrom
Icxolu:from-py-with
Mar 1, 2025
Merged

allow path in from_py_with and deprecate string literal#4860
Icxolu merged 1 commit intoPyO3:mainfrom
Icxolu:from-py-with

Conversation

@Icxolu
Copy link
Copy Markdown
Member

@Icxolu Icxolu commented Jan 16, 2025

This allows specifying the from_py_with function as a path rather than a string literal. The old syntax is still valid and shows a deprecation warning.

Complementary to #4850.

@Icxolu Icxolu force-pushed the from-py-with branch 3 times, most recently from 5a5e09a to 8fdecc2 Compare January 17, 2025 17:12
Copy link
Copy Markdown
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, sorry for the slow review. I should be able to reliably be more active again now.

LGTM with one small suggestion before merge...

Comment thread pyo3-macros-backend/src/frompyobject.rs Outdated
Comment on lines +285 to +290
quote_spanned! { expr_path.span() =>
#[deprecated(since = "0.24.0", note = "`from_py_with` string literals are deprecated. Use the function path instead.")]
#[allow(dead_code)]
const LIT_STR_DEPRECATION: () = ();
let _: () = LIT_STR_DEPRECATION;
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

To avoid repetition, shall we put a helper function somewhere to produce these fragments?

Also, I wonder if we should be more direct with the hint to users. E.g.

= help: use `foo` instead of `"foo"`

(we could even substitute in the user input)

or maybe

remove the quotes from the literal

Copy link
Copy Markdown
Member Author

@Icxolu Icxolu Mar 1, 2025

Choose a reason for hiding this comment

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

Good idea! I made a small helper in utils and changed the deprecation message, let me know what you think

warning: use of deprecated constant `__pyfunction_f::LIT_STR_DEPRECATION`: `from_py_with` string literals is deprecated. Use the function path instead.
--> tests/ui/invalid_argument_attributes.rs:23:28
|
23 | fn f(#[pyo3(from_py_with = "bytes_from_py")] _bytes: Vec<u8>) {}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

so e.g. maybe here it says

use `bytes_from_py` instead of `"bytes_from_py"`

Copy link
Copy Markdown
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.

Looks great to me, thanks!

@davidhewitt davidhewitt added this pull request to the merge queue Mar 1, 2025
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to a conflict with the base branch Mar 1, 2025
@Icxolu Icxolu enabled auto-merge March 1, 2025 13:57
@Icxolu Icxolu added this pull request to the merge queue Mar 1, 2025
Merged via the queue into PyO3:main with commit 31bb2f4 Mar 1, 2025
@Icxolu Icxolu deleted the from-py-with branch March 1, 2025 14:45
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.

2 participants