diff --git a/CHANGES.current b/CHANGES.current index ae12d1a2c..a715b7e5d 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -7,6 +7,37 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/ Version 4.1.0 (in progress) =========================== +2022-03-02: geographika, wsfulton + [Python] #1951 Add Python variable annotations support. + + 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 + + *** POTENTIAL INCOMPATIBILITY *** + 2022-03-02: olly #891 Give error for typemap argument without a value. Previously SWIG segfaulted. @@ -23,6 +54,8 @@ Version 4.1.0 (in progress) -features python:annotations=c + Also see entry dated 2022-03-02, regarding variable annotations. + *** POTENTIAL INCOMPATIBILITY *** 2022-02-26: wsfulton diff --git a/Doc/Manual/Contents.html b/Doc/Manual/Contents.html index 56049ba6c..07cd1e723 100644 --- a/Doc/Manual/Contents.html +++ b/Doc/Manual/Contents.html @@ -1478,7 +1478,7 @@
Python function annotations are not supported. +
Python annotations are not supported.
Although the -fastproxy option results in faster code over the default, the generated proxy code is not as user-friendly -as docstring/doxygen comments, Python function annotations and functions with default values are not visible in the generated Python proxy class. +as docstring/doxygen comments, Python annotations and functions with default values are not visible in the generated Python proxy class. The -olddefs option can rectify this.
@@ -6762,13 +6762,15 @@ The following are Python 3 new features that are currently supported by SWIG. -Python 3 supports function annotations as defined in PEP 3107. -Note that there is currently no annotations support for the -builtin nor +Python 3.6 and later additionally support variable annotations as defined in +PEP 526. +Note that currently there is no annotations support in SWIG for the -builtin nor the -fastproxy option. Annotations are added via the python:annotations %feature directives. @@ -6779,7 +6781,7 @@ SWIG currently supports one type of function annotation.
-The %feature("python:annotations", "c") directive generates function annotations +The %feature("python:annotations", "c") directive generates annotations containing C/C++ types. For example:
@@ -6789,16 +6791,16 @@ int *global_ints(int &ri);-The generated code then contains function annotations containing the C types: +The generated code then contains function annotations containing the C++ types:
def global_ints(ri: "int &") -> "int *": - return _python_annotations_c.global_ints(ri) + return _example.global_ints(ri)
-There are some limitations with annotations support, for example, overloaded functions use +There are some limitations with function annotations support, for example, overloaded functions use *args or **kwargs when keyword arguments are enabled. The parameter names and types are then not shown. For example, with input:
@@ -6815,13 +6817,65 @@ Only the return type is annotated.def global_overloaded(*args) -> "int *": - return _python_annotations_c.global_overloaded(*args) + return _example.global_overloaded(*args)
+Below is an example demonstrating variable annotations. +
+ ++%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) + ... +
+Variable annotations are only supported from Python 3.6. If you need to support earlier versions of Python, you'll need to turn variable annotations off via the python:annotations:novar feature flag. +It is quite easy to support function annotations but turn off variable annotations. The next example shows how to do this for all variables. +
+ ++%feature("python:annotations", "c"); // Turn on function annotations and variable annotations globally +%feature("python:annotations:novar"); // Turn off variable annotations globally + +struct V { + float val; + void vv(float *v) const; +}; +
+The resulting code will work with versions older than Python 3.6 as the variable annotations are turned off: +
+ ++class V(object): + val = property(_example.V_val_get, _example.V_val_set) + + def vv(self, v: "float *") -> "void": + return _example.V_vv(self, v) + ... +
Compatibility Note: SWIG-4.1.0 changed the way that function annotations are generated. -Prior versions required the -py3 option which enabled function annotation support +Prior versions required the -py3 option to generate function annotation support containing C/C++ types instead of supporting %feature("python:annotations", "c"). +Variable annotations were also added in SWIG-4.1.0.