diff --git a/dds/_version.py b/dds/_version.py index 58dc983..8a3f41e 100644 --- a/dds/_version.py +++ b/dds/_version.py @@ -1 +1 @@ -version = "0.7.2" +version = "0.7.3" diff --git a/dds/fun_args.py b/dds/fun_args.py index fd68db6..16f149d 100644 --- a/dds/fun_args.py +++ b/dds/fun_args.py @@ -88,10 +88,11 @@ def get_arg_ctx( for (idx, (n, p_)) in enumerate(arg_sig.parameters.items()): p: inspect.Parameter = p_ # _logger.debug(f"get_arg_ctx: {f}: idx={idx} n={n} p={p}") - if p.kind != Parameter.POSITIONAL_OR_KEYWORD: + if p.kind not in (Parameter.POSITIONAL_OR_KEYWORD, Parameter.VAR_KEYWORD): raise NotImplementedError( f"Argument type not understood: {p.kind} {f} {arg_sig}" ) + h: Optional[PyHash] if idx < num_args: # It is a list argument # TODO: should it discard arguments of not-whitelisted types? @@ -110,6 +111,9 @@ def get_arg_ctx( # TODO: should it discard arguments of not-whitelisted types? # TODO: raise a warning for non-whitelisted objects h = dds_hash(p.default or "__none__") + elif p.kind == Parameter.VAR_KEYWORD: + # kwargs: for now, just ignored + h = None else: raise NotImplementedError( f"Cannot deal with argument name {n} {p.kind} {f} {arg_sig}" @@ -145,7 +149,11 @@ def process_arg(node: ast.AST) -> Optional[PyHash]: p: inspect.Parameter = p_ # _logger.debug(f"get_arg_ctx: {f}: idx={idx} n={n} p={p}") h: Optional[PyHash] - if p.kind != Parameter.POSITIONAL_OR_KEYWORD: + if p.kind not in ( + Parameter.POSITIONAL_OR_KEYWORD, + Parameter.VAR_KEYWORD, + Parameter.VAR_POSITIONAL, + ): raise NotImplementedError( f"Argument type not understood {p.kind} {f} {arg_sig}" ) diff --git a/dds_tests/test_kargs.py b/dds_tests/test_kargs.py new file mode 100644 index 0000000..7649946 --- /dev/null +++ b/dds_tests/test_kargs.py @@ -0,0 +1,49 @@ +import dds +import pytest +from .utils import cleandir + +_ = cleandir + +path_1 = "/path_1" + + +def f1(x, y, z): + return x + y + z + + +def f1_1(): + kargs = [1, 2, 3] + return dds.keep("/p1", f1, *kargs) + + +def f1_2(): + kargs = [2] + kwargs = [{"z": 3}] + return f1(1, *kargs, **kwargs[0]) + + +@pytest.mark.usefixtures("cleandir") +def test_kargs_1(): + assert dds.eval(f1_1) == 6 + assert dds.eval(f1_2) == 6 + + +def f2(*args, **kwargs): + return len(args) + len(kwargs) + + +def f2_1(): + kargs = [1, 2, 3] + return dds.keep("/p1", f2, *kargs) + + +def f2_2(): + kargs = [2] + kwargs = [{"z": 3}] + return f2(1, *kargs, **kwargs[0]) + + +@pytest.mark.usefixtures("cleandir") +def test_kargs_2(): + assert dds.eval(f2_1) == 3 + assert dds.eval(f2_2) == 3 diff --git a/doc_source/changelog.md b/doc_source/changelog.md index e14c48e..3007d2c 100644 --- a/doc_source/changelog.md +++ b/doc_source/changelog.md @@ -1,5 +1,10 @@ # Changelog +## v0.7.3 + +Fixes the usage of positional and keyworded arguments when used in conjunction +with `dds.keep`. + ## v0.7.2 Small usability fixes in this release: diff --git a/doc_source/tut_sklearn.ipynb b/doc_source/tut_sklearn.ipynb index 837289b..d56e142 100644 --- a/doc_source/tut_sklearn.ipynb +++ b/doc_source/tut_sklearn.ipynb @@ -46,7 +46,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Seting up the `dds` store:" + "Seting up the `dds` store. This is a recommended operation (here necessary to generate the documentation)." ] }, { diff --git a/doc_source/user_guide.ipynb b/doc_source/user_guide.ipynb index 40043eb..5c89fac 100644 --- a/doc_source/user_guide.ipynb +++ b/doc_source/user_guide.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2020-12-05T14:06:30.567220Z", @@ -18,7 +18,23 @@ "source": [ "%%sh\n", "# Small cleanup for reproducibility\n", - "rm -rf /tmp/dds/*" + "rm -rf /tmp/dds/user_guide" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "hide_cell" + ] + }, + "outputs": [], + "source": [ + "import dds\n", + "dds.set_store(\"local\",\n", + " data_dir=\"/tmp/dds/user_guide/data\",\n", + " internal_dir=\"/tmp/dds/user_guide/internal\")" ] }, { @@ -45,7 +61,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2020-12-05T14:06:30.589409Z", @@ -54,25 +70,7 @@ "shell.execute_reply": "2020-12-05T14:06:30.601367Z" } }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "hello_world() has been called\n" - ] - }, - { - "data": { - "text/plain": [ - "'Hello, world'" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "import dds\n", "\n", @@ -99,19 +97,11 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hello, world" - ] - } - ], + "outputs": [], "source": [ - "! cat /tmp/dds/data/hello_world" + "! cat /tmp/dds/user_guide/data/hello_world" ] }, { @@ -123,19 +113,11 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "/tmp/dds/internal/blobs/a4a7492042d58fe1746b5c006ee5e2fb5f267d213698240016b855572b0272c3\r\n" - ] - } - ], + "outputs": [], "source": [ - "! readlink /tmp/dds/data/hello_world" + "! readlink /tmp/dds/user_guide/data/hello_world" ] }, { @@ -148,7 +130,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2020-12-05T14:06:30.607547Z", @@ -157,18 +139,7 @@ "shell.execute_reply": "2020-12-05T14:06:30.658574Z" } }, - "outputs": [ - { - "data": { - "text/plain": [ - "'Hello, world'" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "hello_world()" ] @@ -182,7 +153,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2020-12-05T14:06:30.666678Z", @@ -191,18 +162,7 @@ "shell.execute_reply": "2020-12-05T14:06:30.692795Z" } }, - "outputs": [ - { - "data": { - "text/plain": [ - "'Hello, world'" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "@dds.data_function(\"/hello_world\")\n", "def hello_world():\n", @@ -222,7 +182,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2020-12-05T14:06:30.700519Z", @@ -231,25 +191,7 @@ "shell.execute_reply": "2020-12-05T14:06:30.729773Z" } }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Calling f\n" - ] - }, - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "my_var = 1\n", "\n", @@ -270,7 +212,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2020-12-05T14:06:30.739381Z", @@ -279,18 +221,7 @@ "shell.execute_reply": "2020-12-05T14:06:30.771507Z" } }, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "f()" ] @@ -304,7 +235,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2020-12-05T14:06:30.782868Z", @@ -313,25 +244,7 @@ "shell.execute_reply": "2020-12-05T14:06:30.819630Z" } }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Calling f\n" - ] - }, - { - "data": { - "text/plain": [ - "2" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "my_var = 2\n", "f()" @@ -346,7 +259,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2020-12-05T14:06:30.830594Z", @@ -356,18 +269,7 @@ }, "scrolled": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "my_var = 1\n", "f()" @@ -391,27 +293,9 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Calling function hello on world\n" - ] - }, - { - "data": { - "text/plain": [ - "'Hello, world'" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "def hello(name):\n", " print(f\"Calling function hello on {name}\")\n", @@ -430,29 +314,11 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "metadata": { "scrolled": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Calling function hello on world\n" - ] - }, - { - "data": { - "text/plain": [ - "'Hello, world'" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "greeting = dds.keep(\"/greeting\", hello, \"world\")\n", "greeting" @@ -469,27 +335,9 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Calling hello_from_file\n" - ] - }, - { - "data": { - "text/plain": [ - "'Hello, world'" - ] - }, - "execution_count": 13, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "def hello_from_file(file):\n", " name = file.readline().strip()\n", @@ -502,7 +350,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -519,28 +367,9 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Opening file <_io.TextIOWrapper name='input.txt' mode='r' encoding='UTF-8'>\n", - "Calling hello_from_file\n" - ] - }, - { - "data": { - "text/plain": [ - "'Hello, world'" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "def wrapper_hello():\n", " f = open(\"input.txt\", \"r\")\n", @@ -565,27 +394,9 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Opening file <_io.TextIOWrapper name='input.txt' mode='r' encoding='UTF-8'>\n" - ] - }, - { - "data": { - "text/plain": [ - "'Hello, world'" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "dds.eval(wrapper_hello)" ] @@ -606,20 +417,9 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Hello, world'" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "dds.load(\"/hello_world\")" ] @@ -640,7 +440,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -668,7 +468,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -696,24 +496,11 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "03b90b2750b7b2fc1959ec068df4610e231a2b5725ea6f189846ee50dff6ebdc\r\n", - "0e701a6548c227bee7c27f2ac6e04bff0e7facfd3e93b4f4317c55994420bda7\r\n", - "9f6a010e46f75b07154ef9c413e1547b9a4f3e5e27a6913b871ff3b76497fff7\r\n", - "a4a7492042d58fe1746b5c006ee5e2fb5f267d213698240016b855572b0272c3\r\n", - "e5eb71c4dc7ee3ba89b66fd6e46f47a663f938ae78b9b854d863cd2094f4f87b\r\n", - "fd58899678abe5b67dd3aed4855417652ed70a8b5aba0d449befb9f3e6ee9d4a\r\n" - ] - } - ], + "outputs": [], "source": [ - "! ls /tmp/dds/internal/blobs | grep -v meta" + "! ls /tmp/dds/user_guide/internal/blobs | grep -v meta" ] } ], diff --git a/docs/changelog/changelog.md b/docs/changelog/changelog.md index e14c48e..3007d2c 100644 --- a/docs/changelog/changelog.md +++ b/docs/changelog/changelog.md @@ -1,5 +1,10 @@ # Changelog +## v0.7.3 + +Fixes the usage of positional and keyworded arguments when used in conjunction +with `dds.keep`. + ## v0.7.2 Small usability fixes in this release: diff --git a/docs/changelog/index.html b/docs/changelog/index.html index 1472497..a3f9242 100644 --- a/docs/changelog/index.html +++ b/docs/changelog/index.html @@ -87,6 +87,8 @@