From dd0553d8fcd039d7071a7c0fe25013a9f5c2d294 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20M=C3=BCller?= Date: Fri, 7 Jun 2024 17:36:51 +0200 Subject: [PATCH] enh: support passing custom default arguments to get_class_method_info --- CHANGELOG | 2 ++ src/dcnum/meta/ppid.py | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index b44f9f9..b54a7b4 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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 diff --git a/src/dcnum/meta/ppid.py b/src/dcnum/meta/ppid.py index bc07081..0e8102e 100644 --- a/src/dcnum/meta/ppid.py +++ b/src/dcnum/meta/ppid.py @@ -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 @@ -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(), @@ -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