Skip to content

Commit

Permalink
fix(mediatypes): only use dataclass(slots=True) where supported (>=py…
Browse files Browse the repository at this point in the history
…310)
  • Loading branch information
vytas7 committed Oct 5, 2024
1 parent b0e3829 commit 8618640
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions falcon/util/mediatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import dataclasses
import functools
import math
import sys
from typing import Dict, Iterable, Iterator, Tuple

from falcon import errors
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 8618640

Please sign in to comment.