From 27a494347e04c3920f2822bee2adf40cf5a14e94 Mon Sep 17 00:00:00 2001 From: Anthony Shaw Date: Tue, 8 Mar 2022 19:00:40 +1100 Subject: [PATCH] Python 3.9 compatibility --- bench_class.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/bench_class.py b/bench_class.py index 0520863..83874e1 100644 --- a/bench_class.py +++ b/bench_class.py @@ -1,7 +1,7 @@ from collections import namedtuple from dataclasses import dataclass import typing - +import sys def attributes_in_class(): class Pet: @@ -46,15 +46,16 @@ def attributes_in_dataclass(): dog = Pet(4, "woof") str(dog) -def attributes_in_dataclass_with_slots(): - @dataclass(slots=True) - class Pet: - legs: int - noise: str - - for _ in range(100000): - dog = Pet(4, "woof") - str(dog) +if sys.version_info.minor >= 10: + def attributes_in_dataclass_with_slots(): + @dataclass(slots=True) + class Pet: + legs: int + noise: str + + for _ in range(100000): + dog = Pet(4, "woof") + str(dog) def attributes_in_namedtuple(): Pet = namedtuple("Pet", "legs noise") @@ -78,10 +79,12 @@ def attributes_in_dict(): __benchmarks__ = [ (attributes_in_dataclass, attributes_in_class, "Class instead of dataclass"), - (attributes_in_dataclass, attributes_in_dataclass_with_slots, "dataclass with slots"), (attributes_in_dataclass, attributes_in_namedtuple, "Namedtuple instead of dataclass"), (attributes_in_namedtuple, attributes_in_class, "class instead of namedtuple"), (attributes_in_namedtuple, attributes_in_namedtuple_type, "namedtuple class instead of namedtuple"), (attributes_in_class, attributes_in_dict, "dict instead of class"), - (attributes_in_class, attributes_in_class_with_slots, "class with slots"), + (attributes_in_class, attributes_in_class_with_slots, "class with slots") ] +if sys.version_info.minor >= 10: + __benchmarks__.append((attributes_in_dataclass, attributes_in_dataclass_with_slots, "dataclass with slots")) +