Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise error when creating class with unknown keyword argument #209

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")