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:
parent
aef2673c38
commit
0909e94527
15
sanic/log.py
15
sanic/log.py
|
@ -2,12 +2,23 @@ import logging
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from typing import Any, Dict
|
from typing import TYPE_CHECKING, Any, Dict
|
||||||
from warnings import warn
|
from warnings import warn
|
||||||
|
|
||||||
from sanic.compat import is_atty
|
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
|
LOGGING_CONFIG_DEFAULTS: Dict[str, Any] = dict( # no cov
|
||||||
version=1,
|
version=1,
|
||||||
disable_existing_loggers=False,
|
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"
|
END = "\033[0m"
|
||||||
BOLD = "\033[1m"
|
BOLD = "\033[1m"
|
||||||
BLUE = "\033[34m"
|
BLUE = "\033[34m"
|
||||||
|
|
|
@ -10,6 +10,7 @@ import pytest
|
||||||
import sanic
|
import sanic
|
||||||
|
|
||||||
from sanic import Sanic
|
from sanic import Sanic
|
||||||
|
from sanic.log import Colors
|
||||||
from sanic.log import LOGGING_CONFIG_DEFAULTS, logger
|
from sanic.log import LOGGING_CONFIG_DEFAULTS, logger
|
||||||
from sanic.response import text
|
from sanic.response import text
|
||||||
|
|
||||||
|
@ -250,3 +251,14 @@ def test_verbosity(app, caplog, app_verbosity, log_verbosity, exists):
|
||||||
|
|
||||||
if app_verbosity == 0:
|
if app_verbosity == 0:
|
||||||
assert ("sanic.root", logging.INFO, "DEFAULT") in caplog.record_tuples
|
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
|
||||||
|
|
Loading…
Reference in New Issue
Block a user