Created
November 18, 2020 19:49
-
-
Save victorusachev/a6711a7bcbf404484c25d491de5678b1 to your computer and use it in GitHub Desktop.
This file contains 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
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