Skip to content

Commit

Permalink
Fix generator for unions
Browse files Browse the repository at this point in the history
  • Loading branch information
vidartf committed Jan 17, 2020
1 parent 5ed5085 commit 4623f25
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions packages/schema/generate-spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,22 @@
import json
from operator import itemgetter

from traitlets import (CaselessStrEnum, Unicode, Tuple, List, Bool, CFloat,
Float, CInt, Int, Instance, Dict, Bytes, Any)
from traitlets import (
CaselessStrEnum,
Unicode,
Tuple,
List,
Bool,
CFloat,
Float,
CInt,
Int,
Instance,
Dict,
Bytes,
Any,
Union,
)

import ipywidgets as widgets
from ipywidgets import Color
Expand Down Expand Up @@ -62,7 +76,16 @@ def trait_type(trait, widget_list):
w_type = 'color'
elif isinstance(trait, Dict):
w_type = 'object'
elif isinstance(trait, Bytes) or isinstance(trait, ByteMemoryView):
elif isinstance(trait, Union):
union_attributes = []
union_types = []
for ut in trait.trait_types:
ua = trait_type(ut, widget_list)
union_attributes.append(ua)
union_types.append(ua['type'])
w_type = union_types
attributes['union_attributes'] = union_attributes
elif isinstance(trait, (Bytes, ByteMemoryView)):
w_type = 'bytes'
elif isinstance(trait, Instance) and issubclass(trait.klass,
widgets.Widget):
Expand All @@ -89,6 +112,8 @@ def jsdefault(trait):
default = trait.make_dynamic_default()
if issubclass(trait.klass, widgets.Widget):
return 'reference to new instance'
elif isinstance(trait, Union):
default = trait.make_dynamic_default()
else:
default = trait.default_value
if isinstance(default, bytes) or isinstance(default, memoryview):
Expand All @@ -112,6 +137,10 @@ def mddefault(attribute):

def mdtype(attribute):
md_type = attribute['type']
if 'union_attributes' in attribute and isinstance(md_type, (list, tuple)):
md_type = ' or '.join(
mdtype(ua) for ua in attribute['union_attributes']
)
if md_type in NUMBER_MAP:
md_type = NUMBER_MAP[md_type]
if attribute.get('allow_none'):
Expand Down

0 comments on commit 4623f25

Please sign in to comment.