Created
February 2, 2024 03:30
-
-
Save mypy-play/f32efd891ff8b25fa333c937093682e1 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" | |
| def my_func(obj: Choices) -> None: | |
| assert isinstance(obj, Choices) | |
| for element in list(Choices): | |
| my_func(element) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment