Skip to content

Commit

Permalink
Fix slots missing in python 3.9
Browse files Browse the repository at this point in the history
  • Loading branch information
r0x0d committed Nov 7, 2024
1 parent 8ccaa58 commit 7e58335
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions command_line_assistant/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import dataclasses
import json
import logging
Expand Down Expand Up @@ -36,7 +38,24 @@
"""


@dataclasses.dataclass(slots=True)
def dataclass(cls, slots=True):
"""Custom dataclass decorator to mimic the behavior of dataclass for Python 3.9"""
try:
return dataclasses.dataclass(cls, slots=slots)
except TypeError:

Check warning on line 45 in command_line_assistant/config.py

View check run for this annotation

Codecov / codecov/patch

command_line_assistant/config.py#L45

Added line #L45 was not covered by tests

def wrap(cls):

Check warning on line 47 in command_line_assistant/config.py

View check run for this annotation

Codecov / codecov/patch

command_line_assistant/config.py#L47

Added line #L47 was not covered by tests
# Create a new dict for our new class.
cls_dict = dict(cls.__dict__)
field_names = tuple(name for name, _ in cls_dict.keys())

Check warning on line 50 in command_line_assistant/config.py

View check run for this annotation

Codecov / codecov/patch

command_line_assistant/config.py#L49-L50

Added lines #L49 - L50 were not covered by tests
# The slots for our class
cls_dict["__slots__"] = field_names
return dataclasses.dataclass(cls)

Check warning on line 53 in command_line_assistant/config.py

View check run for this annotation

Codecov / codecov/patch

command_line_assistant/config.py#L52-L53

Added lines #L52 - L53 were not covered by tests

return wrap(cls)

Check warning on line 55 in command_line_assistant/config.py

View check run for this annotation

Codecov / codecov/patch

command_line_assistant/config.py#L55

Added line #L55 was not covered by tests


@dataclass
class LoggingSchema:
"""This class represents the [logging] section of our config.toml file."""

Expand All @@ -57,7 +76,7 @@ def __post_init__(self):
self.file = Path(self.file).expanduser()


@dataclasses.dataclass(slots=True)
@dataclass
class OutputSchema:
"""This class represents the [output] section of our config.toml file."""

Expand All @@ -69,7 +88,7 @@ def __post_init__(self):
self.file = Path(self.file).expanduser()


@dataclasses.dataclass(slots=True)
@dataclass
class HistorySchema:
"""This class represents the [history] section of our config.toml file."""

Expand All @@ -83,7 +102,7 @@ def __post_init__(self):
self.file = Path(self.file).expanduser()


@dataclasses.dataclass(slots=True)
@dataclass
class BackendSchema:
"""This class represents the [backend] section of our config.toml file."""

Expand Down

0 comments on commit 7e58335

Please sign in to comment.