[mod] addition of various type hints / tbc

- pyright configuration [1]_
- stub files: types-lxml [2]_
- addition of various type hints
- enable use of new type system features on older Python versions [3]_
- ``.tool-versions`` - set python to lowest version we support (3.10.18) [4]_:
  Older versions typically lack some typing features found in newer Python
  versions.  Therefore, for local type checking (before commit), it is necessary
  to use the older Python interpreter.

.. [1] https://docs.basedpyright.com/v1.20.0/configuration/config-files/
.. [2] https://pypi.org/project/types-lxml/
.. [3] https://typing-extensions.readthedocs.io/en/latest/#
.. [4] https://mise.jdx.dev/configuration.html#tool-versions

Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
Format: reST
This commit is contained in:
Markus Heiser
2025-08-22 17:17:51 +02:00
committed by Markus Heiser
parent 09500459fe
commit 57b9673efb
107 changed files with 1205 additions and 1251 deletions

View File

@@ -34,7 +34,7 @@ from searx.cache import ExpireCache, ExpireCacheCfg
from searx.extended_types import sxng_request
from searx.wikidata_units import convert_to_si, convert_from_si
WEATHER_DATA_CACHE: ExpireCache = None # type: ignore
WEATHER_DATA_CACHE: ExpireCache | None = None
"""A simple cache for weather data (geo-locations, icons, ..)"""
YR_WEATHER_SYMBOL_URL = "https://raw.githubusercontent.com/nrkno/yr-weather-symbols/refs/heads/master/symbols/outline"
@@ -90,7 +90,7 @@ def _get_sxng_locale_tag() -> str:
return "en"
def symbol_url(condition: WeatherConditionType) -> str | None:
def symbol_url(condition: "WeatherConditionType") -> str | None:
"""Returns ``data:`` URL for the weather condition symbol or ``None`` if
the condition is not of type :py:obj:`WeatherConditionType`.
@@ -168,7 +168,7 @@ class GeoLocation:
return babel.Locale("en", territory="DE")
@classmethod
def by_query(cls, search_term: str) -> GeoLocation:
def by_query(cls, search_term: str) -> "GeoLocation":
"""Factory method to get a GeoLocation object by a search term. If no
location can be determined for the search term, a :py:obj:`ValueError`
is thrown.
@@ -182,10 +182,10 @@ class GeoLocation:
geo_props = cls._query_open_meteo(search_term=search_term)
cache.set(key=search_term, value=geo_props, expire=None, ctx=ctx)
return cls(**geo_props)
return cls(**geo_props) # type: ignore
@classmethod
def _query_open_meteo(cls, search_term: str) -> dict:
def _query_open_meteo(cls, search_term: str) -> dict[str, str]:
url = f"https://geocoding-api.open-meteo.com/v1/search?name={quote_plus(search_term)}"
resp = network.get(url, timeout=3)
if resp.status_code != 200:
@@ -200,6 +200,7 @@ class GeoLocation:
DateTimeFormats = typing.Literal["full", "long", "medium", "short"]
@typing.final
class DateTime:
"""Class to represent date & time. Essentially, it is a wrapper that
conveniently combines :py:obj:`datetime.datetime` and
@@ -226,6 +227,7 @@ class DateTime:
return babel.dates.format_datetime(self.datetime, format=fmt, locale=locale)
@typing.final
class Temperature:
"""Class for converting temperature units and for string representation of
measured values."""
@@ -293,6 +295,7 @@ class Temperature:
return template.format(value=val_str, unit=unit)
@typing.final
class Pressure:
"""Class for converting pressure units and for string representation of
measured values."""
@@ -335,6 +338,7 @@ class Pressure:
return template.format(value=val_str, unit=unit)
@typing.final
class WindSpeed:
"""Class for converting speed or velocity units and for string
representation of measured values.
@@ -384,6 +388,7 @@ class WindSpeed:
return template.format(value=val_str, unit=unit)
@typing.final
class RelativeHumidity:
"""Amount of relative humidity in the air. The unit is ``%``"""
@@ -417,6 +422,7 @@ class RelativeHumidity:
return template.format(value=val_str, unit=unit)
@typing.final
class Compass:
"""Class for converting compass points and azimuth values (360°)"""