[fix] msgspec.Struct: alias name t.ClassVar not properly detected

Reported in [1], HOTFIX in [2], this patch here is now the final solution.

Note that if using PEP 563 postponed evaluation of annotations" (e.g. ``from
__future__ import annotations``) only the following spellings will work:

    ClassVar or ClassVar[<type>]
    typing.ClassVar or typing.ClassVar[<type>]

Importing ClassVar or typing under an aliased name (e.g. ``import typing as t``)
will not be properly detected. [3]

[1] https://github.com/searxng/searxng/issues/5304#issuecomment-3394140820
[2] https://github.com/searxng/searxng/pull/5309
[3] https://jcristharif.com/msgspec/structs.html#class-variables

Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
This commit is contained in:
Markus Heiser 2025-10-30 12:23:47 +01:00 committed by Markus Heiser
parent 5712827703
commit b155e66fe5

View File

@ -6,6 +6,11 @@
# - https://github.com/searxng/searxng/issues/5284 # - https://github.com/searxng/searxng/issues/5284
from __future__ import annotations from __future__ import annotations
# msgspec: note that if using PEP 563 “postponed evaluation of annotations”
# (e.g. from __future__ import annotations) only the following spellings will
# work: https://jcristharif.com/msgspec/structs.html#class-variables
from typing import ClassVar
__all__ = [ __all__ = [
"symbol_url", "symbol_url",
"Temperature", "Temperature",
@ -266,8 +271,8 @@ class Temperature(msgspec.Struct, kw_only=True):
val: float val: float
unit: TemperatureUnit unit: TemperatureUnit
si_name: t.ClassVar[str] = "Q11579" si_name: ClassVar[str] = "Q11579"
UNITS: t.ClassVar[tuple[TemperatureUnit]] = TEMPERATURE_UNITS UNITS: ClassVar[tuple[TemperatureUnit]] = TEMPERATURE_UNITS
def __post_init__(self): def __post_init__(self):
if self.unit not in self.UNITS: if self.unit not in self.UNITS:
@ -334,8 +339,8 @@ class Pressure(msgspec.Struct, kw_only=True):
val: float val: float
unit: PressureUnit unit: PressureUnit
si_name: t.ClassVar[str] = "Q44395" si_name: ClassVar[str] = "Q44395"
UNITS: t.ClassVar[tuple[PressureUnit]] = PRESSURE_UNITS UNITS: ClassVar[tuple[PressureUnit]] = PRESSURE_UNITS
def __post_init__(self): def __post_init__(self):
if self.unit not in self.UNITS: if self.unit not in self.UNITS:
@ -387,8 +392,8 @@ class WindSpeed(msgspec.Struct, kw_only=True):
val: float val: float
unit: WindSpeedUnit unit: WindSpeedUnit
si_name: t.ClassVar[str] = "Q182429" si_name: ClassVar[str] = "Q182429"
UNITS: t.ClassVar[tuple[WindSpeedUnit]] = WIND_SPEED_UNITS UNITS: ClassVar[tuple[WindSpeedUnit]] = WIND_SPEED_UNITS
def __post_init__(self): def __post_init__(self):
if self.unit not in self.UNITS: if self.unit not in self.UNITS:
@ -432,8 +437,8 @@ class RelativeHumidity(msgspec.Struct):
val: float val: float
# there exists only one unit (%) --> set "%" as the final value (constant) # there exists only one unit (%) --> set "%" as the final value (constant)
unit: t.ClassVar[RelativeHumidityUnit] = "%" unit: ClassVar[RelativeHumidityUnit] = "%"
UNITS: t.ClassVar[tuple[RelativeHumidityUnit]] = RELATIVE_HUMIDITY_UNITS UNITS: ClassVar[tuple[RelativeHumidityUnit]] = RELATIVE_HUMIDITY_UNITS
def __post_init__(self): def __post_init__(self):
if self.unit not in self.UNITS: if self.unit not in self.UNITS:
@ -476,15 +481,15 @@ class Compass(msgspec.Struct):
val: "float | int | CompassPoint" val: "float | int | CompassPoint"
unit: CompassUnit = "°" unit: CompassUnit = "°"
UNITS: t.ClassVar[tuple[CompassUnit]] = COMPASS_UNITS UNITS: ClassVar[tuple[CompassUnit]] = COMPASS_UNITS
TURN: t.ClassVar[float] = 360.0 TURN: ClassVar[float] = 360.0
"""Full turn (360°)""" """Full turn (360°)"""
POINTS: t.ClassVar[tuple[CompassPoint]] = COMPASS_POINTS POINTS: ClassVar[tuple[CompassPoint]] = COMPASS_POINTS
"""Compass points.""" """Compass points."""
RANGE: t.ClassVar[float] = TURN / len(POINTS) RANGE: ClassVar[float] = TURN / len(POINTS)
"""Angle sector of a compass point""" """Angle sector of a compass point"""
def __post_init__(self): def __post_init__(self):