Skip to content

Commit f31aae2

Browse files
Copilotllucax
andcommitted
Remove timezonefinder dependency and automatic timezone lookup
Co-authored-by: llucax <1031485+llucax@users.noreply.github.com>
1 parent df07a2b commit f31aae2

File tree

3 files changed

+11
-49
lines changed

3 files changed

+11
-49
lines changed

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ dependencies = [
4242
"frequenz-client-common >= 0.3.2, < 0.4.0",
4343
"grpcio >= 1.63.0, < 2",
4444
"protobuf >= 5.26.1, < 7",
45-
"timezonefinder >= 6.2.0, < 7",
4645
"typing-extensions >= 4.13.0, < 5",
4746
]
4847
dynamic = ["version"]

src/frequenz/client/microgrid/_metadata.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
from zoneinfo import ZoneInfo
88

99
from frequenz.client.common.microgrid import MicrogridId
10-
from timezonefinder import TimezoneFinder
11-
12-
_timezone_finder = TimezoneFinder()
1310

1411

1512
@dataclass(frozen=True, kw_only=True)
@@ -25,22 +22,9 @@ class Location:
2522
timezone: ZoneInfo | None = None
2623
"""The timezone of the microgrid.
2724
28-
If not passed during construction (or `None` is passed), and there is a `longitude`
29-
and `latitude`, then the timezone wil be looked up in a database based on the
30-
coordinates. This lookup could fail, in which case the timezone will still be
31-
`None`.
25+
If not provided during construction, the timezone will remain `None`.
3226
"""
3327

34-
def __post_init__(self) -> None:
35-
"""Initialize the timezone of the microgrid."""
36-
if self.latitude is None or self.longitude is None or self.timezone is not None:
37-
return
38-
39-
timezone = _timezone_finder.timezone_at(lat=self.latitude, lng=self.longitude)
40-
if timezone:
41-
# The dataclass is frozen, so it needs to use __setattr__ to set the timezone.
42-
object.__setattr__(self, "timezone", ZoneInfo(key=timezone))
43-
4428

4529
@dataclass(frozen=True, kw_only=True)
4630
class Metadata:

tests/test_metadata.py

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
"""Tests for the microgrid metadata types."""
55

6-
from collections.abc import Iterator
7-
from unittest.mock import MagicMock, patch
86
from zoneinfo import ZoneInfo
97

108
import pytest
@@ -13,15 +11,6 @@
1311
from frequenz.client.microgrid import Location, Metadata
1412

1513

16-
@pytest.fixture
17-
def timezone_finder() -> Iterator[MagicMock]:
18-
"""Return a mock timezone finder."""
19-
with patch(
20-
"frequenz.client.microgrid._metadata._timezone_finder", autospec=True
21-
) as mock_timezone_finder:
22-
yield mock_timezone_finder
23-
24-
2514
@pytest.mark.parametrize(
2615
"latitude, longitude, timezone",
2716
[
@@ -32,40 +21,29 @@ def timezone_finder() -> Iterator[MagicMock]:
3221
(52.52, None, ZoneInfo(key="UTC")),
3322
(None, 13.405, ZoneInfo(key="UTC")),
3423
(52.52, 13.405, ZoneInfo(key="UTC")),
24+
(52.52, 13.405, None),
3525
],
3626
ids=str,
3727
)
38-
def test_location_timezone_not_looked_up_if_not_possible_or_necessary(
39-
timezone_finder: MagicMock,
28+
def test_location_timezone_behavior(
4029
latitude: float | None,
4130
longitude: float | None,
4231
timezone: ZoneInfo | None,
4332
) -> None:
44-
"""Test the location timezone is not looked up if is not necessary or possible."""
45-
timezone_finder.timezone_at.return_value = "Europe/Berlin"
46-
33+
"""Test the location timezone behavior with different inputs."""
4734
location = Location(latitude=latitude, longitude=longitude, timezone=timezone)
4835

4936
assert location.latitude == latitude
5037
assert location.longitude == longitude
5138
assert location.timezone == timezone
52-
timezone_finder.timezone_at.assert_not_called()
5339

5440

55-
@pytest.mark.parametrize("timezone", [None, "Europe/Berlin"], ids=str)
56-
def test_location_timezone_lookup(
57-
timezone_finder: MagicMock, timezone: str | None
58-
) -> None:
59-
"""Test the location timezone is looked up if not provided and there is enough info."""
60-
timezone_finder.timezone_at.return_value = timezone
61-
41+
def test_location_timezone_remains_none_when_not_provided() -> None:
42+
"""Test that timezone remains None when not explicitly provided, even with lat/lng."""
6243
location = Location(latitude=52.52, longitude=13.405)
63-
64-
if timezone is None:
65-
assert location.timezone is None
66-
else:
67-
assert location.timezone == ZoneInfo(key=timezone)
68-
timezone_finder.timezone_at.assert_called_once_with(lat=52.52, lng=13.405)
44+
45+
# Timezone should remain None since automatic lookup is no longer performed
46+
assert location.timezone is None
6947

7048

7149
def test_metadata_initialization() -> None:
@@ -81,11 +59,12 @@ def test_metadata_initialization() -> None:
8159
assert metadata.microgrid_id == microgrid_id
8260
assert metadata.location is None
8361

84-
# Test with only location
62+
# Test with only location - timezone should be None even with lat/lng
8563
location = Location(latitude=52.52, longitude=13.405)
8664
metadata = Metadata(location=location)
8765
assert metadata.microgrid_id is None
8866
assert metadata.location == location
67+
assert metadata.location.timezone is None
8968

9069
# Test with both parameters
9170
metadata = Metadata(microgrid_id=microgrid_id, location=location)

0 commit comments

Comments
 (0)