From c492ac42fee1c17f46501d93305a637649d5a6bf Mon Sep 17 00:00:00 2001 From: Martin Durant Date: Wed, 19 Jun 2024 09:29:34 -0400 Subject: [PATCH 1/4] Respect inverse in scalar binop --- src/dask_awkward/lib/core.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/dask_awkward/lib/core.py b/src/dask_awkward/lib/core.py index 204ccc3f..e52e43af 100644 --- a/src/dask_awkward/lib/core.py +++ b/src/dask_awkward/lib/core.py @@ -502,9 +502,17 @@ def f(self, other): if is_dask_collection(other): task = (op, self.key, *other.__dask_keys__()) deps.append(other) - plns.append(other.name) + if inv: + plns.insert(0, other.name) + else: + plns.append(other.name) else: - task = (op, self.key, other) + if inv: + task = (op, other, self.key) + else: + task = (op, self.key, other) + if inv: + plns.reverse() graph = HighLevelGraph.from_collections( name, layer=AwkwardMaterializedLayer( @@ -514,10 +522,16 @@ def f(self, other): ), dependencies=tuple(deps), ) - if isinstance(other, Scalar): - meta = op(self._meta, other._meta) + if isinstance(other, (Scalar, Array)): + if inv: + meta = op(other._meta, self._meta) + else: + meta = op(self._meta, other._meta) else: - meta = op(self._meta, other) + if inv: + meta = op(other, self._meta) + else: + meta = op(self._meta, other) return new_scalar_object(graph, name, meta=meta) return f From 46b0a9f2bdeea7d4fc3c789ee753c2128fb6ea70 Mon Sep 17 00:00:00 2001 From: Martin Durant Date: Wed, 19 Jun 2024 09:34:30 -0400 Subject: [PATCH 2/4] Add test --- tests/test_core.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/tests/test_core.py b/tests/test_core.py index 369cdbb4..8dd82b9f 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -115,7 +115,7 @@ def test_len(ndjson_points_file: str) -> None: assert len(daa) == 10 daa.eager_compute_divisions() assert daa.known_divisions - assert len(daa) == 10 # type: ignore + assert len(daa) == 10 def test_meta_exists(daa: Array) -> None: @@ -157,7 +157,7 @@ def test_partitions_divisions(ndjson_points_file: str) -> None: assert not t1.known_divisions t2 = daa.partitions[1] assert t2.known_divisions - assert t2.divisions == (0, divs[2] - divs[1]) # type: ignore + assert t2.divisions == (0, divs[2] - divs[1]) def test_array_rebuild(ndjson_points_file: str) -> None: @@ -537,7 +537,7 @@ def test_compatible_partitions_after_slice() -> None: assert_eq(lazy, ccrt) # sanity - assert dak.compatible_partitions(lazy, lazy + 2) # type: ignore + assert dak.compatible_partitions(lazy, lazy + 2) assert dak.compatible_partitions(lazy, dak.num(lazy, axis=1) > 2) assert not dak.compatible_partitions(lazy[:-2], lazy) @@ -646,6 +646,14 @@ def test_scalar_divisions(daa: Array) -> None: assert s.divisions == (None, None) +def test_scalar_binop_inv() -> None: + # GH #515 + x = dak.from_lists([[1]]) + y = x[0] # scalar + assert (0 - y) == -1 + assert (y - 0) == 1 + + def test_array_persist(daa: Array) -> None: daa2 = daa["points"]["x"].persist() assert_eq(daa["points"]["x"], daa2) @@ -886,7 +894,7 @@ def test_shape_only_ops(fn: Callable, tmp_path_factory: pytest.TempPathFactory) p = tmp_path_factory.mktemp("zeros-like-flat") ak.to_parquet(a, str(p / "file.parquet")) lazy = dak.from_parquet(str(p)) - result = fn(lazy.b) # type: ignore + result = fn(lazy.b) with dask.config.set({"awkward.optimization.enabled": True}): result.compute() @@ -898,7 +906,7 @@ def test_assign_behavior() -> None: with pytest.raises( TypeError, match="'mappingproxy' object does not support item assignment" ): - dx.behavior["should_fail"] = None # type: ignore + dx.behavior["should_fail"] = None assert dx.behavior == behavior @@ -909,7 +917,7 @@ def test_assign_attrs() -> None: with pytest.raises( TypeError, match="'mappingproxy' object does not support item assignment" ): - dx.attrs["should_fail"] = None # type: ignore + dx.attrs["should_fail"] = None assert dx.attrs == attrs From ca75020a9fddae210bde5422bf6e6d4e5b426dda Mon Sep 17 00:00:00 2001 From: Martin Durant Date: Wed, 19 Jun 2024 09:44:04 -0400 Subject: [PATCH 3/4] no mypy for test files --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 56ab6f1e..7527ba14 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -111,7 +111,7 @@ src_paths = ["src", "tests"] [tool.mypy] python_version = "3.9" -files = ["src", "tests"] +files = ["src"] strict = false warn_unused_configs = true show_error_codes = true From 442d44136a9f56a05197f628c0ff22fac3c96685 Mon Sep 17 00:00:00 2001 From: Martin Durant Date: Wed, 19 Jun 2024 10:22:45 -0400 Subject: [PATCH 4/4] Don't mypy tests (again) --- .pre-commit-config.yaml | 1 + pyproject.toml | 1 + 2 files changed, 2 insertions(+) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4c06f6eb..226b54f1 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -62,6 +62,7 @@ repos: rev: v1.10.0 hooks: - id: mypy + files: "src/" args: [--ignore-missing-imports] additional_dependencies: - dask diff --git a/pyproject.toml b/pyproject.toml index 7527ba14..4bbafc23 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -112,6 +112,7 @@ src_paths = ["src", "tests"] [tool.mypy] python_version = "3.9" files = ["src"] +exclude = ["tests/"] strict = false warn_unused_configs = true show_error_codes = true