Skip to content

Commit

Permalink
Error when creating class with unknown keyword argument (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebarron authored Nov 6, 2023
1 parent a2b6913 commit c5bc317
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
24 changes: 24 additions & 0 deletions lonboard/_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from anywidget import AnyWidget
from ipywidgets import Widget


class BaseWidget(Widget):
def __init__(self, **kwargs):
# Raise error on unknown keyword name
layer_trait_names = self.class_own_traits().keys()
for provided_trait_name in kwargs.keys():
if provided_trait_name not in layer_trait_names:
raise TypeError(f"unexpected keyword argument '{provided_trait_name}'")

super().__init__(**kwargs)


class BaseAnyWidget(AnyWidget):
def __init__(self, **kwargs):
# Raise error on unknown keyword name
layer_trait_names = self.class_own_traits().keys()
for provided_trait_name in kwargs.keys():
if provided_trait_name not in layer_trait_names:
raise TypeError(f"unexpected keyword argument '{provided_trait_name}'")

super().__init__(**kwargs)
4 changes: 2 additions & 2 deletions lonboard/_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import geopandas as gpd
import pyarrow as pa
import traitlets
from ipywidgets import Widget

from lonboard._base import BaseWidget
from lonboard._constants import EPSG_4326, EXTENSION_NAME, OGC_84
from lonboard._geoarrow.geopandas_interop import geopandas_to_geoarrow
from lonboard._serialization import infer_rows_per_chunk
Expand All @@ -22,7 +22,7 @@
from typing_extensions import Self


class BaseLayer(Widget):
class BaseLayer(BaseWidget):
table: traitlets.TraitType

pickable = traitlets.Bool(True).tag(sync=True)
Expand Down
4 changes: 2 additions & 2 deletions lonboard/_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
from pathlib import Path
from typing import Union

import anywidget
import ipywidgets
import traitlets
from ipywidgets.embed import embed_minimal_html

from lonboard._base import BaseAnyWidget
from lonboard._layer import BaseLayer
from lonboard._viewport import compute_view

# bundler yields lonboard/static/{index.js,styles.css}
bundler_output_dir = Path(__file__).parent / "static"


class Map(anywidget.AnyWidget):
class Map(BaseAnyWidget):
"""
The top-level class used to display a map in a Jupyter Widget.
Expand Down
8 changes: 8 additions & 0 deletions tests/test_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@ def test_accessor_length_validation():
_layer = ScatterplotLayer.from_geopandas(gdf, get_radius=np.array([1, 2, 3]))

_layer = ScatterplotLayer.from_geopandas(gdf, get_radius=np.array([1, 2]))


def test_layer_fails_with_unexpected_argument():
points = shapely.points([1, 2], [3, 4])
gdf = gpd.GeoDataFrame(geometry=points)

with pytest.raises(TypeError, match="unexpected keyword argument"):
ScatterplotLayer.from_geopandas(gdf, unknown_keyword="foo")
15 changes: 15 additions & 0 deletions tests/test_map.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import geopandas as gpd
import pytest
import shapely

from lonboard import Map, ScatterplotLayer


def test_map_fails_with_unexpected_argument():
points = shapely.points([1, 2], [3, 4])
gdf = gpd.GeoDataFrame(geometry=points)

layer = ScatterplotLayer.from_geopandas(gdf)

with pytest.raises(TypeError, match="unexpected keyword argument"):
Map(layers=[layer], unknown_keyword="foo")

0 comments on commit c5bc317

Please sign in to comment.