Skip to content

Commit 40ee11d

Browse files
committed
FIX: fixed CheckedSession on Python3.14 (see #1147)
1 parent 27e3dbd commit 40ee11d

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

larray/core/checked.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from larray.core.axis import AxisCollection
88
from larray.core.array import Array, full
99
from larray.core.session import Session
10+
from larray.util.misc import get_annotations
1011

1112

1213
class NotLoaded:
@@ -132,11 +133,8 @@ def validate_array(value: Any, info: ValidationInfo) -> Array:
132133
class LArrayModelMetaclass(ModelMetaclass):
133134
def __new__(mcs, cls_name: str, bases: tuple[type[Any], ...],
134135
namespace: dict[str, Any], **kwargs):
135-
136-
# any type hints defined in the class body will land in
137-
# __annotations__ (this is not pydantic-specific) but
138-
# __annotations__ is only defined if there are type hints
139-
raw_annotations = namespace.get('__annotations__', {})
136+
# get user-defined annotations
137+
raw_annotations = get_annotations(namespace)
140138
type_annotations = {
141139
key: type(value)
142140
for key, value in namespace.items()

larray/util/misc.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,3 +1081,27 @@ def concatenate_ndarrays(arrays) -> np.ndarray:
10811081

10821082
def first(iterable, default=None):
10831083
return next(iter(iterable), default)
1084+
1085+
1086+
try:
1087+
# Python 3.14+
1088+
import annotationlib
1089+
1090+
def get_annotations(namespace):
1091+
# should not happen in Python3.14+ unless
1092+
# "from __future__ import annotations" is used
1093+
if "__annotations__" in namespace:
1094+
return namespace["__annotations__"]
1095+
elif annotate := annotationlib.get_annotate_from_class_namespace(namespace):
1096+
return annotationlib.call_annotate_function(
1097+
annotate, format=annotationlib.Format.FORWARDREF
1098+
)
1099+
else:
1100+
return {}
1101+
except ImportError:
1102+
# Python <3.14
1103+
def get_annotations(namespace):
1104+
# any type hints defined in the class body will land in a
1105+
# __annotations__ key in its namespace (this is not pydantic-specific)
1106+
# but __annotations__ is only defined if there are type hints
1107+
return namespace.get('__annotations__', {})

0 commit comments

Comments
 (0)