Skip to content

Instantly share code, notes, and snippets.

@victorusachev
Created November 18, 2020 19:49
Show Gist options
  • Save victorusachev/a6711a7bcbf404484c25d491de5678b1 to your computer and use it in GitHub Desktop.
Save victorusachev/a6711a7bcbf404484c25d491de5678b1 to your computer and use it in GitHub Desktop.
import enum
from typing import Iterable, Type
def merge_enums(new_name: str, *enums: Iterable[Type[enum.Enum]]) -> Type[enum.Enum]:
attributes = [{m.name: m.value for m in e} for e in enums]
intersection = set.intersection(*map(set, attributes))
if intersection:
raise ValueError(f"Error codes are duplicated: {', '.join(intersection)}")
return enum.Enum(new_name, {k: v for e in attributes for k, v in e.items()})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment