Skip to content

Commit e3401a3

Browse files
authored
Merge pull request #519 from minrk/cluster-parent-config
Nicer inspection of Cluster objects
2 parents 83e37b4 + 5e795c9 commit e3401a3

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

ipyparallel/cluster/cluster.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
from ..util import _all_profile_dirs
4040
from ..util import _default_profile_dir
4141
from ..util import _locate_profiles
42+
from ..util import _traitlet_signature
4243
from ..util import abbreviate_profile_dir
4344

4445
_suffix_chars = string.ascii_lowercase + string.digits
@@ -61,6 +62,7 @@ def _atexit_cleanup_clusters(*args):
6162
_atexit_cleanup_clusters.registered = False
6263

6364

65+
@_traitlet_signature
6466
class Cluster(AsyncFirst, LoggingConfigurable):
6567
"""Class representing an IPP cluster
6668

ipyparallel/util.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from types import FunctionType
2424

2525
import tqdm
26+
import traitlets
2627
import zmq
2728
from dateutil.parser import parse as dateutil_parse
2829
from dateutil.tz import tzlocal
@@ -720,3 +721,63 @@ def _locate_profiles(profiles=None):
720721
def shlex_join(cmd):
721722
"""Backport shlex.join to Python < 3.8"""
722723
return ' '.join(shlex.quote(s) for s in cmd)
724+
725+
726+
_traitlet_annotations = {
727+
traitlets.Bool: bool,
728+
traitlets.Integer: int,
729+
traitlets.Float: float,
730+
traitlets.List: list,
731+
traitlets.Dict: dict,
732+
traitlets.Set: set,
733+
traitlets.Unicode: str,
734+
traitlets.Tuple: tuple,
735+
}
736+
737+
738+
class _TraitAnnotation:
739+
"""Trait annotation for a trait type"""
740+
741+
def __init__(self, trait_type):
742+
self.trait_type = trait_type
743+
744+
def __repr__(self):
745+
return self.trait_type.__name__
746+
747+
748+
def _trait_annotation(trait_type):
749+
"""Return an annotation for a trait"""
750+
if trait_type in _traitlet_annotations:
751+
return _traitlet_annotations[trait_type]
752+
else:
753+
annotation = _traitlet_annotations[trait_type] = _TraitAnnotation(trait_type)
754+
return annotation
755+
756+
757+
def _traitlet_signature(cls):
758+
"""Add traitlet-based signature to a class"""
759+
parameters = []
760+
for name, trait in cls.class_traits().items():
761+
if name.startswith("_"):
762+
# omit private traits
763+
continue
764+
if hasattr(trait, 'default'):
765+
# traitlets 5
766+
default = trait.default()
767+
else:
768+
default = trait.default_value
769+
if default is traitlets.Undefined:
770+
default = None
771+
772+
annotation = _trait_annotation(trait.__class__)
773+
774+
parameters.append(
775+
inspect.Parameter(
776+
name=name,
777+
kind=inspect.Parameter.KEYWORD_ONLY,
778+
annotation=annotation,
779+
default=default,
780+
)
781+
)
782+
cls.__signature__ = inspect.Signature(parameters)
783+
return cls

0 commit comments

Comments
 (0)