From 0909e9452780b8669d570b3725008b6e32bdfcfa Mon Sep 17 00:00:00 2001 From: LiraNuna <205628+LiraNuna@users.noreply.github.com> Date: Tue, 29 Nov 2022 04:17:48 -0600 Subject: [PATCH] Corrected `Colors` enum under Python 3.11 (#2590) Co-authored-by: Adam Hopkins Fixes https://github.com/sanic-org/sanic/issues/2589 --- sanic/log.py | 15 +++++++++++++-- tests/test_logging.py | 12 ++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/sanic/log.py b/sanic/log.py index 46879645..f6781e6d 100644 --- a/sanic/log.py +++ b/sanic/log.py @@ -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" diff --git a/tests/test_logging.py b/tests/test_logging.py index 63611f34..23a7d9a3 100644 --- a/tests/test_logging.py +++ b/tests/test_logging.py @@ -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