Skip to content

Commit 4a53285

Browse files
committed
Type hints in vector_layers.py
1 parent 0f87cff commit 4a53285

File tree

2 files changed

+77
-20
lines changed

2 files changed

+77
-20
lines changed

folium/features.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,11 @@ def __init__(
9393
):
9494
super().__init__(location, popup=popup, tooltip=tooltip)
9595
self._name = "RegularPolygonMarker"
96-
self.options = path_options(**kwargs)
96+
self.options = path_options(line=False, radius=radius, **kwargs)
9797
self.options.update(
9898
parse_options(
9999
number_of_sides=number_of_sides,
100100
rotation=rotation,
101-
radius=radius,
102101
)
103102
)
104103

folium/vector_layers.py

Lines changed: 76 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,26 @@
22
Wraps leaflet Polyline, Polygon, Rectangle, Circle, and CircleMarker
33
44
"""
5+
from typing import List, Optional, Sequence, Union
56

67
from branca.element import MacroElement
78
from jinja2 import Template
89

910
from folium.map import Marker, Popup, Tooltip
10-
from folium.utilities import camelize, get_bounds, validate_multi_locations
11-
12-
13-
def path_options(line=False, radius=False, **kwargs):
11+
from folium.utilities import (
12+
TypeLine,
13+
TypeMultiLine,
14+
TypePathOptions,
15+
camelize,
16+
get_bounds,
17+
validate_locations,
18+
validate_multi_locations,
19+
)
20+
21+
22+
def path_options(
23+
line: bool = False, radius: Optional[float] = None, **kwargs: TypePathOptions
24+
):
1425
"""
1526
Contains options and constants shared between vector overlays
1627
(Polygon, Polyline, Circle, CircleMarker, and Rectangle).
@@ -82,7 +93,7 @@ def path_options(line=False, radius=False, **kwargs):
8293
fill = True
8394
elif not fill_color:
8495
fill_color = color
85-
fill = kwargs.pop("fill", False)
96+
fill = kwargs.pop("fill", False) # type: ignore
8697

8798
gradient = kwargs.pop("gradient", None)
8899
if gradient is not None:
@@ -114,7 +125,12 @@ class BaseMultiLocation(MacroElement):
114125
115126
"""
116127

117-
def __init__(self, locations, popup=None, tooltip=None):
128+
def __init__(
129+
self,
130+
locations: TypeMultiLine,
131+
popup: Union[Popup, str, None] = None,
132+
tooltip: Union[Tooltip, str, None] = None,
133+
):
118134
super().__init__()
119135
self.locations = validate_multi_locations(locations)
120136
if popup is not None:
@@ -124,7 +140,7 @@ def __init__(self, locations, popup=None, tooltip=None):
124140
tooltip if isinstance(tooltip, Tooltip) else Tooltip(str(tooltip))
125141
)
126142

127-
def _get_self_bounds(self):
143+
def _get_self_bounds(self) -> List[List[Optional[float]]]:
128144
"""Compute the bounds of the object itself."""
129145
return get_bounds(self.locations)
130146

@@ -138,6 +154,7 @@ class PolyLine(BaseMultiLocation):
138154
----------
139155
locations: list of points (latitude, longitude)
140156
Latitude and Longitude of line (Northing, Easting)
157+
Pass multiple sequences of coordinates for a multi-polyline.
141158
popup: str or folium.Popup, default None
142159
Input text or visualization for object displayed when clicking.
143160
tooltip: str or folium.Tooltip, default None
@@ -179,7 +196,10 @@ class Polygon(BaseMultiLocation):
179196
Parameters
180197
----------
181198
locations: list of points (latitude, longitude)
182-
Latitude and Longitude of line (Northing, Easting)
199+
- One list of coordinate pairs to define a polygon. You don't have to
200+
add a last point equal to the first point.
201+
- If you pass a list with multiple of those it will make a multi-
202+
polygon.
183203
popup: string or folium.Popup, default None
184204
Input text or visualization for object displayed when clicking.
185205
tooltip: str or folium.Tooltip, default None
@@ -201,21 +221,27 @@ class Polygon(BaseMultiLocation):
201221
"""
202222
)
203223

204-
def __init__(self, locations, popup=None, tooltip=None, **kwargs):
224+
def __init__(
225+
self,
226+
locations: TypeMultiLine,
227+
popup: Union[Popup, str, None] = None,
228+
tooltip: Union[Tooltip, str, None] = None,
229+
**kwargs: TypePathOptions
230+
):
205231
super().__init__(locations, popup=popup, tooltip=tooltip)
206232
self._name = "Polygon"
207-
self.options = path_options(line=True, **kwargs)
233+
self.options = path_options(line=True, radius=None, **kwargs)
208234

209235

210-
class Rectangle(BaseMultiLocation):
236+
class Rectangle(MacroElement):
211237
"""Draw rectangle overlays on a map.
212238
213239
See :func:`folium.vector_layers.path_options` for the `Path` options.
214240
215241
Parameters
216242
----------
217-
bounds: list of points (latitude, longitude)
218-
Latitude and Longitude of line (Northing, Easting)
243+
bounds: [(lat1, lon1), (lat2, lon2)]
244+
Two lat lon pairs marking the two corners of the rectangle.
219245
popup: string or folium.Popup, default None
220246
Input text or visualization for object displayed when clicking.
221247
tooltip: str or folium.Tooltip, default None
@@ -237,10 +263,28 @@ class Rectangle(BaseMultiLocation):
237263
"""
238264
)
239265

240-
def __init__(self, bounds, popup=None, tooltip=None, **kwargs):
241-
super().__init__(bounds, popup=popup, tooltip=tooltip)
266+
def __init__(
267+
self,
268+
bounds: TypeLine,
269+
popup: Union[Popup, str, None] = None,
270+
tooltip: Union[Tooltip, str, None] = None,
271+
**kwargs: TypePathOptions
272+
):
273+
super().__init__()
242274
self._name = "rectangle"
243-
self.options = path_options(line=True, **kwargs)
275+
self.options = path_options(line=True, radius=None, **kwargs)
276+
self.locations = validate_locations(bounds)
277+
assert len(self.locations) == 2, "Need two lat/lon pairs"
278+
if popup is not None:
279+
self.add_child(popup if isinstance(popup, Popup) else Popup(str(popup)))
280+
if tooltip is not None:
281+
self.add_child(
282+
tooltip if isinstance(tooltip, Tooltip) else Tooltip(str(tooltip))
283+
)
284+
285+
def _get_self_bounds(self) -> List[List[Optional[float]]]:
286+
"""Compute the bounds of the object itself."""
287+
return get_bounds(self.locations)
244288

245289

246290
class Circle(Marker):
@@ -279,7 +323,14 @@ class Circle(Marker):
279323
"""
280324
)
281325

282-
def __init__(self, location=None, radius=50, popup=None, tooltip=None, **kwargs):
326+
def __init__(
327+
self,
328+
location: Sequence[float],
329+
radius: float = 50,
330+
popup: Union[Popup, str, None] = None,
331+
tooltip: Union[Tooltip, str, None] = None,
332+
**kwargs: TypePathOptions
333+
):
283334
super().__init__(location, popup=popup, tooltip=tooltip)
284335
self._name = "circle"
285336
self.options = path_options(line=False, radius=radius, **kwargs)
@@ -318,7 +369,14 @@ class CircleMarker(Marker):
318369
"""
319370
)
320371

321-
def __init__(self, location=None, radius=10, popup=None, tooltip=None, **kwargs):
372+
def __init__(
373+
self,
374+
location: Optional[Sequence[float]] = None,
375+
radius: float = 10,
376+
popup: Union[Popup, str, None] = None,
377+
tooltip: Union[Tooltip, str, None] = None,
378+
**kwargs: TypePathOptions
379+
):
322380
super().__init__(location, popup=popup, tooltip=tooltip)
323381
self._name = "CircleMarker"
324382
self.options = path_options(line=False, radius=radius, **kwargs)

0 commit comments

Comments
 (0)