Python: Amend annotations test

This commit is contained in:
Julien Schueller 2025-04-10 17:47:59 +02:00
parent 3bfdf13c60
commit 55237efa72
1 changed files with 17 additions and 7 deletions

View File

@ -1,4 +1,17 @@
import sys
import inspect
def get_annotations(cls):
# Python >=3.14 removed the __annotations__ attribute
# retrieve it via inspect (see also annotationlib)
if hasattr(inspect, "get_annotations"):
# Python >=3.10
return inspect.get_annotations(cls)
else:
# Python <3.10
return getattr(cls, "__annotations__", {})
# Variable annotations for properties is only supported in python-3.6 and later (PEP 526)
if sys.version_info[0:2] >= (3, 6):
@ -8,17 +21,14 @@ if sys.version_info[0:2] >= (3, 6):
annotations_supported = not(is_python_builtin() or is_python_fastproxy())
if annotations_supported:
ts = TemplateShort()
anno = ts.__annotations__
anno = get_annotations(TemplateShort)
if anno != {'member_variable': 'int'}:
raise RuntimeError("annotations mismatch: {}".format(anno))
ts = StructWithVar()
anno = ts.__annotations__
anno = get_annotations(StructWithVar)
if anno != {'member_variable': 'int'}:
raise RuntimeError("annotations mismatch: {}".format(anno))
ts = StructWithVarNotAnnotated()
if getattr(ts, "__annotations__", None) != None:
anno = ts.__annotations__
anno = get_annotations(StructWithVarNotAnnotated)
if anno != {}:
raise RuntimeError("annotations mismatch: {}".format(anno))