Skip to content

emit deprecation on pyclasses that are affected by the FromPyObject blanket impl#5550

Merged
davidhewitt merged 1 commit intoPyO3:mainfrom
Icxolu:deprecate-frompyobject-blanket
Oct 26, 2025
Merged

emit deprecation on pyclasses that are affected by the FromPyObject blanket impl#5550
davidhewitt merged 1 commit intoPyO3:mainfrom
Icxolu:deprecate-frompyobject-blanket

Conversation

@Icxolu
Copy link
Copy Markdown
Member

@Icxolu Icxolu commented Oct 22, 2025

This emits a deprecation warning on #[pyclass]es that are affected by the FromPyObject + Clone blanket impl. It uses a Probe to detect the Clone impl and conditionally emits the deprecation. This ensures only types are flagged that are actually affected by the change. The deprecation can be suppressed by specifying either the skip_from_py_object or from_py_object option.

With this in 0.28, I think we can remove the blanket impl in 0.29.

#5419

@Icxolu Icxolu force-pushed the deprecate-frompyobject-blanket branch from 36cd3f7 to 762ef5f Compare October 22, 2025 16:58
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, generally looks great to me, nice touch to only emit it on types which are actually Clone!

I think also worth noting in the migration guide?

Comment thread tests/ui/invalid_pyclass_args.stderr Outdated
|
= note: available fields are: `0`, `1`, `2`

warning: use of deprecated associated constant `pyo3::impl_::deprecated::DeprecatedFromPyObjectBlanket::<true>::MSG`: Implicit by value extraction of pyclasses is deprecated. Use `from_py_object` to keep the current behaviour or `skip_from_py_object` to opt-out.
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.

I feel like it would be helpful here to make the hint a bit clearer that this is applicable to classes which implement `Clone.

Maybe something like the following (note I renamed the struct too given it shows in the public error message):

Suggested change
warning: use of deprecated associated constant `pyo3::impl_::deprecated::DeprecatedFromPyObjectBlanket::<true>::MSG`: Implicit by value extraction of pyclasses is deprecated. Use `from_py_object` to keep the current behaviour or `skip_from_py_object` to opt-out.
warning: use of deprecated associated constant `pyo3::impl_::deprecated::HasAutomaticFromPyObject::<true>::MSG`: The automatically derived `FromPyObject` implementation for `#[pyclass]` types which implement `Clone` is being phased out. Use `from_py_object` to keep the automatic derive or `skip_from_py_object` to accept the new behaviour.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Sounds good to me!

@Icxolu Icxolu force-pushed the deprecate-frompyobject-blanket branch from 762ef5f to 3e0c463 Compare October 26, 2025 21:35
@davidhewitt davidhewitt enabled auto-merge October 26, 2025 22:17
@davidhewitt davidhewitt added this pull request to the merge queue Oct 26, 2025
Merged via the queue into PyO3:main with commit c7c3c53 Oct 26, 2025
41 of 42 checks passed
@Icxolu Icxolu deleted the deprecate-frompyobject-blanket branch October 27, 2025 17:31
@matanox
Copy link
Copy Markdown

matanox commented Mar 6, 2026

Just asking this in user context. Is it the case that when the self is clone:

  • a FromPyObject was previously always applying via the blanket implementation?
  • a FromPyObject is necessasry for implementing Clone for a pyo3 class? A short comment about why would be just a nice to have.

Otherwise the deprecation warnings seem very clear right now, as a newcomer to this topic just upgrading to the latest release.

But can become a bit clearer if the docs do make it a bit crisper about what you lose in user code when the self is Clone and you opt for a #[pyclass(skip_from_py_object)].

Or just actually, commenting on how are Cloning and converting from python coupled.

I'll be reading https://github.com/PyO3/pyo3/blob/main/guide/src/conversions/traits.md in parallel.

@Tpt
Copy link
Copy Markdown
Contributor

Tpt commented Mar 6, 2026

@matanox

a FromPyObject was previously always applying via the blanket implementation?

Yes!

a FromPyObject is necessasry for implementing Clone for a pyo3 class? A short comment about why would be just a nice to have.

No, it's the other direction. To implement FromPyObject you need to be Clone.

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.

4 participants