Skip to content

Commit

Permalink
Corrected Colors enum under Python 3.11
Browse files Browse the repository at this point in the history
  • Loading branch information
LiraNuna committed Oct 26, 2022
1 parent 6b9edfd commit 124a9f0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
15 changes: 13 additions & 2 deletions sanic/log.py
Expand Up @@ -2,12 +2,23 @@
import sys

from enum import Enum
from typing import Any, Dict
from typing import TYPE_CHECKING, Any, Dict
from warnings import warn

from sanic.compat import is_atty


# Python 3.11 changed the way Enum formatting works for mixed-in types.
if sys.version_info < (3, 11, 0):

class StrEnum(str, Enum):
pass

else:
if not TYPE_CHECKING:
from enum import StrEnum


LOGGING_CONFIG_DEFAULTS: Dict[str, Any] = dict( # no cov
version=1,
disable_existing_loggers=False,
Expand Down Expand Up @@ -68,7 +79,7 @@
"""


class Colors(str, Enum): # no cov
class Colors(StrEnum): # no cov
END = "\033[0m"
BOLD = "\033[1m"
BLUE = "\033[34m"
Expand Down
12 changes: 12 additions & 0 deletions tests/test_logging.py
Expand Up @@ -10,6 +10,7 @@
import sanic

from sanic import Sanic
from sanic.log import Colors
from sanic.log import LOGGING_CONFIG_DEFAULTS, logger
from sanic.response import text

Expand Down Expand Up @@ -250,3 +251,14 @@ def log_info(request):

if app_verbosity == 0:
assert ("sanic.root", logging.INFO, "DEFAULT") in caplog.record_tuples


def test_colors_enum_format():
assert f'{Colors.END}' == Colors.END.value
assert f'{Colors.BOLD}' == Colors.BOLD.value
assert f'{Colors.BLUE}' == Colors.BLUE.value
assert f'{Colors.GREEN}' == Colors.GREEN.value
assert f'{Colors.PURPLE}' == Colors.PURPLE.value
assert f'{Colors.RED}' == Colors.RED.value
assert f'{Colors.SANIC}' == Colors.SANIC.value
assert f'{Colors.YELLOW}' == Colors.YELLOW.value

0 comments on commit 124a9f0

Please sign in to comment.