-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Process functions: Infer argument
valid_type
from type hints
An advantage of process functions over calculation jobs and work chains are that they are light weight and very easy to start using. But there are also disadvantages; due to the process specification being inferred from the function signature, the user cannot benefit from all the features of the process specification. For example, it is not possible to explicitly define a help string or valid type for the arguments of the process function. With PEP 484 type hints were introduced in Python 3.5 that made it possible to indicate the expected types for function arguments. Here we make use of this functionality to infer the valid type of function arguments based on the provided type hint, if any. As an example, a user can now define the following calcfunction: @calcfunction def add(a: int, b: int): return a + b and when it is called with anything other than an `int` or `Int` for either of the two arguments, a validation error is raised reporting that an argument with an invalid type was provided. The new functionality is fully optional and process functions without typing will continue to work as before. If incorrect typing is provided, a warning is logged and the typing is ignored. The annotation of the wrapped process function is parsed using the `inspect.get_annotation` method. This was added in Python 3.10 and so to provide support in older versions we install the `get-annotations` backport package. Since we have to use `eval_str=True` in the call to get unstringized versions of the types. This will fail for the backport implementation if the type uses union syntax of PEP 604, e.g `str | int` instead of `typing.Union[str, int]`, even if this functionality is enabled using `from __future__ import annotations`.
- Loading branch information
Showing
13 changed files
with
295 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
docs/source/topics/processes/include/snippets/functions/typing_call_raise.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# -*- coding: utf-8 -*- | ||
from aiida.engine import calcfunction | ||
from aiida.orm import Float, Int | ||
|
||
|
||
@calcfunction | ||
def add(x: Int, y: Int): | ||
return x + y | ||
|
||
add(Int(1), Float(1.0)) |
16 changes: 16 additions & 0 deletions
16
docs/source/topics/processes/include/snippets/functions/typing_none.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# -*- coding: utf-8 -*- | ||
import typing as t | ||
|
||
from aiida.engine import calcfunction | ||
from aiida.orm import Int | ||
|
||
|
||
@calcfunction | ||
def add_multiply(x: Int, y: Int, z: typing.Optional[Int] = None): | ||
if z is None: | ||
z = Int(3) | ||
|
||
return (x + y) * z | ||
|
||
result = add_multiply(Int(1), Int(2)) | ||
result = add_multiply(Int(1), Int(2), Int(3)) |
11 changes: 11 additions & 0 deletions
11
docs/source/topics/processes/include/snippets/functions/typing_pep_563.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import annotations | ||
|
||
from aiida.engine import calcfunction | ||
|
||
|
||
@calcfunction | ||
def add(x: int, y: int): | ||
return x + y | ||
|
||
add(1, 2) |
16 changes: 16 additions & 0 deletions
16
docs/source/topics/processes/include/snippets/functions/typing_pep_604.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import annotations | ||
|
||
from aiida.engine import calcfunction | ||
from aiida.orm import Int | ||
|
||
|
||
@calcfunction | ||
def add_multiply(x: int, y: int, z: int | None = None): | ||
if z is None: | ||
z = Int(3) | ||
|
||
return (x + y) * z | ||
|
||
result = add_multiply(1, 2) | ||
result = add_multiply(1, 2, 3) |
12 changes: 12 additions & 0 deletions
12
docs/source/topics/processes/include/snippets/functions/typing_union.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# -*- coding: utf-8 -*- | ||
import typing as t | ||
|
||
from aiida.engine import calcfunction | ||
from aiida.orm import Float, Int | ||
|
||
|
||
@calcfunction | ||
def add(x: t.Union[Int, Float], y: t.Union[Int, Float]): | ||
return x + y | ||
|
||
add(Int(1), Float(1.0)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.