From 86186404716a5e158d6a1f82bc2c04a1573cf104 Mon Sep 17 00:00:00 2001 From: Vytautas Liuolia Date: Sat, 5 Oct 2024 12:56:28 +0200 Subject: [PATCH] fix(mediatypes): only use dataclass(slots=True) where supported (>=py310) --- falcon/util/mediatypes.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/falcon/util/mediatypes.py b/falcon/util/mediatypes.py index 125ba8f5e..fa4faad17 100644 --- a/falcon/util/mediatypes.py +++ b/falcon/util/mediatypes.py @@ -19,6 +19,7 @@ import dataclasses import functools import math +import sys from typing import Dict, Iterable, Iterator, Tuple from falcon import errors @@ -114,10 +115,17 @@ def _parse_media_type_header(media_type: str) -> Tuple[str, str, dict]: # TODO(vytas): Should we make these data structures public? +# NOTE(vytas): The slots parameter requires Python 3.10. +_dataclass_with_slots = ( + dataclasses.dataclass(slots=True) + if sys.version_info >= (3, 10) + else dataclasses.dataclass() +) + # PERF(vytas): It would be nice to use frozen=True as we never modify the data, # but it seems to incur a performance hit (~2-3x) on CPython 3.12. -@dataclasses.dataclass(slots=True) +@_dataclass_with_slots class _MediaType: main_type: str subtype: str @@ -128,7 +136,7 @@ def parse(cls, media_type: str) -> _MediaType: return cls(*_parse_media_type_header(media_type)) -@dataclasses.dataclass(slots=True) +@_dataclass_with_slots class _MediaRange: main_type: str subtype: str