Skip to content

Commit

Permalink
Merge pull request #122 from neutrinoceros/support_metadata_in_userde…
Browse files Browse the repository at this point in the history
…f_dep
  • Loading branch information
neutrinoceros authored Jun 11, 2023
2 parents 1cbb243 + 6345b8a commit 87d88f2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "gpgi"
version = "0.12.0"
version = "0.13.0"
description = "A Generic Particle+Grid Interface"
authors = [
{ name = "C.M.T. Robert" },
Expand Down
2 changes: 1 addition & 1 deletion src/gpgi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

from .api import load

__version__ = "0.12.0"
__version__ = "0.13.0"
10 changes: 8 additions & 2 deletions src/gpgi/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import warnings
from abc import ABC, abstractmethod
from copy import deepcopy
from functools import cached_property, reduce
from functools import cached_property, partial, reduce
from itertools import chain
from time import monotonic_ns
from typing import TYPE_CHECKING, Any, Dict, Literal, Protocol, Tuple, cast
Expand Down Expand Up @@ -558,7 +558,13 @@ def deposit(
method = "ngp"

if callable(method):
func = method
from inspect import signature

sig = signature(method)
if "metadata" in sig.parameters:
func = cast(DepositionMethodT, partial(method, metadata=self.metadata))
else:
func = method
else:
if method not in _deposition_method_names:
raise ValueError(
Expand Down
15 changes: 15 additions & 0 deletions tests/test_deposit.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,21 @@ def test_callable_method(sample_2D_dataset):
sample_2D_dataset.deposit("mass", method=_deposit_ngp_2D)


def test_callable_method_with_metadata(sample_2D_dataset):
_is_used = False
_md_is_received = False

def fake_dep(*args, metadata=None, **kwargs):
nonlocal _is_used, _md_is_received
_is_used = True
_md_is_received = metadata is not None
return _deposit_ngp_2D(*args, **kwargs)

sample_2D_dataset.deposit("mass", method=fake_dep)
assert _is_used
assert _md_is_received


@pytest.mark.parametrize("method", ["ngp", "cic", "tsc"])
@pytest.mark.mpl_image_compare
def test_2D_deposit(sample_2D_dataset, method):
Expand Down

0 comments on commit 87d88f2

Please sign in to comment.