Skip to content

♻️ REFACTOR: __slots__ for dataclasses #214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions markdown_it/_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from __future__ import annotations

from collections.abc import Mapping
import sys
from typing import Any

if sys.version_info >= (3, 10):
DATACLASS_KWARGS: Mapping[str, Any] = {"slots": True}
else:
DATACLASS_KWARGS: Mapping[str, Any] = {}
4 changes: 3 additions & 1 deletion markdown_it/ruler.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class Ruler
from dataclasses import dataclass, field
from typing import TYPE_CHECKING

from markdown_it._compat import DATACLASS_KWARGS

if TYPE_CHECKING:
from markdown_it import MarkdownIt

Expand Down Expand Up @@ -50,7 +52,7 @@ def src(self, value: str) -> None:
RuleFunc = Callable


@dataclass()
@dataclass(**DATACLASS_KWARGS)
class Rule:
name: str
enabled: bool
Expand Down
3 changes: 2 additions & 1 deletion markdown_it/rules_inline/state_inline.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from dataclasses import dataclass
from typing import TYPE_CHECKING

from .._compat import DATACLASS_KWARGS
from ..common.utils import isMdAsciiPunct, isPunctChar, isWhiteSpace
from ..ruler import StateBase
from ..token import Token
Expand All @@ -13,7 +14,7 @@
from markdown_it import MarkdownIt


@dataclass()
@dataclass(**DATACLASS_KWARGS)
class Delimiter:
# Char code of the starting marker (number).
marker: int
Expand Down
4 changes: 3 additions & 1 deletion markdown_it/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from typing import Any
import warnings

from markdown_it._compat import DATACLASS_KWARGS


def convert_attrs(value: Any) -> Any:
"""Convert Token.attrs set as ``None`` or ``[[key, value], ...]`` to a dict.
Expand All @@ -18,7 +20,7 @@ def convert_attrs(value: Any) -> Any:
return value


@dc.dataclass()
@dc.dataclass(**DATACLASS_KWARGS)
class Token:

type: str
Expand Down