Skip to content

Commit 8031427

Browse files
committed
fix: Python 3.14 may not create the annotations dict
Signed-off-by: Henry Schreiner <[email protected]>
1 parent c5699a8 commit 8031427

File tree

2 files changed

+3
-5
lines changed

2 files changed

+3
-5
lines changed

include/pybind11/pytypes.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2583,7 +2583,8 @@ str_attr_accessor object_api<D>::doc() const {
25832583

25842584
template <typename D>
25852585
object object_api<D>::annotations() const {
2586-
#if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION <= 9
2586+
#if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION <= 9 \
2587+
|| PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 14
25872588
// https://docs.python.org/3/howto/annotations.html#accessing-the-annotations-dict-of-an-object-in-python-3-9-and-older
25882589
if (!hasattr(derived(), "__annotations__")) {
25892590
setattr(derived(), "__annotations__", dict());

tests/test_pytypes.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,10 +1146,7 @@ def get_annotations_helper(o):
11461146
if sys.version_info >= (3, 14):
11471147
import annotationlib
11481148

1149-
try:
1150-
return annotationlib.get_annotations(o) or None
1151-
except TypeError:
1152-
return None
1149+
return annotationlib.get_annotations(o) or None
11531150
if isinstance(o, type):
11541151
return o.__dict__.get("__annotations__", None)
11551152
return getattr(o, "__annotations__", None)

0 commit comments

Comments
 (0)