Skip to content

Commit

Permalink
refactor: Use string and integer enumerations
Browse files Browse the repository at this point in the history
By using string and integer enumerations, the enumeration values can then be compared to actual strings and integers, without using their `.value` attribute.

This also means that the enumeration values can now be serialized to JSON without special casing them.
  • Loading branch information
pawamoy committed Jul 14, 2024
1 parent e1b7bd9 commit 06b383b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 36 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ classifiers = [
]
dependencies = [
"astunparse>=1.6; python_version < '3.9'",
"backports-strenum>=1.3; python_version < '3.11'",
"colorama>=0.4",
]

Expand Down
14 changes: 2 additions & 12 deletions src/_griffe/encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from typing import TYPE_CHECKING, Any, Callable

from _griffe import expressions
from _griffe.enumerations import DocstringSectionKind, Kind, ParameterKind
from _griffe.enumerations import Kind, ParameterKind
from _griffe.models import (
Alias,
Attribute,
Expand All @@ -26,25 +26,15 @@
Parameter,
Parameters,
)
from griffe.enumerations import DocstringSectionKind, Kind, ParameterKind

if TYPE_CHECKING:
from enum import Enum

from griffe.enumerations import Parser


def _enum_value(obj: Enum) -> str | int:
return obj.value
from _griffe.enumerations import Parser


_json_encoder_map: dict[type, Callable[[Any], Any]] = {
Path: str,
PosixPath: str,
WindowsPath: str,
ParameterKind: _enum_value,
Kind: _enum_value,
DocstringSectionKind: _enum_value,
set: sorted,
}

Expand Down
48 changes: 37 additions & 11 deletions src/_griffe/enumerations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,36 @@

from __future__ import annotations

import enum


class DocstringSectionKind(enum.Enum):
import sys
from enum import IntEnum

# YORE: Bump 1.0.0: Replace block with line 2.
if sys.version_info >= (3, 11):
from enum import StrEnum
else:
from backports.strenum import StrEnum


class LogLevel(StrEnum):
"""Enumeration of available log levels."""

trace: str = "trace"
"""The TRACE log level."""
debug: str = "debug"
"""The DEBUG log level."""
info: str = "info"
"""The INFO log level."""
success: str = "success"
"""The SUCCESS log level."""
warning: str = "warning"
"""The WARNING log level."""
error: str = "error"
"""The ERROR log level."""
critical: str = "critical"
"""The CRITICAL log level."""


class DocstringSectionKind(StrEnum):
"""Enumeration of the possible docstring section kinds."""

text = "text"
Expand Down Expand Up @@ -40,7 +66,7 @@ class DocstringSectionKind(enum.Enum):
"""Admonition block."""


class ParameterKind(enum.Enum):
class ParameterKind(StrEnum):
"""Enumeration of the different parameter kinds."""

positional_only: str = "positional-only"
Expand All @@ -55,7 +81,7 @@ class ParameterKind(enum.Enum):
"""Variadic keyword parameter."""


class Kind(enum.Enum):
class Kind(StrEnum):
"""Enumeration of the different object kinds."""

MODULE: str = "module"
Expand All @@ -70,7 +96,7 @@ class Kind(enum.Enum):
"""Aliases (imported objects)."""


class ExplanationStyle(enum.Enum):
class ExplanationStyle(StrEnum):
"""Enumeration of the possible styles for explanations."""

ONE_LINE: str = "oneline"
Expand All @@ -83,7 +109,7 @@ class ExplanationStyle(enum.Enum):
"""Explanation as GitHub workflow commands warnings, adapted to CI."""


class BreakageKind(enum.Enum):
class BreakageKind(StrEnum):
"""Enumeration of the possible API breakages."""

PARAMETER_MOVED: str = "Positional parameter was moved"
Expand Down Expand Up @@ -112,7 +138,7 @@ class BreakageKind(enum.Enum):
"""Base class was removed"""


class Parser(enum.Enum):
class Parser(StrEnum):
"""Enumeration of the different docstring parsers."""

google = "google"
Expand All @@ -123,7 +149,7 @@ class Parser(enum.Enum):
"""Numpydoc-style docstrings parser."""


class ObjectKind(enum.Enum):
class ObjectKind(StrEnum):
"""Enumeration of the different runtime object kinds."""

MODULE: str = "module"
Expand Down Expand Up @@ -158,7 +184,7 @@ def __str__(self) -> str:


# YORE: Bump 1.0.0: Remove block.
class When(enum.Enum):
class When(IntEnum):
"""Enumeration of the different times at which an extension is used.
Deprecated. This enumeration is used with the `VisitorExtension` and `InspectorExtension` classes,
Expand Down
13 changes: 0 additions & 13 deletions src/_griffe/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,9 @@ def get_logger(name):
from __future__ import annotations

import logging
from enum import Enum
from typing import Any, Callable, ClassVar


class LogLevel(Enum):
"""Enumeration of available log levels."""

trace: str = "trace"
debug: str = "debug"
info: str = "info"
success: str = "success"
warning: str = "warning"
error: str = "error"
critical: str = "critical"


class _Logger:
_default_logger: Any = logging.getLogger
_instances: ClassVar[dict[str, _Logger]] = {}
Expand Down

0 comments on commit 06b383b

Please sign in to comment.