Skip to content

Commit

Permalink
enh: support passing custom default arguments to get_class_method_info
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmueller committed Jun 7, 2024
1 parent 8020822 commit dd0553d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
0.23.1
- enh: support passing custom default arguments to get_class_method_info
0.23.0
- feat: implement segmentation using PyTorch models
- fix: always compute image_bg if it is not in the input file
Expand Down
18 changes: 16 additions & 2 deletions src/dcnum/meta/ppid.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ def convert_to_dtype(value, dtype):


def get_class_method_info(class_obj: ClassWithPPIDCapabilities,
static_kw_methods: List = None):
static_kw_methods: List = None,
static_kw_defaults: Dict = None,
):
"""Return dictionary of class info with static keyword methods docs
Parameters
Expand All @@ -69,7 +71,16 @@ def get_class_method_info(class_obj: ClassWithPPIDCapabilities,
static_kw_methods: list of callable
The methods to inspect; all kwargs-only keyword arguments
are extracted.
static_kw_defaults: dict
If a key in this dictionary matches an item in `static_kw_methods`,
then these are the default values returned in the "defaults"
dictionary. This is used in cases where a base class does
implement some annotations, but the subclass does not actually
use them, because e.g. they are taken from a property such as is
the case for the mask postprocessing of segmenter classes.
"""
if static_kw_defaults is None:
static_kw_defaults = {}
doc = class_obj.__doc__ or class_obj.__init__.__doc__
info = {
"code": class_obj.get_ppid_code(),
Expand All @@ -82,7 +93,10 @@ def get_class_method_info(class_obj: ClassWithPPIDCapabilities,
for mm in static_kw_methods:
meth = getattr(class_obj, mm)
spec = inspect.getfullargspec(meth)
defau[mm] = spec.kwonlydefaults or {}
if mm_defaults := static_kw_defaults.get(mm):
defau[mm] = mm_defaults
else:
defau[mm] = spec.kwonlydefaults or {}
annot[mm] = spec.annotations
info["defaults"] = defau
info["annotations"] = annot
Expand Down

0 comments on commit dd0553d

Please sign in to comment.