mirror of https://github.com/swig/swig
Python: Amend annotations test
This commit is contained in:
parent
3bfdf13c60
commit
55237efa72
|
@ -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))
|
||||
|
|
Loading…
Reference in New Issue