Skip to content

Commit 95e828f

Browse files
committed
Type hints in raster_layers.py
1 parent fa7e3c9 commit 95e828f

File tree

1 file changed

+61
-50
lines changed

1 file changed

+61
-50
lines changed

folium/raster_layers.py

Lines changed: 61 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,23 @@
22
Wraps leaflet TileLayer, WmsTileLayer (TileLayer.WMS), ImageOverlay, and VideoOverlay
33
44
"""
5+
from typing import TYPE_CHECKING, Any, Callable, Optional, Union
56

67
from branca.element import Element, Figure
78
from jinja2 import Environment, PackageLoader, Template
89

910
from folium.map import Layer
10-
from folium.utilities import image_to_url, mercator_transform, parse_options
11+
from folium.utilities import (
12+
TypeBounds,
13+
TypeJsonValue,
14+
image_to_url,
15+
mercator_transform,
16+
parse_options,
17+
)
18+
19+
if TYPE_CHECKING:
20+
import xyzservices
21+
1122

1223
ENV = Environment(loader=PackageLoader("folium", "templates"))
1324

@@ -78,30 +89,30 @@ class TileLayer(Layer):
7889

7990
def __init__(
8091
self,
81-
tiles="OpenStreetMap",
82-
min_zoom=0,
83-
max_zoom=18,
84-
max_native_zoom=None,
85-
attr=None,
86-
detect_retina=False,
87-
name=None,
88-
overlay=False,
89-
control=True,
90-
show=True,
91-
no_wrap=False,
92-
subdomains="abc",
93-
tms=False,
94-
opacity=1,
92+
tiles: Union[str, "xyzservices.TileProvider"] = "OpenStreetMap",
93+
min_zoom: int = 0,
94+
max_zoom: int = 18,
95+
max_native_zoom: Optional[int] = None,
96+
attr: Optional[str] = None,
97+
detect_retina: bool = False,
98+
name: Optional[str] = None,
99+
overlay: bool = False,
100+
control: bool = True,
101+
show: bool = True,
102+
no_wrap: bool = False,
103+
subdomains: str = "abc",
104+
tms: bool = False,
105+
opacity: float = 1,
95106
**kwargs
96107
):
97108

98109
# check for xyzservices.TileProvider without importing it
99110
if isinstance(tiles, dict):
100-
attr = attr if attr else tiles.html_attribution
111+
attr = attr if attr else tiles.html_attribution # type: ignore
101112
min_zoom = tiles.get("min_zoom", min_zoom)
102113
max_zoom = tiles.get("max_zoom", max_zoom)
103114
subdomains = tiles.get("subdomains", subdomains)
104-
tiles = tiles.build_url(fill_subdomain=False, scale_factor="{r}")
115+
tiles = tiles.build_url(fill_subdomain=False, scale_factor="{r}") # type: ignore
105116

106117
self.tile_name = (
107118
name if name is not None else "".join(tiles.lower().strip().split())
@@ -198,17 +209,17 @@ class WmsTileLayer(Layer):
198209

199210
def __init__(
200211
self,
201-
url,
202-
layers,
203-
styles="",
204-
fmt="image/jpeg",
205-
transparent=False,
206-
version="1.1.1",
207-
attr="",
208-
name=None,
209-
overlay=True,
210-
control=True,
211-
show=True,
212+
url: str,
213+
layers: str,
214+
styles: str = "",
215+
fmt: str = "image/jpeg",
216+
transparent: bool = False,
217+
version: str = "1.1.1",
218+
attr: str = "",
219+
name: Optional[str] = None,
220+
overlay: bool = True,
221+
control: bool = True,
222+
show: bool = True,
212223
**kwargs
213224
):
214225
super().__init__(name=name, overlay=overlay, control=control, show=show)
@@ -289,16 +300,16 @@ class ImageOverlay(Layer):
289300

290301
def __init__(
291302
self,
292-
image,
293-
bounds,
294-
origin="upper",
295-
colormap=None,
296-
mercator_project=False,
297-
pixelated=True,
298-
name=None,
299-
overlay=True,
300-
control=True,
301-
show=True,
303+
image: Any,
304+
bounds: TypeBounds,
305+
origin: str = "upper",
306+
colormap: Optional[Callable] = None,
307+
mercator_project: bool = False,
308+
pixelated: bool = True,
309+
name: Optional[str] = None,
310+
overlay: bool = True,
311+
control: bool = True,
312+
show: bool = True,
302313
**kwargs
303314
):
304315
super().__init__(name=name, overlay=overlay, control=control, show=show)
@@ -313,7 +324,7 @@ def __init__(
313324

314325
self.url = image_to_url(image, origin=origin, colormap=colormap)
315326

316-
def render(self, **kwargs):
327+
def render(self, **kwargs) -> None:
317328
super().render()
318329

319330
figure = self.get_root()
@@ -338,7 +349,7 @@ def render(self, **kwargs):
338349
Element(pixelated), name="leaflet-image-layer"
339350
) # noqa
340351

341-
def _get_self_bounds(self):
352+
def _get_self_bounds(self) -> TypeBounds:
342353
"""
343354
Computes the bounds of the object itself (not including it's children)
344355
in the form [[lat_min, lon_min], [lat_max, lon_max]].
@@ -388,15 +399,15 @@ class VideoOverlay(Layer):
388399

389400
def __init__(
390401
self,
391-
video_url,
392-
bounds,
393-
autoplay=True,
394-
loop=True,
395-
name=None,
396-
overlay=True,
397-
control=True,
398-
show=True,
399-
**kwargs
402+
video_url: str,
403+
bounds: TypeBounds,
404+
autoplay: bool = True,
405+
loop: bool = True,
406+
name: Optional[str] = None,
407+
overlay: bool = True,
408+
control: bool = True,
409+
show: bool = True,
410+
**kwargs: TypeJsonValue
400411
):
401412
super().__init__(name=name, overlay=overlay, control=control, show=show)
402413
self._name = "VideoOverlay"
@@ -405,7 +416,7 @@ def __init__(
405416
self.bounds = bounds
406417
self.options = parse_options(autoplay=autoplay, loop=loop, **kwargs)
407418

408-
def _get_self_bounds(self):
419+
def _get_self_bounds(self) -> TypeBounds:
409420
"""
410421
Computes the bounds of the object itself (not including it's children)
411422
in the form [[lat_min, lon_min], [lat_max, lon_max]]

0 commit comments

Comments
 (0)