Corrected Colors enum under Python 3.11 (#2590)

Co-authored-by: Adam Hopkins <adam@amhopkins.com>
Fixes https://github.com/sanic-org/sanic/issues/2589
This commit is contained in:
LiraNuna 2022-11-29 04:17:48 -06:00 committed by GitHub
parent aef2673c38
commit 0909e94527
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 2 deletions

View File

@ -2,12 +2,23 @@ import logging
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,
@ -68,7 +79,7 @@ Defult logging configuration
"""
class Colors(str, Enum): # no cov
class Colors(StrEnum): # no cov
END = "\033[0m"
BOLD = "\033[1m"
BLUE = "\033[34m"

View File

@ -10,6 +10,7 @@ import pytest
import sanic
from sanic import Sanic
from sanic.log import Colors
from sanic.log import LOGGING_CONFIG_DEFAULTS, logger
from sanic.response import text
@ -250,3 +251,14 @@ def test_verbosity(app, caplog, app_verbosity, log_verbosity, exists):
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