Skip to content

Commit

Permalink
Don't fail catastrophically when range is zero
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpap committed Apr 30, 2024
1 parent 43a141b commit 2535273
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
11 changes: 11 additions & 0 deletions bokehjs/src/lib/core/kinds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,16 @@ export namespace Kinds {
}
}

export class NonEmptyList<ItemType> extends List<ItemType> {
override valid(value: unknown): value is ItemType[] {
return super.valid(value) && value.length != 0
}

override toString(): string {
return `NonEmptyList(${this.item_type.toString()})`
}
}

export class Null extends Primitive<null> {
valid(value: unknown): value is null {
return value === null
Expand Down Expand Up @@ -639,6 +649,7 @@ export const PartialStruct = <T extends {[key: string]: unknown}>(struct_type: K
export const Iterable = <ItemType>(item_type: Kind<ItemType>) => new Kinds.Iterable(item_type)
export const Arrayable = <ItemType>(item_type: Kind<ItemType>) => new Kinds.Arrayable(item_type)
export const List = <ItemType>(item_type: Kind<ItemType>) => new Kinds.List(item_type)
export const NonEmptyList = <ItemType>(item_type: Kind<ItemType>) => new Kinds.NonEmptyList(item_type)
export const Dict = <V>(item_type: Kind<V>) => new Kinds.Dict(item_type)
export const Mapping = <K, V>(key_type: Kind<K>, item_type: Kind<V>) => new Kinds.Mapping(key_type, item_type)
export const Set = <V>(item_type: Kind<V>) => new Kinds.Set(item_type)
Expand Down
8 changes: 6 additions & 2 deletions bokehjs/src/lib/models/tickers/composite_ticker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export class CompositeTicker extends ContinuousTicker {
}

static {
this.define<CompositeTicker.Props>(({List, Ref}) => ({
tickers: [ List(Ref(ContinuousTicker)), [] ],
this.define<CompositeTicker.Props>(({NonEmptyList, Ref}) => ({
tickers: [ NonEmptyList(Ref(ContinuousTicker)) ],
}))
}

Expand All @@ -52,6 +52,10 @@ export class CompositeTicker extends ContinuousTicker {

get_best_ticker(data_low: number, data_high: number, desired_n_ticks: number): ContinuousTicker {
const data_range = data_high - data_low
if (data_range == 0) {
return this.tickers[0]
}

const ideal_interval = this.get_ideal_interval(data_low, data_high, desired_n_ticks)
const ticker_ndxs = [
sorted_index(this.min_intervals, ideal_interval) - 1,
Expand Down
9 changes: 6 additions & 3 deletions src/bokeh/models/tickers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
Float,
Instance,
Int,
NonEmpty,
Nullable,
Override,
Required,
Expand Down Expand Up @@ -169,10 +170,12 @@ class CompositeTicker(ContinuousTicker):
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)

tickers = Seq(Instance(Ticker), default=[], help="""
A list of Ticker objects to combine at different scales in order
tickers = NonEmpty(Seq(Instance(Ticker)), help="""
A list of ``Ticker`` objects to combine at different scales in order
to generate tick values. The supplied tickers should be in order.
Specifically, if S comes before T, then it should be the case that::
Specifically, if S comes before T, then it should be the case that:
.. code-block:: javascript
S.get_max_interval() < T.get_min_interval()
Expand Down
5 changes: 4 additions & 1 deletion tests/baselines/defaults.json5
Original file line number Diff line number Diff line change
Expand Up @@ -5654,7 +5654,10 @@
},
CompositeTicker: {
__extends__: "ContinuousTicker",
tickers: [],
tickers: {
type: "symbol",
name: "unset",
},
},
ContinuousTicker: {
__extends__: "Ticker",
Expand Down

0 comments on commit 2535273

Please sign in to comment.