Add support for Python variable annotations as a feature.
Both function annotations and variable annotations are turned on using the
"python:annotations" feature. Example:
%feature("python:annotations", "c");
struct V {
float val;
};
The generated code contains a variable annotation containing the C float type:
class V(object):
val: "float" = property(_example.V_val_get, _example.V_val_set)
...
Python 3.5 and earlier do not support variable annotations, so variable
annotations can be turned off with a "python:annotations:novar" feature flag.
Example turning on function annotations but not variable annotations globally:
%feature("python:annotations", "c");
%feature("python:annotations:novar");
or via the command line:
-features python:annotations=c,python:annotations:novar
Closes #1951
This commit is contained in:
parent
0ba26d8f73
commit
3159de3e9f
7 changed files with 182 additions and 24 deletions
|
|
@ -0,0 +1,24 @@
|
|||
import sys
|
||||
|
||||
# Variable annotations for properties is only supported in python-3.6 and later (PEP 526)
|
||||
if sys.version_info[0:2] >= (3, 6):
|
||||
from python_annotations_variable_c import *
|
||||
|
||||
# No SWIG __annotations__ support with -builtin or -fastproxy
|
||||
annotations_supported = not(is_python_builtin() or is_python_fastproxy())
|
||||
|
||||
if annotations_supported:
|
||||
ts = TemplateShort()
|
||||
anno = ts.__annotations__
|
||||
if anno != {'member_variable': 'int'}:
|
||||
raise RuntimeError("annotations mismatch: {}".format(anno))
|
||||
|
||||
ts = StructWithVar()
|
||||
anno = ts.__annotations__
|
||||
if anno != {'member_variable': 'int'}:
|
||||
raise RuntimeError("annotations mismatch: {}".format(anno))
|
||||
|
||||
ts = StructWithVarNotAnnotated()
|
||||
if getattr(ts, "__annotations__", None) != None:
|
||||
anno = ts.__annotations__
|
||||
raise RuntimeError("annotations mismatch: {}".format(anno))
|
||||
Loading…
Add table
Add a link
Reference in a new issue