Created
February 2, 2024 02:53
-
-
Save mypy-play/3e91cdf18f5a07b47d6619cb43940b6c to your computer and use it in GitHub Desktop.
Shared via mypy Playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from typing import overload, Self | |
| from enum import StrEnum as _StrEnum | |
| class StrEnum(_StrEnum): | |
| # mimic str.__new__'s overloads | |
| @overload | |
| def __new__(cls, object: object = ...) -> Self: ... | |
| @overload | |
| def __new__(cls, object: object, encoding: str = ..., errors: str = ...) -> Self: ... | |
| def __new__(cls, *values): | |
| # when we import enum, _StrEnum.__new__ gets moved to _new_member_ when | |
| # the "final" _StrEnum class is created via EnumType | |
| return _StrEnum._new_member_(cls, *values) | |
| class Choices(StrEnum): | |
| LOREM = "lorem" | |
| IPSUM = "ipsum" | |
| # this is still ok | |
| def ok_func() -> list[Choices]: | |
| return list(Choices) | |
| # and this no longer produces an error | |
| def error_func() -> list[Choices]: | |
| var = list(Choices) | |
| return var |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment