Skip to content

Instantly share code, notes, and snippets.

@mypy-play
Created February 2, 2024 02:53
Show Gist options
  • Select an option

  • Save mypy-play/3e91cdf18f5a07b47d6619cb43940b6c to your computer and use it in GitHub Desktop.

Select an option

Save mypy-play/3e91cdf18f5a07b47d6619cb43940b6c to your computer and use it in GitHub Desktop.
Shared via mypy Playground
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