fix: use subclass check for AttributeError in getattr_opt on Python < 3.13#5985
Merged
davidhewitt merged 4 commits intoPyO3:mainfrom Apr 24, 2026
Merged
Conversation
… 3.13 The `#[cfg(not(Py_3_13))]` branch of `getattr_opt` used `.is()` (type identity) to check for `AttributeError`, which missed subclasses. This replaces it with `.is_subclass_of::<PyAttributeError>()` to match the behavior of `PyObject_GetOptionalAttr` on Python 3.13+.
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Fixes getattr_opt behavior on Python < 3.13 to treat AttributeError subclasses as “attribute not found”, aligning with Python 3.13+ semantics, and adds a regression test.
Changes:
- Use subclass checking instead of type identity when determining whether to swallow an
AttributeErroringetattr_opt(Python < 3.13 path). - Add a unit test that raises an
AttributeErrorsubclass from a property and assertsgetattr_optreturnsNone. - Add a newsfragment documenting the fix.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/types/any.rs | Updates error-type checking in getattr_opt and adds a regression test for AttributeError subclasses. |
| newsfragments/5985.fixed.md | Documents the behavior fix for Python < 3.13. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Switch from `err.get_type().is_subclass_of::<PyAttributeError>()?` to `err.is_instance_of::<PyAttributeError>()` which is infallible and avoids masking the original error if the subclass check itself fails. This also matches the pattern used by `hasattr` in the same file.
davidhewitt
reviewed
Apr 18, 2026
…ions Add a compatibility shim for PyObject_GetOptionalAttr in pyo3-ffi/src/compat/py_3_13.rs that uses PyObject_GetAttr + PyErr_ExceptionMatches on Python < 3.13, matching the real C API behavior on 3.13+. Simplify getattr_opt to use the compat function unconditionally, removing the #[cfg] branching.
davidhewitt
approved these changes
Apr 23, 2026
davidhewitt
pushed a commit
to davidhewitt/pyo3
that referenced
this pull request
May 1, 2026
… 3.13 (PyO3#5985) * fix: use subclass check for AttributeError in getattr_opt on Python < 3.13 The `#[cfg(not(Py_3_13))]` branch of `getattr_opt` used `.is()` (type identity) to check for `AttributeError`, which missed subclasses. This replaces it with `.is_subclass_of::<PyAttributeError>()` to match the behavior of `PyObject_GetOptionalAttr` on Python 3.13+. * chore: rename newsfragment to PR PyO3#5985 * fix: use infallible is_instance_of instead of fallible is_subclass_of Switch from `err.get_type().is_subclass_of::<PyAttributeError>()?` to `err.is_instance_of::<PyAttributeError>()` which is infallible and avoids masking the original error if the subclass check itself fails. This also matches the pattern used by `hasattr` in the same file. * fix: add PyObject_GetOptionalAttr compat shim and use it for all versions Add a compatibility shim for PyObject_GetOptionalAttr in pyo3-ffi/src/compat/py_3_13.rs that uses PyObject_GetAttr + PyErr_ExceptionMatches on Python < 3.13, matching the real C API behavior on 3.13+. Simplify getattr_opt to use the compat function unconditionally, removing the #[cfg] branching.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
#[cfg(not(Py_3_13))]fallback path ofgetattr_optused.is()(type identity) to check whether an error wasAttributeError. This missesAttributeErrorsubclasses — they get propagated as errors instead of being treated as "attribute not found".On Python 3.13+,
PyObject_GetOptionalAttrinternally usesPyErr_ExceptionMatcheswhich does a proper subclass check, so this inconsistency only affects Python < 3.13.This PR replaces
.is()with.is_subclass_of::<PyAttributeError>()to match the 3.13+ semantics, and adds a test covering the case.Closes #5984
Discovered via pydantic/pydantic#13092 — pydantic's
from_attributes=Trueregressed on Python 3.12 after switching from a hand-rolledgetattrwrapper (which did subclass checking) to PyO3'sgetattr_opt.