diff options
| author | Markus Heiser <markus.heiser@darmarit.de> | 2025-09-30 14:00:09 +0200 |
|---|---|---|
| committer | Markus Heiser <markus.heiser@darmarIT.de> | 2025-10-01 07:13:10 +0200 |
| commit | e16b6cb148d9fe6599ec8ce4b2803f0aed3a1d6b (patch) | |
| tree | 3beec5a878f90987954fb0a3e4fc52919eacd570 /searx/engines | |
| parent | 41e0f2abf0546c3d27aa33d84e6220ea8bf257d0 (diff) | |
[fix] JSON format: serialization of the result-types
The ``JSONEncoder`` (``format="json"``) must perform a conversion to the
built-in types for the ``msgspec.Struct``::
if isinstance(o, msgspec.Struct):
return msgspec.to_builtins(o)
The result types are already of type ``msgspec.Struct``, so they can be
converted into built-in types.
The field types (in the result type) that were not yet of type ``msgspec.Struct``
have been converted to::
searx.weather.GeoLocation@dataclass -> msgspec.Struct
searx.weather.DateTime -> msgspec.Struct
searx.weather.Temperature -> msgspec.Struct
searx.weather.PressureUnits -> msgspec.Struct
searx.weather.WindSpeed -> msgspec.Struct
searx.weather.RelativeHumidity -> msgspec.Struct
searx.weather.Compass -> msgspec.Struct
BTW: Wherever it seemed sensible, the typing was also modernized in the modified
files.
Closes: https://github.com/searxng/searxng/issues/5250
Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
Diffstat (limited to 'searx/engines')
| -rw-r--r-- | searx/engines/duckduckgo_weather.py | 8 | ||||
| -rw-r--r-- | searx/engines/open_meteo.py | 12 | ||||
| -rw-r--r-- | searx/engines/wttr.py | 12 |
3 files changed, 18 insertions, 14 deletions
diff --git a/searx/engines/duckduckgo_weather.py b/searx/engines/duckduckgo_weather.py index 9fad1e546..4d52effcd 100644 --- a/searx/engines/duckduckgo_weather.py +++ b/searx/engines/duckduckgo_weather.py @@ -76,12 +76,12 @@ def _weather_data(location: weather.GeoLocation, data: dict[str, t.Any]): return EngineResults.types.WeatherAnswer.Item( location=location, - temperature=weather.Temperature(unit="°C", value=data['temperature']), + temperature=weather.Temperature(val=data['temperature'], unit="°C"), condition=WEATHERKIT_TO_CONDITION[data["conditionCode"]], - feels_like=weather.Temperature(unit="°C", value=data['temperatureApparent']), + feels_like=weather.Temperature(val=data['temperatureApparent'], unit="°C"), wind_from=weather.Compass(data["windDirection"]), - wind_speed=weather.WindSpeed(data["windSpeed"], unit="mi/h"), - pressure=weather.Pressure(data["pressure"], unit="hPa"), + wind_speed=weather.WindSpeed(val=data["windSpeed"], unit="mi/h"), + pressure=weather.Pressure(val=data["pressure"], unit="hPa"), humidity=weather.RelativeHumidity(data["humidity"] * 100), cloud_cover=data["cloudCover"] * 100, ) diff --git a/searx/engines/open_meteo.py b/searx/engines/open_meteo.py index 31ada12b0..948996b3c 100644 --- a/searx/engines/open_meteo.py +++ b/searx/engines/open_meteo.py @@ -1,6 +1,8 @@ # SPDX-License-Identifier: AGPL-3.0-or-later """Open Meteo (weather)""" +import typing as t + from urllib.parse import urlencode from datetime import datetime @@ -106,16 +108,16 @@ WMO_TO_CONDITION: dict[int, weather.WeatherConditionType] = { } -def _weather_data(location: weather.GeoLocation, data: dict): +def _weather_data(location: weather.GeoLocation, data: dict[str, t.Any]): return WeatherAnswer.Item( location=location, - temperature=weather.Temperature(unit="°C", value=data["temperature_2m"]), + temperature=weather.Temperature(val=data["temperature_2m"], unit="°C"), condition=WMO_TO_CONDITION[data["weather_code"]], - feels_like=weather.Temperature(unit="°C", value=data["apparent_temperature"]), + feels_like=weather.Temperature(val=data["apparent_temperature"], unit="°C"), wind_from=weather.Compass(data["wind_direction_10m"]), - wind_speed=weather.WindSpeed(data["wind_speed_10m"], unit="km/h"), - pressure=weather.Pressure(data["pressure_msl"], unit="hPa"), + wind_speed=weather.WindSpeed(val=data["wind_speed_10m"], unit="km/h"), + pressure=weather.Pressure(val=data["pressure_msl"], unit="hPa"), humidity=weather.RelativeHumidity(data["relative_humidity_2m"]), cloud_cover=data["cloud_cover"], ) diff --git a/searx/engines/wttr.py b/searx/engines/wttr.py index aec4d1075..9c7f69b43 100644 --- a/searx/engines/wttr.py +++ b/searx/engines/wttr.py @@ -1,6 +1,8 @@ # SPDX-License-Identifier: AGPL-3.0-or-later """wttr.in (weather forecast service)""" +import typing as t + from urllib.parse import quote from datetime import datetime @@ -80,19 +82,19 @@ def request(query, params): return params -def _weather_data(location: weather.GeoLocation, data: dict): +def _weather_data(location: weather.GeoLocation, data: dict[str, t.Any]): # the naming between different data objects is inconsitent, thus temp_C and # tempC are possible tempC: float = data.get("temp_C") or data.get("tempC") # type: ignore return WeatherAnswer.Item( location=location, - temperature=weather.Temperature(unit="°C", value=tempC), + temperature=weather.Temperature(val=tempC, unit="°C"), condition=WWO_TO_CONDITION[data["weatherCode"]], - feels_like=weather.Temperature(unit="°C", value=data["FeelsLikeC"]), + feels_like=weather.Temperature(val=data["FeelsLikeC"], unit="°C"), wind_from=weather.Compass(int(data["winddirDegree"])), - wind_speed=weather.WindSpeed(data["windspeedKmph"], unit="km/h"), - pressure=weather.Pressure(data["pressure"], unit="hPa"), + wind_speed=weather.WindSpeed(val=data["windspeedKmph"], unit="km/h"), + pressure=weather.Pressure(val=data["pressure"], unit="hPa"), humidity=weather.RelativeHumidity(data["humidity"]), cloud_cover=data["cloudcover"], ) |