Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sanic colors with python 3.11 displayed incorrectly #2589

Closed
1 task done
LiraNuna opened this issue Oct 26, 2022 · 2 comments · Fixed by #2590
Closed
1 task done

Sanic colors with python 3.11 displayed incorrectly #2589

LiraNuna opened this issue Oct 26, 2022 · 2 comments · Fixed by #2590
Labels

Comments

@LiraNuna
Copy link
Contributor

LiraNuna commented Oct 26, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Using Python3.11, the DEBUG message when starting the server shows:

[2022-10-26 11:43:52 -0500] [109121] [DEBUG] Colors.BLUEStarting a process: Colors.BOLDColors.SANICSanic-Server-0-0Colors.END
[2022-10-26 11:43:52 -0500] [109121] [DEBUG] Colors.BLUEStarting a process: Colors.BOLDColors.SANICSanic-Reloader-0Colors.END

This is fairly minor issue but could be a note of a larger issue.

Code snippet

No response

Expected Behavior

No response

How do you run Sanic?

As a script (app.run or Sanic.serve)

Operating System

Linux

Sanic Version

22.9.0

Additional context

Using Python3.10 works correctly.

@LiraNuna LiraNuna added the bug label Oct 26, 2022
@LiraNuna
Copy link
Contributor Author

LiraNuna commented Oct 26, 2022

I can get the issue to fix if I change the Colors Enum to be class Colors(StrEnum): using the new StrEnum class in python3.11. This is obviously not perfect for backwards compatibility.

This item from the python3.11 changelog may be the root cause:

Changed Enum.__format__() (the default for format(), str.format() and f-strings) of enums with mixed-in types (e.g. int, str) to also include the class name in the output, not just the member’s key. This matches the existing behavior of enum.Enum.__str__(), returning e.g. 'AnEnum.MEMBER' for an enum AnEnum(str, Enum) instead of just 'MEMBER'.

I think a quick fix for this would maybe something like this (untested but hopefully gets the point across):

import sys

if sys.version_info < (3, 11, 0):
    from enum import Enum
    class StrEnum(str, Enum): pass
else:
    from enum import StrEnum    

...

class Colors(StrEnum):
    ...

Another fix may be to manually restore the old behavior of __format__ with an override:

class Colors(str, Enum):  # no cov
    ...

    def __format__(self, format_spec: str) -> str:
        return self.value + format_spec

@ahopkins
Copy link
Member

Yes, the StrEnum fix is exactly something I've been planning once it was available. Happy to see you make a PR with this! 😎

ahopkins pushed a commit that referenced this issue Nov 29, 2022
Co-authored-by: Adam Hopkins <adam@amhopkins.com>
Fixes #2589
awesomo4000 pushed a commit to awesomo4000/sanic that referenced this issue Dec 3, 2022
Co-authored-by: Adam Hopkins <adam@amhopkins.com>
Fixes sanic-org#2589
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants