Skip to content

Commit

Permalink
feat: trait_to_py now handles ObserveHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
thorwhalen committed Nov 27, 2024
1 parent e466c42 commit 79031ed
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions ju/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
model_field_descriptions, # Extracts a dictionary of field paths & their descriptions from model.
)
from ju.viz import model_digraph # visualize pydantic models
from ju.traitlets_util import trait_to_py
3 changes: 2 additions & 1 deletion ju/tests/traitlets_util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ class Color(enum.Enum):
traitlets.Enum,
traitlets.CaselessStrEnum,
traitlets.FuzzyEnum,
traitlets.ObserveHandler,
]:
trait_instance = traitlet_type(values=['a', 'b', 'c'])
trait_instance = traitlet_type(['a', 'b', 'c'])
elif traitlet_type in {traitlets.List, traitlets.Set}:
trait_instance = traitlet_type(one_traitlet_type)
elif traitlet_type is traitlets.Tuple:
Expand Down
6 changes: 5 additions & 1 deletion ju/traitlets_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import traitlets
from traitlets import TraitType
import typing
from typing import Union, Type
from typing import Union, Type, Any, Callable, Dict

import enum
import re
import collections.abc

ObserveHandlerPyType = Callable[[Dict[str, Any]], Any]

# Mapping from traitlets types to Python types (as defined previously)
py_type_for_traitlet_type = {
# C* types before their base types
Expand Down Expand Up @@ -45,6 +48,7 @@
traitlets.ClassBasedTraitType: type,
# Other types
traitlets.Callable: collections.abc.Callable,
traitlets.ObserveHandler: ObserveHandlerPyType,
traitlets.UseEnum: enum.Enum,
traitlets.Union: typing.Union,
traitlets.Any: object,
Expand Down

0 comments on commit 79031ed

Please sign in to comment.